The check in php and sending the form not cooperate

Hi,
Please advice and assistance. I have a form with php control that is functional. How to embed php script to send it goes send unfilled. Will you help me where is mistake? I’m not good at php.
PHP for checking:
[sup]

<?php $titleErr = $nameErr = $emailErr = $commentErr = ""; $title = $name = $email = $comment = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["title"])) { $titleErr = "You must enter Title!"; } else { $title = test_input($_POST["title"]); } if (empty($_POST["name"])) { $nameErr = "Must enter the Name!"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Must enter the name Email!"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["comment"])) { $commentErr = "Must enter the name comment"; } else { $comment = test_input($_POST["comment"]); if (!preg_match("/^[a-zA-Z ]*$/",$comment)) { $commentErr = "Only letters and white space allowed"; } } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> "> Write us what you need to know * required field.

Title:
* <?php echo $titleErr;?> / Mr, Ms, … /



Name:
* <?php echo $nameErr;?> / First Name and Last Name /



E-mail:
* <?php echo $emailErr;?>



Comment: *



<?php echo $comment;?>
<?php echo $commentErr;?>


[/sup]

PHP for sending to email:
[sup]

<?php if(isset($_POST['submit'])){ $to = "@my email"; $from = $_POST['email']; $title = $_POST['title']; $name = $_POST['name']; $subject = "Info from"; $subject2 = "Copy of your form submission"; $comment = $title . " " . $name . " " . "\n\n" . $_POST['comment']; $comment2 = "Here is a copy of your comment " . $name . "\n\n" . $_POST['comment']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$comment,$headers); mail($from,$subject2,$comment2,$headers2); echo header("Location: http://my web/thanks.html"); } ?>

[/sup]
Please add a script, as far as it will go:
The form was not sent with the redirect on webpage errors and click on this link goes back on a partly completed form.
Thank you very much for your help!!!

willH, not very sure what you want help with. Do you mean you want to add in the email code into the
other code? If so, do it this way:
[php]

<?php $titleErr = $nameErr = $emailErr = $commentErr = ""; $title = $name = $email = $comment = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["title"])) { $titleErr = "You must enter Title!"; } else { $title = test_input($_POST["title"]); } if (empty($_POST["name"])) { $nameErr = "Must enter the Name!"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Must enter the name Email!"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["comment"])) { $commentErr = "Must enter the name comment"; } else { $comment = test_input($_POST["comment"]); if (!preg_match("/^[a-zA-Z ]*$/",$comment)) { $commentErr = "Only letters and white space allowed"; } } } // Check error messages, if none, then send email... if ($titleErr=="" AND $nameErr=="" AND $emailErr=="" AND $commentErr=="") { $to = "@my email"; $from = $_POST['email']; $title = $_POST['title']; $name = $_POST['name']; $subject = "Info from"; $subject2 = "Copy of your form submission"; $comment = $title . " " . $name . " " . "\n\n" . $_POST['comment']; $comment2 = "Here is a copy of your comment " . $name . "\n\n" . $_POST['comment']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$comment,$headers); mail($from,$subject2,$comment2,$headers2); echo header("Location: http://my web/thanks.html"); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Write us what you need to know * required field.

Title:
* <?php echo $titleErr;?> / Mr, Ms, … /



Name:
* <?php echo $nameErr;?> / First Name and Last Name /



E-mail:
* <?php echo $emailErr;?>



Comment: *



<?php echo $comment;?>
<?php echo $commentErr;?>


[/php] There are better ways to do this project. One would be to just use one variable for the error messages. You can concatenate the errors instead of using several variables for them. This means only one variable and one check before sending the email. But, first let's get your code working.

The change I made was to check for the errors instead of checking for the submit button before sending
the email. You do not want to send the email if there is an error message. Hope that makes sense!

There has been a lot of discussion about security on this type of site. It has be decided that you should
not use an address inside of the form’s action function if it posts to itself. Therefore, you should change
the line: <form method=“post” action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
to this instead: On all new server’s this is handled correctly and it does
prevent hackers from seeing your page and folder layout if they grab your session info.

Oh, also, please place your code next time between the tags. Just press the PHP button above
your editor area and place the code inside those tags. It helps us copy them into our editors. Thanks!

Web site does not want to display. Appears thanks for the email.
This reference: header(“Location: http://my web/thanks.html”);
In an email account are empty mails.
Is possible to fix it?? Thanks.

Very sorry, I missed this part…

  echo
  header("Location: http://my web/thanks.html");
  }

Take out the blank “echo” … It will break the code…

  header("Location: http://my web/thanks.html");
  }

I have changed the script:

mail($to,$subject,$comment,$headers);
mail($from,$subject2,$comment2,$headers2);

  header("Location: http://pcsanatorium.co.uk/thanks.html");
  }

