I have a form where the user will input a bunch of data. It is set up like this:
<form action="add.php" method="post">
This makes it so that when I click my submit button it will go to the add.php page. add.php however is a blank page because it only runs some SQL queries and does nothing else.
I want it so that when I click submit, the information is sent one place and the screen goes to another. My button is set up like this:
<input type="submit" value="Submit">
Just to make sure I am explaining the issue correctly, when I click submit I want the data from the form to go to add.php, and the browser to display a different page rather than displaying the add.php page, which is what it is doing right now.
The solution is easy, send data to add.php, in the end of your process. redirect to another page, let's say page.php
on your add.php
<?php
/// do you mysql stuffs here
header('Location: page.php');
?>
Here is two solutions :
Asynchronous solution
Use Javascript and ajax to send the data to add.php asynchronously and redirect the user to the page you want.
This solution might be not a good one, because the process result of add.php is ignored (if you redirect the user before getting the result)
Synchronous solution
Redirect the user to add.php, then, redirect the user to the page you want at the end of the process.
After your SQL Queries are done on add.php you can use the header function and pass your data via get-method:
header("Location: nextSite.php?key=value"); //Replace key and value with whatever you want
we can use a session or cookie in PHP.
Session:
//On page 1
$_SESSION['name'] = $formvariable; // You can use document.getElementById('form input
name')
//On page 2
$var_value = $_SESSION['name'];
Using include method
//On page 1
This page has a form without submit button
//On page 2
This page has a submit button
so we include page 1 something like this.
EX:
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
I hope this will help you to solve your issue- thanks!
Related
I have a button on a php page with some data on this same page. My button redirect to an other php page and I want to get back this data.
I tried to do
if(isset($_POST['button'])){
echo($data);
}
The isset is true (I do a test with a string) but I can't get back my datas from this other php page.
My form code of my button :
<form action = "/lienPaybox.php" method = "post">
<input type = "submit" name = "button" value = 'Submit'>
</form>
When you want to pass datas from one page to another, Session is your best bet
All you have to do is initiate session.
page1.php --> This page contains your datas. Below code in your page1.php
<?php
session_start();
$_SESSION['username'] ='Mr.x';
$_SESSION['usertype'] = 'level1';
?>
page2.php --> Page from where you want to retrieve datas of page1.php.Here is the code,
<?php
session_start();
print_r($_SESSION); //You will notice the datas from previous page is being displayed here
?>
Ofcourse there are best practices which needs to be followed while handling Sessions. Once you get a hang of sessions, you can go over this link Securing Sessions in PHP
I have an HTML form with a form action of foo.php. However, when I click the submit button, I get redirected to the "foo.php", but I don't get redirected back to the page where the form is located. Could anyone tell me if there is some sort of code that is necessary for this to happen?
Here's my PHP file, if this helps:
<?php
$title = $_POST['title'];
$content = $_POST['content'];
$postid = $_POST['postid'];
?>
You can set header redirect back to form file, after processing the form data. Assuming your form file has name "form.php"
header('location: form.php');
Two ways:
<?php
header('Location: myform.php');
?>
or use javascript:
<script>
window.location('myform.php');
It very simple. Try to understand your task first. You need to redirect to another page after completing your task whatever save/update/processing data.
just set header in your php script after completing your work.
<?php
// your all code will goes here
header("Location: myform.php");
?>
I have site with form with dropdown input. When you select some item from dropdown and submit form, you are redirected to another site with another form.
I need refresh first site if you press "Back" in your browser. Can I sent some header or something to say to browser, that site need to be reload? But only, when you submit form, no when you just visit first page.
Edit:\\ Sorry, I just realized, that form is sent to the same page, but in PHP, after saving data to DB, I use header() to redirect it to second page.
Edit2:\\
File a.php
if(!empty($_POST)){
//save data do DB
header("Location: b.php");
exit();
}?>
<form method="post" action="a.php">
...
File b.php
if(!empty($_POST)){
//save data do DB
}
//sending HTML with big form to user
When you submit form in a.php and everything is correct, you are redirected to b.php, but when you press back in b.php, you are returned to a.php and I need reload this page to update dropdown and rest of HTML
Apart from your question being worded poorly, i am assuming that you are opening a new tab in the browser for the new form. So when the form is submit you just run this.
header ( 'Location: '.$_SERVER['REQUEST_URI'] ) ;
I have a questionnaire in a form. In the end of it the submit button is pressed that is supposed to call a .php file that inserts data into a database through its action information and afterwards show the last page that contains something like "thank you for participating etc." via the onsubmit info.
problem is that the last page is shown before the .php file is shown which means it is visible only for like half a second and then the php script is carried out which ends up showing a blank page.
The php script works it inserts data into the questionnaire correctly so there should be no mistakes syntax-wise.
any ideas if I have to exit the cript or something and return to the .html file or what could be wrong?
on your opening form tag add action="submit.php"
then once it goes to that page when the submit button is hit add this to the bottom of that php page:
header("Location: successfull.html");
IT sounds like what youre doing is showing the message with Javascript via the onsubmit event - this happens before the request is even set to the server and the php script. Youd either need to do an ajax form submission and then display the message when the request completes or make the php script redirect to the success message page when it is done.
But this is all just speculation without seeing any code... you should post some :-)
Why not submit the form to process.php then process it:
if(isset($_POST)){
$name = $_POST['name'];
// etc etc
// if all error checks pass, then echo out - thanks for taking part in our survey!
}
What you're doing is submitting it, and it seems you're getting javascript to say 'thank you' but it is being submitted before this thank you message can be displayed - no harm in echoing this out on your .php page!!
Update
You mention about redirecting to a page afterwards, but this can be done by:
header("Location: where/to/go.php");
exit;
But you can't do this with the above (echoing out a success) since it will redirect straight away.
The way I deal with this is putting the html contents into the php file.
<?php
if (!isset($_POST["submit"])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>survey</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
...... (your form) ......
<input type="submit" value="submit" name="submit"><br />
</form><br />
</body>
</html>
<?
}
else {
$db = new PDO('...');
$db->exec(...);
echo "Thank you!";
}
?>
A few ways you could accomplish that.
You could make the php file it submits send out the data for the "thank you for participating" page (if you're fine with simply going to another page).
Alternatively, if you want to stay on the same page but just show the "thank you" notification, I would use JavaScript to disable the default action (e.preventDefault(); in the event handler) for the "submit" button on the forum, then also use JavaScript to use AJAX to submit the data.
An example (using JQuery), which won't change your page and perform the submit in the background, and display the "thank you" when done, on the current page.
$("a#confirmSubmit").click(function(e) {
e.preventDefault(); // Prevents the submit button from changing pages
data = {
Name: $("input#Name").attr("value")
// Add other data here also
};
$.post("/page/to/submit/to.php", data, function(d) {
//Write the code here to show the "thank you" notification.
//It will show upon completion here.
});
});
If you want to check for errors with inserting into the DB, you could check the value of the data of the AJAX call, to conditionally show the error. You can then return the user to the exact same form they were already on, with all the data still there, and show them an error message.
I need to make sure users don't go to the download page unless they fill out the form on the register page. I need help inserting a session variable into the submission script, something like
$_POST['authenticated'] = 'yes';
Then on the download page require this:
<?php
session_start();
if($_SESSION['authenticated'] !== 'yes') {
header("Location: http://domain.com/default.php");
};
?>
I need to make them successfully fill out the form - then add it to the session - then once on the page - purge the session info but don't know how
I need a push in the right direction on how to require the form to be filled out in order to get to download page.
EDIT
on purchase-registration.php (form) I added this
$authReq=$_SESSION['authReq']=rand(1,65535);
and in the form(s) on that page I added this
<input type='hidden' name='authreq' value='<?PHP=$authReq?>'>
In the processor script (form submit) I added this:
"$authReq = $_POST["authReq"];"
and on the final page, the download-software.php which Im trying to restrict access to I added this
<?php
session_start();
if($_SESSION['authReq']==$_POST['authReq']) {
header("Location: http://kinetick.com/V3/download-free.php");
};
?>
no joy, is this incorrect?
thx
The way I usually handle that is:
on the page that renders the form:
$authReq=$_SESSION['authReq']=rand(1,65535);
on the form:
<input type='hidden' name='authreq' value='<?PHP=$authReq?>'>
and on the submitted page
if($_SESSION['authReq']==$_POST['authReq'])....