How to merge mail.php into my index.php

A

Anonymous

Guest
I have a contact form in my index.php as follows:
Code:
   <div "myform"> 

    <br /><h1>Need help? Have a suggestion? Why not send us an email.</h1>
          <h3><center>You'll receive a copy for your records</center></h3>  
    <br />

        <form id="form1"  action="mail.php" method="POST">

            <label>Name</label>
                <input type="text" name="name">

            <label>Email</label>
                <input type="text" name="email">

             <select name="type" id="category" size="1" required>
                <option>Select A Category</option>
                <option>Questions</option>
                <option>Report Problems</option>
                <option>Suggestion</option>
                <option>Other</option>
                <option>Website Problem</option>
            </select>

<br /><br /> <br />  
                 
            <label>Message</label>
                <textarea  name="message" rows="7"></textarea><br />
          
            <button type="submit" value="Send" style="margin-top:15px; margin-left: 500px; text-align: center;">Send Email</button>
          
        <div class="spacer"></div>
<br /><br /><br /><br /><br /><br />  
        </form>
    </div>   
</div>

I'd like to merge my mail.php into it. My mail.php code is:
Code:
<?php
$subject = $_POST['type'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name\r\nEmail: $email\r\nMessage: $message";
if ($subject =="Website Problem") {
        $recipient="webmaster@foxclone.com" ;
      }
else{
        $recipient="help@foxclone.com";
      }
$mailheader = "From: $email\r\nBcc: $email\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you for contacting us!" . " -" . "<a href='index.php#contact' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Is this possible? If so, where should it be inserted, before or after the closing of the form?

Thanks in advance,
Larry
 
You need to do the PHP first so that you can check if the form has been filled in and that the data supplied is of the correct format so that you don't let any bad code get executed on your server.

If the form has been filled in then you send the email, otherwise you offer the form up along with any entered details that are incorrect with a helpful note explaining what is wrong with the supplied data.
 
When I insert it anywhere in my index.php the page never loads. It works fine as a separate mail.php file.
 
hyper said:
check if the form has been filled in

Check that the data supplied is of the correct format so that you don't let any bad code get executed on your server.

If the form has been filled in then you send the email

otherwise you offer the form up along with any entered details that are incorrect with a helpful note explaining what is wrong with the supplied data.

When I insert it anywhere ... the page never loads
It does.

You need to do a bit more than just insert it.
 
I've gone to a separate contact.php page with validation and send in mail.php. If I put the code from mail.php anywhere in the contact.php page after changing the <form id="form1" method="POST" action="mail.php"> to <form id="form1" method="POST" action=" "> I get the following errors:
Code:
error; you need to submit the form!
Notice: Undefined index: name in /home/larry/web/foxclone/public_html/contact.php on line 7

Notice: Undefined index: email in /home/larry/web/foxclone/public_html/contact.php on line 8

Notice: Undefined index: message in /home/larry/web/foxclone/public_html/contact.php on line 9

Notice: Undefined index: type in /home/larry/web/foxclone/public_html/contact.php on line 10
All fields are mandatory!
The php code starts like this
Code:
<?php
if(!isset($_POST['submit']))
{
	//This page should not be accessed directly. Need to submit the form.
	echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$type = $_POST['type'];

Running Apache2 and PHP 7.2 in Linux Mint 19.3. What am I doing wrong?
 
I fixed the previous problem and encountered a new one that I think I know the cause. In the validations, there are exit statements that are taking me back to index.php instead of just displaying the error and exiting the script, staying on the contact.php page. Code follows:
Code:
<?php
if(isset($_POST['submit']))
{
	
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$type = $_POST['type'];

//Validate first
if(empty($name)||empty($visitor_email)||empty($type)||empty($message)) 
{
    echo "All fields are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

    $subject = $_POST['type'];
    $subject2 = "Copy of email sent to foxclone.com";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name\r\nEmail: $email\r\nMessage: $message";
    if ($subject =="Website Problem") {
            $to="webmaster@foxclone.com" ;
          }
    else {
            $to="help@foxclone.com";
          }
    
$email_from = $email;//<== update the email address
$email_subject = $type;
$email_body = $formcontent.
    
$headers = "From: $email \r\n";

//Send the email!
mail($to, $subject, $formcontent, $headers) or die("Error1!");
mail($email, $subject2, $formcontent, $headers) or die("Error2!");

//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
}   
?>

How do I fix this?

Larry

P.S. I'm taking notes so I don't have to ask the same question again.

EDIT: Commenting out the exits doesn't help. Not sure where to look at this point.

EDIT2: Researched further and found that <form id="form1" method="POST" action="/"> and <form id="form1" method="POST" action=" "> don't work in html5. How do I handle this?
 
Back
Top