jonnewbie12 Posted November 6, 2011 Share Posted November 6, 2011 Hi. Pretty straight forward I guess but as the name suggests am a newbie. I have a form that requires the user to enter certain parameters. If the values are blank it submits to itself and loads the error messages. What I want to do is create PHP code that submits the form to a different url. What I thought was create two forms (the second with hidden fields replicating the first form), each form having a different url in the action"" code. What I cant work out is the PHP IF ELSE code to submit form 2 if Form1 is is validated correctly. This is the PHP code relevant to the form validation. Help? <?php //If form was submitted if ($_POST['submitted']==1) { $errormsg = ""; //Initialize errors if ($_POST[width]){ $title = $_POST[width]; //If title was entered } else{ $errormsg = "Please enter width"; } if ($_POST[drop]){ $textentry = $_POST[drop]; //If comment was entered } else{ if ($errormsg){ //If there is already an error, add next error $errormsg = $errormsg . " & content"; }else{ $errormsg = "Please enter drop"; } } } if ($errormsg){ //If any errors display them echo "<div class=\"box red\">$errormsg</div>"; } //If all fields present if ($title && $textentry){ //Do something echo 'THIS IS WHERE I WANT THE CODE TO SUBMIT FORM 2 or SUBMIT FORM 1 TO A DIFFERENT URL'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/ Share on other sites More sharing options...
Gotharious Posted November 6, 2011 Share Posted November 6, 2011 If I get what you mean, that you want to head the user to another form once the first form is validated you can use header('location:form2.php'); Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285563 Share on other sites More sharing options...
Pikachu2000 Posted November 6, 2011 Share Posted November 6, 2011 What do you mean you want it to submit to a different url? If you're describing what I think you are, your best bet is to simply use a conditional for the form's action= attribute, based on whether the form validated or not, and present it to the user in the same form as an "Are You Sure?" type of thing with the fields set to read-only or disabled, and another submit button. So: 1. The user completes and submits the form. 2. Validation routine runs 3. If validation succeeds, the same form is presented, with a different action= attribute, and all fields set to disabled to prevent further edits 4 User submits form again, this time it goes to a different url. If that isn't what you're talking about, provide more details. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285566 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Thank you Pickachu thats exactly what I mean. If the form validation is successful I want to the page to submit the form to a new URL. If the validation is unsuccessful I want it to submit to itself and display the error message. Can you include PHP in the "action" function of the form so it says only submit if validation is successful? HOw would you do that for example? p.s. I will try the 'header' idea now too Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285570 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Header doesnt work. Error code says I cannot modify header information Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285573 Share on other sites More sharing options...
Gotharious Posted November 6, 2011 Share Posted November 6, 2011 Mostly I've seen people use ajax to validate while the user is complete the form before hitting submit, perhaps you'd try that Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285574 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Thanks Gotharius. Don't know anything about Ajax so will try and work with PHP if all else fails will go there. Pickahu: I have tried the following script in the form action button so that if the width is 1 it submits to one url and if not it submits to another: Is that what you meant? It still doesn't seem to work when i is entered though and always submits to the 'template5.php' <form id="form1" name="form1" method="post" action=<?php if ($_POST['width']==1) echo "template3formvalidation.php"; else echo "template5.php";?>> Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285580 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 I think whats happening is that the PHP code is returning an empty value in the width box as the form has not submitted itself prior to the PHP query. On the second attempt it works as the form has now submitted the value so it directs to the right page. Is it possible then to: 1. Enter the width value 2. Submit the form to itself 3. Then run the PHP code in the action button query the value of the width field 4. If the with value is =1 resubmit and it will go to template5.php. 5. If the width value is not 1 it will resubmit to itself again. Problem is I want all this done in one click?? AHGGG Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285591 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Check out this example I made for another user. It validates the data using PHP on the same page, then uses a header() redirect to go to a success page. This could be easily changed to fit your needs. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285594 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 Header doesnt work. Error code says I cannot modify header information Headers have to be set before any output to the browser. You have set your header after some output has been sent to the browser, which is why you are getting the error. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285602 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Sorry totally confused now: Have looked at AJAX and seems like a huge learning curve and only works on most recent browsers. Not sure what you mean by setting the header beforehand? Best chance of keeping this within the realsm of my limited knowlede is to try and keep to PHP. I thought this was quite a simple process but maybe I am not making myself clear enough. All I want to do is the following: 1. User inputs some info into a form field (for my purposes 'width') 2. User presses submit button on form 3. If width equals a certain value or is empty a message is displayed on same page saying value is wrong or empty 4. If width value is ok (i.e. validated), form is submitted to a different page where the value can then be displayed. The form validation by itself seems easy enough providing you don' want to return two different outcomes depending on the submitted form. Happy for anyone to take this from a fresh perspective and start from scratch but at the moment I am totally confused? Sorry to take your time up with this. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285611 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 AJAX isn't your best method anyways, as the user may disable javascript, and the form would not be validated before submission. As for the header comment. Look at this: <html> <?php header('location:form2.php'); ?> With this code, the HTML tag is sent to the browser when that line of code happens. Then the location() function is called after - this will fail. However, if you do it this way: header('location:form2.php'); <html> The header is before the HTML tag, so this is ok (assuming you have no other output before this code). In this case, the redirect will happen. Side note: AJAX works in pretty much all browsers. Even IE6 Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285615 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 Sorry I forgot to post the actual link http://www.phpfreaks.com/forums/index.php?topic=346014 header('location:form2.php'); <html> 'Location:' in the header should always contain a complete URL: http://mysite.com/form2.php Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285622 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Doesnt matter where I put the code: <?php header('location:http://www.mysite.co.uk'); ?> I just keep getting this message Warning: Cannot modify header information - headers already sent by (output started at /websites/my host/LinuxPackage22/mysite.co.uk/public_html/form2.php:73) in /websites/myhost/LinuxPackage22/mysite.co.uk/public_html/form2.php on line 73 Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285630 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 That's because you've echo'd or otherwise output HTML before you made the header call. If you see my example, you'll see all the logic is done before any output, so the header call won't fail. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285636 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 The data you are sending to the browser is being sent here: /websites/my host/LinuxPackage22/mysite.co.uk/public_html/form2.php on line 73. You will need to put your header() call before that. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285637 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 XYPH I have had a good look at the example, thank you and can happily adapt it but no joy In fact I have copied into a new clean page and tried putting the full url of my site in the PHP code. It still comes back with the same error message. Sorry Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285640 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 this is the code as copied from the example in my test page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { $err = array(); if( empty($_POST['name']) ) $err['name'] = 'Name is a required field'; elseif( !preg_match('/^[a-z]++$/i', $_POST['name']) ) $err['name'] = 'Name can only be letters'; if( empty($_POST['email']) ) $err['email'] = 'Email is a required field'; elseif( !preg_match('/@/', $_POST['email']) ) $err['email'] = 'Email must contain @ symbol'; if( empty($err) ) { //function_to_process_data(); //header( 'Location: http://yoursite.com/success.php' ); echo header ('Location: http://www.mysite/template5.php' ); exit(); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>form</title> </head> <body> <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>"> <label>Name: <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>"></label> <?php if(isset($err['name'])) echo '<span style="color: red;">'.$err['name'].'</span>' ?> <br> <label>Email: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>"></label> <?php if(isset($err['email'])) echo '<span style="color: red;">'.$err['email'].'</span>' ?> <br> <input type="submit"><input type="reset"> </form> </body> </html> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285641 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 You need to re-read the few posts above yours and realize what you're doing. If you can't figure out why you're getting those messages, or whats inherently wrong with the above posted markup/code, then you really need to go back to the basics fo HTML markup and leave PHP alone until you understand that concept. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285647 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 You are outputting a doctype, then a bunch of HTML, then your PHP code. Your PHP code has the location() call in it. As I've mentioned a couple of times (and even gave the line number), you have to move the location call before any HTML output to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285654 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 No to mention having multiple DOCTYPE, HTML, HEAD, TITLE, BODY tags? facepalm.jpg Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285663 Share on other sites More sharing options...
jonnewbie12 Posted November 6, 2011 Author Share Posted November 6, 2011 Well thank you all for your help. As I said I am a newbie. I didn't appreciate the doc types and <head> were outputting html. You are right, back to the drawing board but at least it works. Thank you all Quote Link to comment https://forums.phpfreaks.com/topic/250563-if-form-validated-submit-form1-else-submit-form2/#findComment-1285680 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.