function test_input($data) {

Web site does not want to display.

What does not display? The original page or after you enter data, the thank you page?

Not displayed form !

Does not display the original page. Appears only thank you for sending a query!

It looks like you are doing this on different pages.

May I suggest validate the form and on validation, pass the values to the send email page?

Ok. Write me what where to add. Please write to me in detail. PHP’m not so good.

The only way to learn, is to do. I am not handing you code nor giving step by step instructions. This is your thing, if something is unclear I will clarify it.

Well, sometimes a programmer can not see the tree in the forest! I am sorry that I missed this.
(A beginner error!) Well, two issues. First, the way you check for the post is to check if the button was
active or not. You should not really test it by testing the post itself. Most PHP servers “post” the page
to the browser so you really need to check for the button itself instead. Also, the error I missed was that
the email code was not inside the post code. What I am saying is that if there is no posted form, no submit
button was pressed, it still executes the email section. That is wrong. It should be inside the post code.
Here is a fixed up version… Hope it helps you.
[php]

<?php $titleErr = $nameErr = $emailErr = $commentErr = ""; $title = $name = $email = $comment = ""; if (isset($_POST["submit"])) { if (empty($_POST["title"])) { $titleErr = "You must enter Title!"; } else { $title = test_input($_POST["title"]); } if (empty($_POST["name"])) { $nameErr = "Must enter the Name!"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } } if (empty($_POST["email"])) { $emailErr = "Must enter the name Email!"; } else { $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } } if (empty($_POST["comment"])) { $commentErr = "Must enter the name comment"; } else { $comment = test_input($_POST["comment"]); if (!preg_match("/^[a-zA-Z ]*$/",$comment)) { $commentErr = "Only letters and white space allowed"; } } // Check error messages, if none, then send email... if ($titleErr=="" AND $nameErr=="" AND $emailErr=="" AND $commentErr=="") { $to = "@my email"; $from = $_POST['email']; $title = $_POST['title']; $name = $_POST['name']; $subject = "Info from"; $subject2 = "Copy of your form submission"; $comment = $title . " " . $name . " " . "\n\n" . $_POST['comment']; $comment2 = "Here is a copy of your comment " . $name . "\n\n" . $_POST['comment']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$comment,$headers); mail($from,$subject2,$comment2,$headers2); header("Location: thanks.html"); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Write us what you need to know * required field.
 Title: <input type="text" name="title" size="5" value="<?php echo $title;?>">
 <span class="error"><big> * </big><?php echo $titleErr;?></span><span id="example"> / Mr, Ms, ... / </span>
 <br><br>     
 Name: <input type="text" name="name" size="30" value="<?php echo $name;?>">
 <span class="error"><big> * </big> <?php echo $nameErr;?></span><span id="example"> / First Name and Last Name / </span>
 <br><br>
 E-mail: <input type="text" name="email" size="40" value="<?php echo $email;?>">
 <span class="error"><big> * </big> <?php echo $emailErr;?></span>
 <br><br>
 Comment:<span class="error" style="position:relative; top:2px;"><big> * </big></span>
          <br><br>
         <textarea name="comment" rows="7" cols="55"> <?php echo $comment;?></textarea>
         <span class="error"><?php echo $commentErr;?></span>
 <br>
<div  id = "rocaptcha_placeholder" ></div>
  <script  type = "text/javascript"  src = "http://rocaptcha.com/api/js/?key=my key..." ></script>
  <script  type = "text/javascript" >
       RoCaptcha . init ( "rocaptcha_placeholder" );
  </script>
 <br>
<input type="submit" name="submit" value="Submit" id="send">
[/php] One last issue. I just noticed you did not close your tag. You should add that in too....

Please help to solve this problem:

[sup]
// Check error messages, if none, then send email…
if ($titleErr=="" AND $nameErr=="" AND $emailErr=="" AND $commentErr=="") {
$to = “@…My email…”;
$from = $_POST[‘email’];
$title = $_POST[‘title’];
$name = $_POST[‘name’];
$subject = “Info from”;
$subject2 = “Copy of your form submission”;
$comment = $title . " " . $name . " " . “\n\n” . $_POST[‘comment’];
$comment2 = "Here is a copy of your comment " . $name . “\n\n” . $_POST[‘comment’];

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
  mail($to,$subject,$comment,$headers);
  mail($from,$subject2,$comment2,$headers2);
  header("Location: http://..My web../thanks.html"); 
  }

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>
[/sup]

If I have written this line header(“Location: http://…My web…/thanks.html”); so displays just thank you page and form is sent without filling details. It happened once I get there joined “echo”.
You could do solve??

I already answered your issues. As far as the header line goes, you just need it to point at the page.
So, header(“Location: thanks.html”); should work if the thanks page is in the same folder…

It works now!!! ErnieAlex i thank you a lot!!

I have request:

It would be possible to modify the end of the script?
I need there to insert a script:
Poorly completed form. >>> Redirection to error page - error list. >>> Referring back to the wrong completed form.
If it was possible, please a lot about help.Thanks!!!

It works now!! ErnieAlex i thank you a lot!!! :smiley:

willH, Great! Always nice feeling to solve a programming puzzle ! ! ! Glad to help.

I will mark this one solved. If you have another one, just open a new post.

Good luck in your programming…

Sponsor our Newsletter | Privacy Policy | Terms of Service