Jump to content

Validating Error


jamesnkk

Recommended Posts

Hi, I have an entry form, after user click the submit button, it goes to the 2nd form to validate and query the table if data is exist or not. The problem arise here - when the query result show that the data is exist in the table, I use the header to return to the form, but when return, the form entry is all blank, I doesn't want to show blank and also like to display an error message to indciate an eror was detected. How can I achieve in doing that ?

Please help me.

Thanks in advance to all expert out there.
Link to comment
Share on other sites

ah, the old PostBack problem.
you could use .NET and its forms that runat="server" but this is a php forum so thats no good.

that leaves two options.

1) use AJAX. On submit, use javascript's XMLHTTPRequest to submit the form to your submit page and return true or false if the data validates. if it does, then great - everything worked and you can forward the user to the confirmation page. If it returns false, prompt with the error.

2) don't use a seperate page for of PHP to process your form. Instead, do all the PHP Processing at the top of your page that has the form. When the user hits submit, the data is submitted back to the same page and is parsed by the PHP script at the top. If everything validates, then use your header() to forward to the confirmation page. If not, then echo the error - as the page reloads, the HTML part of the page (with your form) will also display again. Since the page being displayed contains all the $_POST data that was sent, simply set the value="" of each form field to echo the posted data. (ie: name="field1" value="<? echo $field1 ?>")
Link to comment
Share on other sites

[!--quoteo(post=357075:date=Mar 22 2006, 04:26 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 22 2006, 04:26 AM) [snapback]357075[/snapback][/div][div class=\'quotemain\'][!--quotec--]
ah, the old PostBack problem.
you could use .NET and its forms that runat="server" but this is a php forum so thats no good.

that leaves two options.

1) use AJAX. On submit, use javascript's XMLHTTPRequest to submit the form to your submit page and return true or false if the data validates. if it does, then great - everything worked and you can forward the user to the confirmation page. If it returns false, prompt with the error.

2) don't use a seperate page for of PHP to process your form. Instead, do all the PHP Processing at the top of your page that has the form. When the user hits submit, the data is submitted back to the same page and is parsed by the PHP script at the top. If everything validates, then use your header() to forward to the confirmation page. If not, then echo the error - as the page reloads, the HTML part of the page (with your form) will also display again. Since the page being displayed contains all the $_POST data that was sent, simply set the value="" of each form field to echo the posted data. (ie: name="field1" value="<? echo $field1 ?>")
[/quote]

Hi thanks for your advise, are you saying if I were to use asp.net it could be much more easy ?
Thanks anyway
Link to comment
Share on other sites

Six of one half a dozen of the other. Personally I don't like microsoft or their failure to adhere to widely accepted standards, so I stay out of ASP whenever I can help it, but each language has its own ups and downs. As for your delima, you never passed the information back to the page. You simply told it to go to page just the same as the first time you went to the page the form was blank because there was no data being submitted. You can still use the same format you have now and have everything you want, but you will need to setup a way to pass variables back to the page (URL or by some type of cookie possibly). Once you get them back to the page you will need to make them the default value of their respected field.

[code]<?php header("Location: form.php?field1=".$_POST['field1']."&field2=".$_POST['field2']); ?>[/code]
[code]<input type="text" value="<?php echo $_GET['field1']; ?>" id="field1" name="field1" />[/code]

This is just one of several options. I hope this gives you some ideas and points you in the right direction. Micah, is once again right though. His suggestions are excellent and will work adequately. His last suggestion about using a single file for both form and processing script would be my natural first choice. I only point out this other method for your benefit.

EDIT: I almost forgot...for errors you could pass error via the url back to the form page and then of the form page if $error != "" echo $error...
Link to comment
Share on other sites

[!--quoteo(post=357245:date=Mar 22 2006, 07:17 PM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 22 2006, 07:17 PM) [snapback]357245[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Six of one half a dozen of the other. Personally I don't like microsoft or their failure to adhere to widely accepted standards, so I stay out of ASP whenever I can help it, but each language has its own ups and downs. As for your delima, you never passed the information back to the page. You simply told it to go to page just the same as the first time you went to the page the form was blank because there was no data being submitted. You can still use the same format you have now and have everything you want, but you will need to setup a way to pass variables back to the page (URL or by some type of cookie possibly). Once you get them back to the page you will need to make them the default value of their respected field.

[code]<?php header("Location: form.php?field1=".$_POST['field1']."&field2=".$_POST['field2']); ?>[/code]
[code]<input type="text" value="<?php echo $_GET['field1']; ?>" id="field1" name="field1" />[/code]

This is just one of several options. I hope this gives you some ideas and points you in the right direction. Micah, is once again right though. His suggestions are excellent and will work adequately. His last suggestion about using a single file for both form and processing script would be my natural first choice. I only point out this other method for your benefit.

EDIT: I almost forgot...for errors you could pass error via the url back to the form page and then of the form page if $error != "" echo $error...
[/quote]

Thanks so much for your help and the sample code your provided is very important to me, as a newbies myself, I can understand more by looking at the actual code you provided.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.