jas4 Posted August 22, 2007 Share Posted August 22, 2007 i'm trying to validate a form and am looking to send the user back so they can retry their form submission if there are any errors, I'm collecting errors like this: if (empty($mobileTel) || !(int)$mobileTel || strlen($mobileTel < 5)) { $errors[] ='Please enter a valid mobile number'; } and displaying the errors like this: if(isset($errors)) { if(is_array($errors)) { echo '<p class="error"><b>The following errors occured:</b></p>'; while (list($key,$value) = each($errors)) { echo '<span class="error">-'.$value.'</span><br />'; }echo"<br />"; } } else { ideally I want a script that will check to see isset($errors) and if they are then to display them. Is there a way I can send $errors back to where they came from i.e. header ('Location: form.php), but if I do this then surely there is no way of knowing the errors that have been sent. hope this is clear enough and any feedback welcome Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/ Share on other sites More sharing options...
Psycho Posted August 22, 2007 Share Posted August 22, 2007 I have always thought the best way to do validation is to have a single page that has the form, validation and procesing included (albeit in included files). Then you just have some conditional statemetns to decide which code you want to run. Main page <?php //determine if page was posted if (isset($_POST{'submit')) { $error = false; //Include the validation script here //Set $error to an error message text if any validation fails } if ($error === false) { //Form was posted and passed validation //Include processing script here //redirect to confirmation or next apge header ("Location: confirmationpage.php"); } //Include form here using $_POST values in the fields ?> <input type="text" name="username" value="<?php echo $_POST['username']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331027 Share on other sites More sharing options...
zq29 Posted August 22, 2007 Share Posted August 22, 2007 The easiest way would be to do your validation on the same script as the input, post the form back to itself and check for the existance of vars containing errors. EDIT: beaten to it. Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331032 Share on other sites More sharing options...
lemmin Posted August 22, 2007 Share Posted August 22, 2007 There isn't really a way to do this that isn't complicated. I agree with mjdamato (my session timed out while posting and you beat me, but I'm posting it anyway!): What I would recommend is use the same php file to print out the form as you do to check it for errors. Before printing out the form, check if the php file received form information, if it has, check it for errors, if there errors, print the errors and the form, if no errors, success! To do it your way you might need to serialize the array as into a session or post a huge string. You CAN get the $_GET array to accept an array from the URL, but I don't think you can put more than one value into the array, you might find something about that on google. Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331034 Share on other sites More sharing options...
jas4 Posted August 23, 2007 Author Share Posted August 23, 2007 so i should rewrite the form so insead of action="validation.php" it should be <form action='<?=echo $_server['PHP_self']?>' method='post'> ? Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331707 Share on other sites More sharing options...
Fjerpje Posted August 23, 2007 Share Posted August 23, 2007 exactly Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331709 Share on other sites More sharing options...
zq29 Posted August 23, 2007 Share Posted August 23, 2007 Or, just leave the action empty, it will post back to itself by default. Quote Link to comment https://forums.phpfreaks.com/topic/66180-sending-data-not-with-post-or-get/#findComment-331866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.