PHP form help
Posted: Fri Nov 19, 2004 7:26 am
AIMS OF MY SCRIPT
I am trying to create a form mailer that uses PHP validation. The form is “below the fold” i.e. users have to scroll down to it. I am aiming to achieve the following:
If the form is completed correctly, load feedbacksent.html and send me an email with the form’s contents. If the form is completed incorrectly, refresh the current page, showing errors and then jump the browser view down to where the form is located.
NOTE: I want to avoid JavaScript and I believe from the example below that this can be done without JavaScript.
THE PARTS OF MY SCRIPT THAT WORK
The following parts of my script work OK: 1) the validation technique ii) the email to me with the form’s contents iii) Referring the users to feedbacksent.html.
PROBLEM WITH MY SCRIPT / HELP REQUIRED
The part that fails is, jumping the browser back down to the form. So, my question is, how can I make the browser jump down to my form?
REASONS WHY JAVASCRIPT NOT REQUIRED / PROGRESS SO FAR
I have included the following in the FORM element:
id=“form”
I know the concept of making the browser jump using PHP *can* work because I've created this example. Here, instead of a correctly completed form taking the user to "feedbacksent.html", it jumps users back to the form. If you would like to see what I mean, on the following page position the form at the very top of the screen, complete the fields and submit the form:
http://www.testarea.north-square.com/fo ... d_test.php
MY SCRIPT IN FULL / HIGHLIGHTS OF MY SCRIPT
My code can be seen here in full as text (probably easier to read).
http://www.testarea.north-square.com/fo ... mplate.txt
Here are highlights of my code:
I realise that there is nothing in the code which *should* make the browser jump to the form. The reason is because all my attempts have failed, so I thought I would see if an expert can help me out. I would really appreciate any help here.
Regards,
Dave
I am trying to create a form mailer that uses PHP validation. The form is “below the fold” i.e. users have to scroll down to it. I am aiming to achieve the following:
If the form is completed correctly, load feedbacksent.html and send me an email with the form’s contents. If the form is completed incorrectly, refresh the current page, showing errors and then jump the browser view down to where the form is located.
NOTE: I want to avoid JavaScript and I believe from the example below that this can be done without JavaScript.
THE PARTS OF MY SCRIPT THAT WORK
The following parts of my script work OK: 1) the validation technique ii) the email to me with the form’s contents iii) Referring the users to feedbacksent.html.
PROBLEM WITH MY SCRIPT / HELP REQUIRED
The part that fails is, jumping the browser back down to the form. So, my question is, how can I make the browser jump down to my form?
REASONS WHY JAVASCRIPT NOT REQUIRED / PROGRESS SO FAR
I have included the following in the FORM element:
id=“form”
I know the concept of making the browser jump using PHP *can* work because I've created this example. Here, instead of a correctly completed form taking the user to "feedbacksent.html", it jumps users back to the form. If you would like to see what I mean, on the following page position the form at the very top of the screen, complete the fields and submit the form:
http://www.testarea.north-square.com/fo ... d_test.php
MY SCRIPT IN FULL / HIGHLIGHTS OF MY SCRIPT
My code can be seen here in full as text (probably easier to read).
http://www.testarea.north-square.com/fo ... mplate.txt
Here are highlights of my code:
Code: Select all
<?php
ob_start();
// I’ve been advised to use ob_start because I am outputting code prior to using header
function check_len(&$check, $field, $max, &$err_field, $err="", $min,
// etc, more statements here that determine validation. If you need to see these in full, please look at the text version above.
if (empty($HTTP_POST_VARS["firstname"])) $HTTP_POST_VARS["firstname"]=""; if (empty($err_firstname)) $err_firstname=" ";
// More statements here, 1 for each field in the form
$checked = true; if (isset($HTTP_POST_VARS["submit"]))
{check_len($checked, $HTTP_POST_VARS["firstname"],50,$err_firstname,"Your first name cannot exceed 50 characters",1,"You must fill in your first name");
// More statements here, 1 for each field in the form}
if ( empty($HTTP_POST_VARS["submit"]) or (!$checked) )
?>
<!-- my HTML goes here. Including my form with id="form" -->
<?php
if (isset($HTTP_POST_VARS["submit"]) and ($checked))
{
$msg .= "Firstname: ".$HTTP_POST_VARS["firstname"]."\n";
// More statements here, 1 for each field in the form
mail ("info@mywebsite.com","Email subject goes here", $msg);
$location = "http://www.mywebsite/feedbacksent.html";
header ("Location:$location");
}
ob_end_flush();
?>Regards,
Dave