Is there anyway to redirect the user after posting a HTML form?
I have a form which looks like this
<form action="/URL/" method="post">
<input type="radio" name="answer" value="W" />W<br />
<input type="radio" name="answer" value="X" />X<br />
<input type="radio" name="answer" value="Y" />Y<br />
<input type="radio" name="answer" value="Z" />Z<br />
<input type="submit" />
</form>
Is there anyway I can redirect the user to a new page after they click the submit button? I found a bit of javascript however that seemed to stop the post from working :S
Cheers!
If you're using PHP, do it on the server:
if( isset( $_POST['answer'])) {
header( 'Location: http://www.example.com/newurl');
exit();
}
Note that you should be using a full URL in the header() call, and that you likely want to exit() after calling header() in this case.
You can use a simple php header('yourlocation.php'); to move them away from the form.
The PHP that your form action pointed to will still continue executing even if your header() is at the top of the php file.
The header redirect will take care of the user hitting refresh - it has already moved them past the point of sending form data, so even if they refresh, it will just open the page again sans any form submissions.
An example is as fololows:
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
// Process Form Data, insert to database, whatever you want... then
header('Location: http://www.example.com/');
?>
The user then ends up at wwww.example.com and they can spam refresh as much as they like, but all the processing has already taken place. It won't resend the information (or process their payment again or anything else).
Yes, use a redirect header:
header("Location: http://new.location.com/w00t");
This must be called before any output is sent.
Take note that everything after the header call would still be executed! So you'll need die() or exit() afterwards to prevent unexpected results.
You can just simply specify URL to which form will go after being submitted.
<form action="http://localhost/redirected.php" method="post">
You can check with PHP whether form is submitted and then redirect:
if (isset($_POST['answer']))
header('Location: redirect.php');
And You can achieve this with javascript/jquery.
$('#submit_button').click(function () {
// do everything with variables
window.location = 'redirect.php';
});
Yep, just post the data using ajax and then use window.location = 'http://xxxx.com' to redirect user to location of your choice.
That's assuming you want to redirect user without using server-side location headers (like the other ones posted).
Related
I have this problem where when I refresh, a notification box will pop up in the browser(Chrome) just like this.
my code for the sign in is here:
<form class="uk-form">
<fieldset data-uk-margin>
<legend>Sign up here:</legend>
<input type="text" placeholder="username" required>
<input type="password" placeholder="password" required>
<a href="" class="uk-form-password-toggle" data-uk-form-password></a>
<select>
<option>PYP</option>
<option>MYP</option>
<option>DP</option>
<option>Adults/Teachers</option>
</select>
<div><input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
<label for="remember-me">Remember me</label>
</div>
<button type="confirm" class="uk-button uk-button-primary">Sign Up</button>
</fieldset>
</form>
and the result is this:
This is occurring because you previously POSTed to that page, meaning that you've most likely come from a form. If you refresh the page it means the POST data will be posted again to that page which is probably not what you want to do.
My advice would be once you've done what you need to do with the form, is to redirect it to another page (could in theory be back to the form page) and repopulate the data if you need to keep it.
After processing the $_POST data on the previous request you can redirect the user, and clear the Post data by doing this:
header('Location: '.$url);
exit;
Where $url is set to the URL you want to send them to. There are 2 very important things if you do this.
You cannot output anything before calling header, not even 1 single space.
Coincidentally this is why you will see some PHP files that do not include a ?> closing tag. The closing tag is optional if the file only contains PHP, and even a space after the end tag can mess up redirect if it was included before calling them. So by omitting the end tags, there can be no output.
Conciser this:
<?php
...code..
?>
\n
Where \n is a new line. If this is included before doing a header or even a download it can have undesirable effects. Personally I never mix PHP and HTML and so I never use the end tags.
Likewise this can prevent the redirect from working
\n
<?php
... code ...
You call exit immediately following the redirect. After doing the redirect PHP will continue to execute the current script, which is probably not what you want and can have unpredictable results.
It's happend because you come from another form that send POST. When you refresh, it will confirm you that data have been posted before will posted again.
I advice you redirect to another page or same page. Read header in php to redirect
This is my admin panel code:
<form action="connectdb.php" method="post">
<input type="text" name="name">
<input type="submit">
</form>
So, It so, the code in connectdb.php will only run, if the "submit" button redirects a user to it. It will not run, if a user directly open /connectdb.php page.
Do I need to start some session, something like that?
Note: I am a newbie, so please explain in detail.
Since your form is using method="post"you can place the following code at the very beginning of your connectdb.php file:
<?php
if (empty($_POST)){
exit;
}
//The rest of your code goes here
This checks to see if the $_POST variable either does not exist or does exist but is empty. If this returns true that means your form was not submitted and a user went to the page directly. The script will then exit and a blank screen will be displayed.
Instead of displaying a blank screen, you may instead want to redirect to a different page such as this:
<?php
if (empty($_POST)){
header("Location: index.html");
exit;
}
//The rest of your code goes here
Whenever you do a redirect like this, it is important to place an exit; statement directly after it, otherwise your script could still process some of the other statements and send data to the browser that shouldn't be sent. This of course could be a security risk in some cases. An exit statement prevents this kind of security risk.
Not sure if you really need it, but you can add a name attribute like the following:
<input name="submit_button" type="submit">
So when you click this button a $_POST['submit_button'] variable will be created on the PHP side, and then you can use it to check if the button was clicked:
if(isset($_POST['submit_button'])){
// your code
}
<input type="submit" name="submit_btn">
Now in your connectdb.php check,
<?php
if(isset($_POST['submit_btn']))
{
//do your code
}
else
{
//redirect to your home page
}
?>
<body>
<?php
if(isset($_POST['vendor_add_submit'])){
//INSERT INTO DB
unset( $_POST['vendor_add_submit'] );
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<label>Email</label>
<input type="text" name="vendor_email" value="" />
<input type="submit" name="vendor_add_submit" value="SAVE" />
</form>
</body>
unset( $_POST['vendor_add_submit'] ); is used to prevent more than one time insertion into db on page refresh. I tested with print_r($_POST['vendor_add_submit'] ) before and after the unset and found that the unset() function does not work.
How can I achieve the purpose of the unset function, plz?
Unset isn't going to stop the refresh from being able to replay the POSTed data to the script. The unset function eliminated it for the remaining execution of that script, but a refresh is a fresh execution.
You could simply re-direct the browser to the entry pageafter doing your insert, that way a subsequent refresh will be safe.
//INSERT INTO DB
//...
header('Location: samepage.php');
exit();
I'm sure there are other ways to accomplish this as well.
Your approach cannot work, you would just be editing the data that the PHP script has recieved. If the user refreshes the browser then it will submit the same data again, and PHP will populate a fresh new $_POST with the data the browser sent.
If you want to stop a refresh resubmitting the data, then use the POST-REDIRECT-GET pattern.
The $_POST value will only be removed on the Server-Side Meaning that on that one request PHP will not be able to see or use it (You removed it).
But you are rendering the Submit button on the page's HTML so every time someone visits the page and submit the form they will submit the value again. And you will remove it again for PHP on that current request. So every request would insert the data into the database.
You might want to rather look into rather checking if the record already does exists (OR if the user has one already, not sure what you want) and then redirect them away from the page using header('Location: otherpage.php');.
Hope that helps...
I've got a form on my websites homepage that contains a box for a user to enter a members name. From there the form currently submits to a PHP script which just calls Header('Location: blah); and then directs them to /search/username/.
Would it be worth it (i'm assuming so) doing that redirection in javascript so that directs them straight to /search/username? If so how would I go about redirecting with javascript, just plain old window.location = "http://www.google.com/"
Server side is always better method as it's more difficult to bypass than client side method.
Be sure to add exit function after redirection using header.
ob_clean();
header('Location: target.php');
exit();
To do it in JavaScript, you could:
<input id="username" type="text" onclick="" />
<input type="button" onclick="window.location='/search/'+document.getElementById('username').value" />
You could add some validation to the event as well.
Would it be worth it
That depends on your specific situation. If your server is getting 100's of these requests a second, then sure. Otherwise, it really doesn't matter which way you do it.
I am using php, js, flash and mysql on 1 website.
I want to do a URL masking using frameset(or maybe iframe). Scenario:
An user click on a link, which direct him/her to my page with this url:
www.domain.com/index.php?var1=string1&var2=string2
How to mask the url so that visitor can only see www.domain.com/index.php, but actually there are some variables over there. I need the variables, but i dont want the visitors to see. How to do URL masking on this? (I dont expect to get any code, I just want to know the logic of the url masking method)
PS. I probably would not use mod_rewrite, because I dont know how to use/write the code. So please, answer with iframe/frameset methods :)
EDIT: I think I misunderstood your question, so here is another attempt:
In www.yourdomain.com/index.php:
<?php
session_start();
if (isset($_REQUEST['flashvar']) && ! isset($_SESSION['flashvar'])) {
// Store any parameters received
$_SESSION['flashvar'] = $_REQUEST['flashvar'];
// Redirecting without query parameters
header('Location: /index.php');
exit;
}
?>
<HTML>
<HEAD></HEAD>
<BODY>
<?php
echo '<embed src="player.swf?flashvar=',
urlencode($_SESSION['flashvar']), '"/>';
?>
</BODY>
</HTML>
This example will start a session and redirect the user to itself without needing to store any parameters in the query string. Naturally, it will only work if the user has cookies enabled.
Can you submit that parameters as POST data?
For example:
<form name="form1" action="index.php" method="POST">
<input type="hidden" name="var1" value="value1" />
<input type="hidden" name="var2" value="value2" />
</form>
Click me
When user clicks on the link, the form will be submitted to index.php with POST parameters var1 and var2. User will never see this parameters in their URL (still possible to see with various tools though).