Jump to content

Recommended Posts

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!

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)

I know of two approaches, and there are certainly more.

 

  1. 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).
  2. 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.
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?

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");   
}
*/
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.