I've finally got the first page of my registration page working. The user has to select one of three options before continuing to the next page. The problem I am having now is that the first page is not sending the data to the next page. Here is the code for
Registration_1.php:
$reg_type = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["Reg_type"])) {
//$reg_type=$_POST["Reg_type"];
//header('Location: Registration_2.php?rtype='.$reg_type);
$reg_type=$_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
?>
<form name="frmtype" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
if(isset($_POST["Submit"]) && !isset($_POST["Reg_type"]))
echo "Please select an option";
?>
</form>
Registration_2.php
<?php
$regtype=$_POST["Reg_type"];
echo "regtype value is:" . $regtype;
if($regtype==1) {
?>
However regtype is blank, meaning i'm not getting any data from the previous page. Can anyone tell me what the problem is?
session_start();
$reg_type=$_POST["Reg_type"];
$_SESSION['cust_type'] = $reg_type;
and in any page,
session_start();
echo $_SESSION['cust_type'];
for more informations,
http://matthom.com/archive/2005/02/19/php-passing-variables-across-pages
http://www.plus2net.com/php_tutorial/variables.php
PHP Pass variable to next page
http://mrarrowhead.com/index.php?page=php_passing_variables.php
http://php.net/manual/en/reserved.variables.session.php
This is because you are doing a redirect, so the post data no longer exists.
You have a few options.
Instead of doing a redirect, you could do an include.
You could store the data (session, database, etc)
You could append the data to the redirect
header('Location: Registration_2.php?Reg_type=' . $_POST['Reg_type');
then use $_GET on the Registration_2 instead of post.
You're posting your form to page1 and then redirecting to page2. Page2 doesn't have access to the posted data due to the redirect (the post is not carried along).
What you should do is process the data in page1 and store it before redirecting (eg, in a session, or use a query string like you've commented out).
Another note, when you call for a redirect using header, make sure you also exit or die immediately afterwards like the php documentation mentions (since you cannot guarantee the page will stop processing there).
First of all, when you use header to redirect, the POST variables are lost. You need to pass the variables with GET in order to retrieve them on Registration_2.php.
Registration_1.php
//...
header('Location: Registration_2.php?Reg_type=' . $_POST["Reg_type"]);
//...
and Registration_2.php:
$regtype=$_GET["Reg_type"];
echo "regtype value is:" . $regtype; if($regtype==1) {
Related
I created a form in php with action as the same page url and method as POST
<?php
if(isset($_POST['submitted']) && $_POST['submitted'] != ''){
echo $_POST['fname'];
echo "<br />";
echo $_POST['lname'];
echo "<br />";
echo $_POST['submitted'];
}
?>
<h1>testing...</h1>
<form action="test.php" method="POST">
First Name: <input type="text" name="fname" />
Last Name: <input type="text" name="lname" />
<input type="submit" name="submitted" />
</form>
When I enter data and submit it, page shows the data as I printed the data. But when I refresh page the data still exists, I want the data to be shown only when I click submit.
Can anybody help me in this?
Am I doing something wrong.
AFAIK. This is a standard browser behavior to repeat last request with f5 button.
My understanding for what do you need to prevent it. It's to prevent 'spam'.
So, you need to understand that first submit was already made.
As test idea you can add timestamp for send button and check it on send. In this case timestamp from button and on send will be different for refreshing
I figured out that
0) {
$_SESSION['shout'] = $_POST['shout'];
header("HTTP/1.1 303 See Other");
header("Location: http://$_SERVER[HTTP_HOST]/echochamber.php");
die();
}
else if (isset($_SESSION['shout'])){
$echoedShout = $_SESSION['shout'];
/*
Put database-affecting code here.
*/
session_unset();
session_destroy();
}
?>
OR I did that in codeigniter using set_flashdata which stores session data for next request only.
My website involves a user submitting data over several pages of forms. I can pass data submitted on one page straight to the next page, but how do I go about sending it to pages after that? Here's a very simplified version of what I'm doing.
Page 1:
<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>
Page 2:
<?php
$name=$_POST["Name"];
echo "Hello $name!<br/>
<form action='page3.php' method='post'>
Please enter your request: <input type='text' name='Req'/>
<input type='submit' value='Submit'/></form>";
?>
Page 3:
<?php
echo "Thank you for your request, $name!";
?>
The final page is supposed to display the user's name, but obviously it won't work because I haven't passed that variable to the page. I can't have all data submitted on the same page for complicated reasons so I need to have everything split up. So how can I get this variable and others to carry over?
Use sessions:
session_start(); on every page
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
You could store the data in a cookie on the user's client, which is abstracted into the concept of a session. See PHP session management.
if you don't want cookies or sessions:
use a hidden input field in second page and initialize the variable by posting it like:
page2----
$name=$_POST['name']; /// from page one
<form method="post" action="page3.php">
<input type="text" name="req">
<input type="hidden" name="holdname" value="<? echo "$name"?>">
////////you can start by making the field visible and see if it holds the value
</form>
page3----
$name=$_POST['holdname']; ////post the form in page 2
$req=$_POST['req']; ///// and the other field
echo "$name, Your name was successfully passed through 3 pages";
As mentioned by others, saving the data in SESSION is probably your best bet.
Alternatly you could add the data to a hidden field, to post it along:
page2:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
echo "hello $_POST['username'};
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
Remember, if you aren't using prepared statements, make sure you escape all user input...
Use SESSION variable or hidden input field
This is my workaround of this problem: instead of manually typing in hidden input fields, I just go foreach over $_POST:
foreach ($_POST as $key => $value) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
Hope this helps those with lots of fields in $_POST :)
So I need to store the choice of selected radio button in session and then based on that value perform an action on a different page.
Page1.php:
<input type="radio" name="person" value="p1"/>Person1
<input type="radio" name="person" value="p2"/>Person2
Page2.php
if Person1 is selected on page one
//do this
if Person2 is selected one page two
//do this
First, you will have to start a session in Page2.php
start by calling (at the top of your page)
<?php
session_start();
//post your data
$person = $_POST['person'];
$_SESSION['person'] = $person;
?>
You will have to have the inputs be wrapped inside of a form:
<form method="post" action="Page2.php">
<input type="radio" name="person" value="p1"/>Person1
<input type="radio" name="person" value="p2"/>Person2
<input type="submit" value="submit"/>
</form>
Now you will be able to use that $_SESSION variable on mostly any page, if the session is not destroyed or overwritten.
To retrieve a session value on another page, simply use:
<?php
session_start();
$person = $_SESSION['person'];
?>
You can do this by posting the data in your form through to your Page2.php page you don't need a session.
$_POST["person"];
Will pull the data out so that you can do:
string person = $_POST["person"]
if(person == something){
//do something
}
else{
//do something else
}
You only need a session if you intend to use the variable value that is returned on multiple occasions. If that's the case then you can find a good easy tutorial here:
http://www.w3schools.com/php/php_sessions.asp
The basics would look something like this within your Page2.php:
session_start();
$_SESSION["person"] = $_POST["person"];
Hope that helps!
my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>
I'm attempting to create a link for users to click that will remove them from a list. I'm trying to figure out how to do this without using a submit button and without using $_GET(if possible).
Anyway, I'm afraid to do it with $_GET (the way I have it now), because the user can type this in the URL (even though 99% wouldn't know how or think to do this) and they would be removed from the list.
How can I name the link so I can use $_POST?
$attendingUsers = mysql_query("Select acceptedInvites from events where eventID = ".mysql_real_escape_string($_GET['eventID'])." ");
$users= mysql_fetch_array($attendingUsers);
$user = $users['acceptedInvites'];
if(preg_match("/$userid/", $user)){
echo "You are attending this event</br>";
echo 'Click here to remove yourself from the list';
if($_GET['delete']=1){
$sql=...
}
}
Is it possible to do this without using $_GET? Thanks!
Never delete via a link. Read The Spider of Doom
Best way is to link to a "delete" page with an "are you sure" form. Submitting the form (via POST) performs the delete and redirects back to a suitable results page.
For example
Click here
to remove yourself from the list
Then, in remove.php
<?php
// get Event details via $_GET['eventID']
if (isset($_POST['confirm'])) {
// delete via SQL
// redirect
header('Location: http://example.com/events.php');
exit;
}
// display event details
?>
<form method="post" action="remove.php?eventID=<?php echo $eventId ?>">
<p>Are you sure?</p>
<input type="submit" name="confirm" value="Remove me from this event">
</form>
You should probably also look into CSRF protection but that's really outside the scope of this question.
Your are required to use either $_GET or $_POST
<form action="delete.php" method="post">
<input type="hidden" name="eventId" value="yourEventId" />
<a href="#" onclick="this.form.submit();" > Delete</a>
</form>
If I have my JavaScript right, this should do the trick:
Delete
<form id="delete" action="delete.php" method="post">
...
</form>
The link will then submit the form.
You could use some kind of encoding to make the get var unreadable, like an md5 or even an encrypted string.