Page 1 of 1

PHP form help

Posted: Fri Nov 19, 2004 7:26 am
by dave79
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:

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="&nbsp;"; 
// 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(); 
?>
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

Posted: Fri Nov 19, 2004 7:34 am
by peni
try using anchors:

$location = "http://www.mywebsite/feedbacksent.html#form";

and don't forget to add sth like "<a name='form'></a>" somewhere above your form.

Posted: Fri Nov 19, 2004 9:34 am
by dave79
Thanks Peni. Because I'm completely new to PHP, could you spell out the whole statement because I tried something like that, and it didn't work. It would also be a great help if you could point out exactly where it should go?

Posted: Fri Nov 19, 2004 10:46 am
by peni
in lines 20 and 21 of your code you redirect the visitor finally to that "thank-you" crap. if you specify an anchor by adding at the end of the url "#form" (like i wrote in my previous post).

also you will have to find that line in your feedbacksent.html where you insert the form and put the anchor above it. sth like:

Code: Select all

<a name="form"></a>
<form id="name" action= &#1111;... bla ...]

Posted: Sat Nov 20, 2004 6:43 am
by dave79
Thanks again for the reply.

Perhaps my comments weren't that clear. The script above is for a page with a form on it (we'll call this page A).

If the user passes validation, then they get referred to feedbacksent.html (page B). Page B doesn't have a form on it; its a basic html page with a simple thank you message.

If the user fails validation, I want the view to jump back to the form on page A. Therefore, your suggestion won't work.

I've used:
$location = "#form";
header ("Location:$location");
...in various places but I just can't make it work! Any ideas how to implement it?

Posted: Sat Nov 20, 2004 6:56 am
by peni

Code: Select all

<?php
//do you recognize this place in your code?
if (isset($HTTP_POST_VARS["submit"]) and ($checked)) { 
    $msg .= "Firstname:  ".$HTTP_POST_VARS["firstname"]."\n"; 
    $location = "http://www.mywebsite/feedbacksent.html"; 
    header ("Location:$location"); 
} 
//and now your redirection for the users who failed the validation:
elseif (isset($HTTP_POST_VARS["submit"]) and (!$checked)) {
    $location = "http://www.yoursite/pageAthatcontainsformandananchor.php#form";
    header ("Location:$location"); 
}

ob_end_flush(); 
?>

Posted: Sat Nov 20, 2004 7:00 am
by potsed
what peni is saying is the correct way to do it .. to maybe clarify what is said ...

in page B there is

Code: Select all

$location = "http://www.mywebsite/feedbacksent.html"; 
header ("Location:$location");
on line 23 & 24...

change add #form to the end of the existing location like

Code: Select all

$location = "http://www.mywebsite/feedbacksent.html#form";
Then in page A add the anchor

Code: Select all

<a name="form"></a>
just above the <form> tag..

That should work...

Posted: Sat Nov 20, 2004 7:07 am
by peni
as i did understand dave's last post:

the navigation to an anchor is only for those people who failed the validation and will not see page B with the thank-you-stuff.
as dave doesn't have a redirect for the failed ones (how goddamn lyrical) at all, i wrote another elseif condition with this redirect.

but, probably, i didn't get the problem at all.. dave?

Posted: Sat Nov 20, 2004 11:21 am
by dave79
Firstly, thanks to both of you for your time on this.

Peni - you definitely understand where I'm trying to go with this. I've just implemented your solution and now, on a form that is incorrectly filled in, the browser view *does* jump to the form, but the validation technique that was working (see the example in my first post)...has stopped working:
http://www.testarea.north-square.com/fo ... mplate.php

Posted: Sun Nov 21, 2004 7:55 am
by peni
well, now i've took a look at your code once again and i'm wondering about the flag $checked. as i understand, this is your flag to see if the validation is passed, but i couldn't point out, how do you handle it. is it always true, until the user fails?
as you see, my added elseif condition asks, if the form was submitted and if the flag $checked is false.

is it possible, that you took my code and replaced your whole code with it? then you have to check if the declaration of the function check_len() is still there and you use it to check user's input.

probably it would be easier if you post the whole code again (without any shortenings).

Posted: Sun Nov 21, 2004 3:34 pm
by dave79
I obtained the code from a scripts website and because I've only just started learning PHP, I don't know the answer to your question. I think I implemented your solution as you intended. Full text version:
http://www.testarea.north-square.com/fo ... _edits.txt

And that code produces the following page:
http://www.testarea.north-square.com/fo ... mplate.php

This page does not display validation errors (unlike the first link in my first post). However, it does still send me the form mailer and it does jump the user back to the form (if they fill in the form incorrectly).

Once again, thanks for your time.

Posted: Sun Nov 21, 2004 4:08 pm
by peni
now, i've got the dilema. the errors will only be shown if you don't use the redirection to "...#form".

it means:

1. kill that piece of code i provided to you, it's unnecessary. (i mean the lines of the alternative condition "elseif( ...")

2. find the line where you start the form and change it that way:

Code: Select all

<form id="form" action="<? echo $GLOBALS&#1111;"PHP_SELF"] ?>#form" method="post">

Posted: Mon Nov 22, 2004 3:38 am
by dave79
It works!!! Thank you so much Peni - I've been trying to resolve this for ages. I really appreciate your time.

Dave