Redirect After Submit php [duplicate1] - php

This is my code:
<input type="submit" name="send" class="do__order" value="order" >
Now, I want to redirect the submitted to any page of my choice after the form data has been submitted, how do I do it?

Add the following line to the script:
header('Location: http://www.example.com/');

You can try something like this on page where you do your check. Let's say it's the check.php
if(isset($_POST['send'])){
header('Location: somewhere.php');
}
And you also need to add a method attribute to your form tag, in this case it would be post, so method="post" and action would be action="check.php".

Related

How to run a php code, only if the "submit" button redirect you.

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
}
?>

HTML Form Submit but Prevent While Refreshing Page Resubmiting Form

I have a simple form. I am sending the form to same page that has the form. However, after every submit process, then when I want to refresh the page manually, the browser asks me:
Do you want to re-send the form?
How can I prevent this?
foo.php
<?php echo $_POST['id']; ?>
<form action="foo.php" method="post">
<input type="text" name="id" value="">
<input type="submit" value="Send">
</form>
Thanks!
check with a condition if the form was posted, process the form and then redirect using javascript.
<?php
if (!empty($_POST["id"]( {
//do stuff
?>
<script>
location.href=("/");
</script>
<?
}
?>
Method 1: Check with a condition if the form was submited and then redirect to the same page.
if(isset($_POST['button_name_from_form'])) {
#...code
header('Location:page.php');
}
!! But make sure that header is before any output. !!
Method 2: Ajax

Redirect to a php page based on submit button clicked

I have two submit buttons in a php page. Based on the submit button clicked, I wish to redirect user to different php page. How that can be done?
<form>
<input type="submit" value="Go To Page1.php">
<input type="submit" value="Go To Page2.php">
</form>
Assuming you don't have any shared input boxes, you can just do something like this, or use simple links.
<form action="http://example.org/Page1.php">
<input type="submit" value="Go To Page1.php">
</form>
<form action="http://example.org/Page2.php">
<input type="submit" value="Go To Page2.php">
</form>
If you have additional input elements, I suggest looking into this solution. Relevant code sample:
<input type="submit" name="submit1" value="submit1" onclick="javascript: form.action='test1.php';" />
Just use a set of a tags inside the form:
<form>
<!-- Other Inputs -->
<div id="redirects">
Go to page 1
Go to page 2
</div>
</form>
If you need to send certain information along with your redirect, keep your current form and have a condition at the top of your file:
<?php
// You will need to send the $_POST data along with the redirect
if(isset($_POST['submit1']))
header('Location: page1.php');
else if(isset($_POST['submit2']))
header('Location: page2.php');
// Continue with the page...
?>
To send the $_POST vars along with the redirect, check this other SO post.
You don't need a form for this, neither input fields. Use <a> tags instead. You can style them with CSS to look like a button if you want that.

How to disable a form after the first submission?

<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" onsubmit="document.getElementById('submit-button').disabled=true;">
I use that line to disable the button after the first click, but it doesnt work..
Here is the line of the button:
<input type="submit" value="Register" id="submit-button"/>
I'd guess that what is happening is your code is firing but then page will refresh after the form has been submitted and the button will no longer be disabled.
If this is the case then the you could insert the disabled property in to the button's HTML from the server side when you know that the page is being rendered as the result of the form being submitted.
If you are posting to the same page and wish for the button to be disabled after the form has been submitted once, what you can do is use PHP to check if the data that was submitted by the form has been posted to the page. If it has, disable the button. It might look like this:
<form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" <? if (isset($_POST['your_form_data'])) echo "disabled='disabled'" ?> >
You may want to save a boolean flag in database for example isRegistered, so if the user is already registered, the form will not be shown.
Without jquery (just an example, better to use jquery):
function disablebtn( idbtn ) {
document.getElementById(idbtn).innerHTML = 'Loading...';
document.getElementById(idbtn).disabled=true;
}
<form onsubmit="disablebtn('formPageBtn')">
<button type="submit" id="formPageBtn">Send</button>
</form>
With this function when you press the button it will change the label to "loading...", too.
change the action to javascript:void%200 after the form was send!
In what way does it "not work"?
If you mean that it doesn't submit the form, the button just stays disabled, try using a setTimeout to delay the disabling slightly.
If you mean that the button is not disabling, are you sure the page isn't reloading by the form being submitted? If this is what's happening, you might want to add <?php if($_POST) echo " disabled"; ?> inside your submit button.

How to check if a form is submitted via javascript?

I have this conventional submit button which submit a form like this:
<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>
And I check if the submit button is clicked using this:
if(isset($_POST['Submit'])){
//update DB
}
Now I have a submit link using jquery:
Submit
JS code:
$("#form_submit").submit();
What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?
If I understand you correctly, try this:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// your code.........
}
You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)
If I understood your problem correctly that you can simply change input type to hidden.
<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>
$_POST['Submit'] variable will be defined.
The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.
If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.

Categories