Forum Moderators: coopster

Message Too Old, No Replies

Form Questions!

         

dreamcatcher

12:39 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys,

I wrote this guestbook script and I have a couple of questions. Firstly, someone else has tried to use it and they have this problem:

On the guestbook page there is a "sign guestbook" link. After they have clicked that and entered their information, they get a preview of their entry, this is written into the sign.php page, ie:

if ($entry==preview)

do actions;

This displays a preview of their entry, obviously. Now if some required fields aren`t filled in an error message appears informing the visitor to back and try again. I`ve used the javascript:history.go(-1) code which works fine for me.

Problem is, in this other persons browser, they cannot go back. Even if they use their browser back button, the page doesn`t return to the sign.php page with the form. All they can do is click the back button twice and return to the actual guestbook page.

Any idea what might be causing this?

Thanks,

:)

dreamcatcher

4:36 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do now have a link to the persons guestbook. Basically the error message appears, but when you click "Back" nothing happens. If you click the the browsers back button it goes back to the actual guestbook page, so its going back two pages in affect.

It would appear as though the server is running in Safe Mode, I wonder if that has anything to do with it?

Strange.

hakre

5:59 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi dreamcatcher,

after reading your posting, i think you're using ms internet explorer. the history.go(-1) acts that way you describe with some forms as far as i know.

to solve your problem, why don't you display the error message and the form on the same page? this will solve the back-button problem and will help the user to correct the input he/she made. you can even mark the erroreos fields with an exclamation mark for example.

checkout this form as an example [pyrosys.de]. it's in german language but you can understand what i mean - just press the '-> weiter' button on the end of the page. this one is completly based on the users input and will display errormessages automaticly.

Shadi

9:37 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



Primer in Form Error Handling

I would highly recommend that you don't use javascript but use PHP for the error infomration. have your form in a function such as



function signGuestbook($error=0)
{
if($error) print $error;
print "myform";
}

"myform" is basicly your HTML of the form you are sending.
once the user submits the form, do the error correction and see what errors

e.g. check if they have a valid email



if (!preg_match("/^[A-Za-z0-9\_]*@[A-Za-z0-9\_]*\.[A-Za-z]{1,4}\.{0,1}[A-Za-z]{0,3}$/",$_REQUEST['email'])) $error.="Your email address must be in the correct form.<p>";

then after all your error checking check if there is anything inside the variable $error



if ($error) signGuestbook($error);
else
{
Insert into Database Logic
}

this will then see and error and get your form back for the user, to edit.

a usfull thing then for your form is to also place the variables they sent you back in the boxes.



<tr>
<td width=\"53%\">Email</td>
<td width=\"47%\"><input type=\"text\" name=\"Email\" value=\"".$_REQUEST['email']."\" style=\"border: 1 solid #000000; width: 150px;\"></td>
</tr>

Therefore this will return the form to the user, printing out the error, and then printing out the form with the already inputted values for the user (so they don't have to place them all again!)

vincevincevince

9:42 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mmm, I like that method with including the $_REQUEST[field] bits - but maybe you should addslashes() them to make sure it's not hijacked?

a lovey primer and welcome to the webmaster world!

a post for the library

hakre

9:56 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jup, stripslashes / addslashes (whatever your php config needs) and don't forget to encode the output (value="") into html entities not to let your (x)html form be spoiled is the only thing i can add, too. ;)

dreamcatcher

10:32 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the advice guys. I actually sorted the problem out and its working ok now. :)

In the original script I was doing the following:

if ($REQUEST['preview']) {

blah blah;

}

and

if ($REQUEST['sign']) {

blah blah;

}

The input fields had the names "sign" and "preview" and the form action was just "$PHP_SELF".

Once I removed the input names and did the following, everything worked ok:

if ($action == "preview") {

blah blah;

}

and

if ($action == "sign") {

blah blah;

}

with the form attribute being "$PHP_SELF?action=preview" or "$PHP_SELF?action=sign"

COOL!

Thanks again!

:)

hakre

10:43 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dreamcatcher,

you can even use the

<input type="hidden" name="action" value="[i]sign/preview[/i]" />
html input tag, instead of the action="" form parameter (if you like). ;)

dreamcatcher

10:57 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers Hakre.

I love this forum, you are learning new things everyday!

:)