drayarms Posted May 24, 2011 Share Posted May 24, 2011 I'm trying to display error messages on the same page that submits a form (register_page.php). So in the script that executes the form submission, I create an array to hold all the error messages, and towards the end of the script I include a conditional at the end, which does the following if there are errors: First I convert the $errors array into a string, url encode it, then pass it into the url of the target page (register_page.php). Then on the target page, I retrieve the message through GET, url decode it, reconvert it to an array and try to print it out the contents of the array. So here is the excerpt of the conditional on the execution page: //Display error messages. } 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"); } And here is the code for the target page: </div> <!--closes register box--> <div id="error"> <?php if(isset($_GET['message'])) { //Decode the url $errors_string = urldecode($_GET['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--> Now everything works just fine, except that the error messages are printed one word per line for example Your email is not a valid email address. I can't seem to find the fluke in the code. Any ideas? Also is there any potential security risk for passing error messages in a url? No sensitive information is being transmitted. Just error messages. Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/ Share on other sites More sharing options...
Pikachu2000 Posted May 24, 2011 Share Posted May 24, 2011 It's a little unclear, does the form submit to itself? Where is the validation/processing being done? Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/#findComment-1219428 Share on other sites More sharing options...
cyberRobot Posted May 24, 2011 Share Posted May 24, 2011 Now everything works just fine, except that the error messages are printed one word per line for example Your email is not a valid email address. I can't seem to find the fluke in the code. Any ideas? Isn't it because you're exploding the error string based on the space character? <?php $errors = explode(" ", $errors_string); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/#findComment-1219537 Share on other sites More sharing options...
drayarms Posted May 24, 2011 Author Share Posted May 24, 2011 @cyberrobot. So which character do you suggest I explode the string based on? Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/#findComment-1219753 Share on other sites More sharing options...
xyph Posted May 24, 2011 Share Posted May 24, 2011 Whatever you're using as a delimiter between errors messages. Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/#findComment-1219774 Share on other sites More sharing options...
cyberRobot Posted May 25, 2011 Share Posted May 25, 2011 @cyberrobot. So which character do you suggest I explode the string based on? As xyph said, you would explode the string based on whatever's between the different error messages. Of course, if you're using the space character you should consider changing it to something else. Preferable something that's not commonly used in the error messages such as the straight bar "|". You could also use multiple characters such as ":|:". Quote Link to comment https://forums.phpfreaks.com/topic/237298-error-message-passed-through-url-not-displayed-correct/#findComment-1219981 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.