simpli Posted April 4, 2009 Share Posted April 4, 2009 Hi, I have a complex issue and I have no idea how I can resolve it. Here goes. I have a form with inputs. I validate the inputs. If they are ok, I redirect to success page. All is fine. If it's not successful however (since it's a self calling form), I get back to the page and there I want to output an error message right? Well that's exactly where my problem is. Since the page has called itself back, the HTML is already in place. How am i supposed to get my error msg on the page since the page is already built? In my PHP all I do is create a text field, calling a css class for positioning but obviously since I am not withing the html tag, the positionning is all screwed up. What does this mean? Does this mean that upon validation I have to generate the whole HTML output with my error message in the misdt of it? Or is there a better way? If so can you tell me how? Thanks for the help, JR Quote Link to comment https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/ Share on other sites More sharing options...
Ashoar Posted April 4, 2009 Share Posted April 4, 2009 You could always use the PRINT function. Example: //IF statement goes here print "Type your error message in here"; Quote Link to comment https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/#findComment-801353 Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 The logic flow of your script should be as follows..... * User accesses php script * Script determines that no data has been submitted via the form yet and displays the form * User sees blank HTML form in browser, fills it out, and submits it * User accesses php script again * Script determines that data has been submitted this time, and processes it accordingly. If successful, do something with the data and then redirect the user to success page. If failed, build array of errors to display and display the form again (without redirecting the user) * User sees form again, but this time it has error messages Quote Link to comment https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/#findComment-801354 Share on other sites More sharing options...
simpli Posted April 4, 2009 Author Share Posted April 4, 2009 So what you are saying is that in the second pass, if there are mistakes, I should rebuild the form completely except there will be an added field which will consist of my error messages is that correct? Is there any other better way? J-R Quote Link to comment https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/#findComment-801388 Share on other sites More sharing options...
charleshill Posted April 4, 2009 Share Posted April 4, 2009 All you have to do is change action="THIS_SCRIPT" to the URL for the script you put this code in: $errors = array(); $processed = array(); // form was submitted if (isset($_POST['name'])) { if (empty($_POST['name'])) $errors['name'] = 'Name cannot be blank.'; if (empty($_POST['msg'])) $errors['msg'] = 'Message cannot be blank.'; if (empty($errors)) { // no errors occurred... do stuff you'd do upon success of form submission } } echo ' <form action="THIS_SCRIPT" method="post"> <table width="100%">'; // there were errors... if (!empty($errors)) echo ' <tr> <td align="center">The following error(s) occurred:<br />', implode('<br />', $errors), '</td> </tr>'; echo ' <tr> <td', isset($errors['name']) ? ' style="color:red;"' : '', '>Name</td> <td><input type="text" name="name" value="', isset($_POST['name']) ? $_POST['name'] : '', '"', isset($errors['name']) ? ' style="border:1px solid red;"' : '', ' /></td> </tr> <tr> <td', isset($errors['msg']) ? ' style="color:red;"' : '', '>Message</td> <td><input type="text" name="msg" value="', isset($_POST['msg']) ? $_POST['msg'] : '', '"', isset($errors['msg']) ? ' style="border:1px solid red;"' : '', ' /></td> </tr> </table> </form>'; Quote Link to comment https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/#findComment-801392 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.