Jump to content


Photo

Validating Error


  • Please log in to reply
4 replies to this topic

#1 jamesnkk

jamesnkk
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 21 March 2006 - 07:51 PM

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.


#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 21 March 2006 - 08:26 PM

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 ?>")
"Confidence in the face of risk."

#3 jamesnkk

jamesnkk
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 21 March 2006 - 10:57 PM

[!--quoteo(post=357075:date=Mar 22 2006, 04:26 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 22 2006, 04:26 AM) View Post[/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

#4 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 22 March 2006 - 11:17 AM

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.

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

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...

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#5 jamesnkk

jamesnkk
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 22 March 2006 - 12:16 PM

[!--quoteo(post=357245:date=Mar 22 2006, 07:17 PM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 22 2006, 07:17 PM) View Post[/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.

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

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users