AlexanderBlade Posted January 20, 2015 Share Posted January 20, 2015 Hi, I'm trying to submit a form to itself and then have go on to another page. I've tried a few things but the form only returns to itself. How can I redirect the form after posting to itself. My HTML code is between <?php ?> tags. <head> <title>untitled</title> <script src="respond.min.js"></script> </head> <body> <form id= "formId" action="goToUrl.com" method ="post"> Name: <input type = "text" name ="firstName" value=""/> <input id="formButton" onClick="self.location='www.msn.com'" type="submit" name="submit" value="Submit" /> <br/> </form> </div> </body> </html> also tried puting this script just after the closing form tag: <script type="text/javascript"> $('#formButton').click(function(){ location="www.google.com" }); </script> In both cases the form seems to ignore the script. Thanks for any help with this! Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 20, 2015 Share Posted January 20, 2015 To redirect with javascript, it's "window.location = the_url", not "location" by itself. However, it looks like you really should be using php to redirect...on the page that receives the form data (once it's saved in the db) Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 21, 2015 Share Posted January 21, 2015 I know of two approaches, and there are certainly more. Post the data to the server using a traditional form. If the data validates, save the data in the db and use http://php.net/manual/en/function.header.php with location to send to the next page. If it doesn't validate, redisplay the page (potentially redirecting as well). Post the data using Ajax. If the data validates, save the data in the db, echo success status (1), and use JavaScript to redirect to the next page. If it doesn't validate, echo no success status (0), display the error, and don't change the page. Quote Link to comment Share on other sites More sharing options...
AlexanderBlade Posted January 21, 2015 Author Share Posted January 21, 2015 So I took CroNiX's advice and put this php redirect function at top of the page. <?php function redirect_to($new_location){ header("Location: " . $new_location); exit; } $page_ok=$_POST['firstName']; if($page_ok=="userFirstName"){ redirect_to("http://www.google.com"); }else{ redirect_to("http://www.msn.com"); } ?> My page is designed to post to itself but after saving the data to the database, assuming it passes validation, I want to send the user to another page. With the php code at the top of the page, as soon as I type the page in the browser's address field it redirects to the second url: redirect_to("http://www.msn.com"); How can I make the function fire only after the button click? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 21, 2015 Share Posted January 21, 2015 Your script has no choice but to redirect to google or msn. Get rid of the else statement and second redirect to msn, and just display your form. Also, please use the code tags <> around your script. Lastly, try to make your script look "pretty". Not only is it easier for others to read, doing so will prevent you from making simple mistakes. While I never used them, a quick Google search of "online php beautifier" produced many results such as http://beta.phpformatter.com/ and http://phpbeautifier.com/. <?php function redirect_to($new_location) { header("Location: " . $new_location); exit; } $page_ok = $_POST['firstName']; if ($page_ok == "userFirstName") { redirect_to("http://www.google.com"); } /* else { redirect_to("http://www.msn.com"); } */ ?> Quote Link to comment 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.