drayarms Posted May 14, 2011 Share Posted May 14, 2011 Im trying to create a page that collects personal info from users and registers them. I have a page that contains the registration form which i will call page 1. A second page which I will call page 2, processes the form. A simplified version of page 2 looks like this <?php if (isset($_POST['submitted'])) { $errors = array(); // Connect to the database. require_once ('config.php'); //Check for errors. //Check to ensure that the password is long enough and is of the right format. if (eregi ("^[[:alnum:]]{8,16}$" , $_POST['password'])) { $b = TRUE; } else { $b = FALSE; $errors[] = 'Please enter a password that consists only of letters and numbers between 8 and 16 characters long.'; } //Check to make sure the password matches the confirmed password. if ($_POST['password'] == $_POST['password2']) { $c = TRUE; $password = $_POST['password']; //Encrypt the password. } else { $c = FALSE; $errors[] = 'The password you entered did not match the confirmed password.'; } //Check to make sure they entered their first name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['firstname'])) { $d = TRUE; } else { $d = FALSE; $errors[] = 'Please enter a valid first name.'; } //Check to make sure they entered their last name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['lastname'])) { $e = TRUE; } else { $e = FALSE; $errors[] = 'Please enter a valid last name.'; } //Insert data into database. if (empty($errors)) { //query the database. for the sake of simplicity, I wont include the code // Show thank you message echo "<h3>Thank You!</h3> You have been registered"; } else { echo '<font color="red">You could not be registered, please contact us about the problem and we will fix it as soon as we can.</font>'; } //Display error messages. } else {// if errors array is not empty header("Location: page1.php?message=$errors");//Trying to pass the errors array into the url for page1 } } ?> So the idea is to create an array that contains all the errors. Upon querying the database, if the user completed the fields as required, a thank you message is printed. If not, he is redirected to the page containing the form (page 1) and the array containing the errors is transferred to page 1. Now I will include the relevant section of page 1 whihc is supposed to print the error messages. <?php if(isset($_GET['message']) { echo '<h3>Error!</h3> The following error(s) occured:<br />'; foreach ($errors as $msg) { echo "$msg<br/>\n"; } } ?> I get the following error message when I submit the registration forms with deliberate errors: Error! The following error(s) occured: PHP Error Message Warning: Invalid argument supplied for foreach() in /home/a4993450/public_html/register_page_content.php on line 66 Free Web Hosting Can anyone point out why this error occurs? Quote Link to comment https://forums.phpfreaks.com/topic/236428-need-help-transferring-error-messages-to-another-page/ Share on other sites More sharing options...
fugix Posted May 14, 2011 Share Posted May 14, 2011 looks to me like you are setting $errors[] equal to a string. Which is why your foreach() loop is triggering an error Quote Link to comment https://forums.phpfreaks.com/topic/236428-need-help-transferring-error-messages-to-another-page/#findComment-1215543 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 The problem is that you're trying to put an Array into the url, which you can't do.. because the URL is string based. If you really want to pass the errors through the URL like that you'll have to - serialize the array - base64_encode it Then on the other page just - base64_decode it - and unserialize it Quote Link to comment https://forums.phpfreaks.com/topic/236428-need-help-transferring-error-messages-to-another-page/#findComment-1215556 Share on other sites More sharing options...
anupamsaha Posted May 15, 2011 Share Posted May 15, 2011 Or, 1. take the errors array into a session 2. show the errors in the target page from the session 3. unset the session variable in the target page after the errors are displayed Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/236428-need-help-transferring-error-messages-to-another-page/#findComment-1215598 Share on other sites More sharing options...
drayarms Posted May 15, 2011 Author Share Posted May 15, 2011 @ zanus. well i tried your suggestion. I imploded the errors array on page 2 and url encoded it before passing it into the url like so: } else {// if errors array is not empty //Conver the errors array into a string $errors_string = implode(" ", $errors); //Encode the imploded string. $message = urlencode($errors_string); header("Location: register_page.php?message=$message"); } } Then on page 1, I decoded the url and reconverted the string into my original errors arryay. <div id="error"> <?php if(isset($_GET['message'])) { //Decode the url $errors_string = urldecode($message); //Explode the decoded errors string back to an array. $errors = explode(" ", $errors_string); echo '<h3>Error!</h3> The following error(s) occured:<br />'; foreach ($errors as $msg) { echo "$msg<br/>\n"; } } ?> </div> <!--closes error--> Well no error messages got displayed this time, but the register errors didn't get printed either. What am I not doing right this time? Quote Link to comment https://forums.phpfreaks.com/topic/236428-need-help-transferring-error-messages-to-another-page/#findComment-1215634 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.