I have been trying to use server side redirect for the drop down menu on my main page. I need the selected option to be remembered using cookies so that it will redirect to the last visited option itself.
This is what I have done so far, but couldn't able to figure it out where I m doing mistake.
<?php
if (isset($_POST['submitted'])){
$newcity=$_POST['city'];
#set cookies
setcookie("city",$newcity,time()+22896000);
}
if ((!isset($_COOKIE['city']) )){
$city = "";
}
else{
header("location:http://example.com/".$_COOKIE['city']."");
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
<?php
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
?>
<form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method ="POST">
<select name="city">
<option value ="toronto">Toronto</option>
<option value ="ottawa">Ottawa</option>
<option value ="kingston">Kingston</option>
</select>
<input type="submit" name="submitted" value="Submit">
</form>
Two thoughts:
Try use exit(0) after the redirect.
Use a cookie name different from your POST and GET variables. Maybe your server is configured in some way that overrides POST, GET and cookies.
Your issue is present because you can't access a cookie value on the same page that you set it. Hence why it only works when you reload the page.
You'd be best to redirect them right after the cookie setting:
if (isset($_POST['submitted'])){
$newcity = $_POST['city'];
setcookie("cookie_city", $newcity, time()+22896000);
die(header("Location: http://example.com/".$new_city));
}
if ((!isset($_COOKIE['cookie_city']))){
$city = "";
} else{
header("Location: http://example.com/".$_COOKIE['cookie_city']);
exit(0); // <<< Try this
}
That way, you'll have the cookie set and then user is redirected either way.
Do you still need that end block though? Are they always going to redirect to the city they selected?
Related
So I am trying to prevent form resubmission using sessions
and this is my code :
<?php
session_start();
if(isset($_GET['unid']))
{
if ($_GET['unid']==$_SESSION["uid"])
{
echo "Form is Submited do something";
}
else
{
echo "no you can't do that";
}
}
$unid = md5(uniqid());
$_SESSION["uid"] = $unid;
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
and it works ...but if the user opens another tab then it will break so how can I fix it ?
I'm not sure about this but may be assigning a new global session variable will work, say $_SESSION['checkSub']. So once the form is submitted, set it to 1 or true and only let form submission if it isn't 1 or false.
You can check to see if the unid is set in the session before generating a new unique id. See updated code below:
<?php
session_start();
if(isset($_GET['unid']))
{
if (isset($_SESSION['unid']) && $_GET['unid']==$_SESSION['unid'])
{
//form has been submitted with matching unid - process it as needed
unset($_SESSION['unid']);
}
else
{
// Form was resubmitted or some other logic error
}
}
$unid = ''; //declare here for good scope
if (isset($_SESSION["unid"])) {
$unid = $_SESSION["unid"];
}
else {
$unid = md5(uniqid());
$_SESSION["unid"] = $unid;
}
?>
<form method="GET">
<input name="name" value="test">
<input name="unid" value="<?php echo $unid;?>">
<input type="submit">
Rather than using form method GET, try to use POST. It will work for you. The $_POST array will only have data in it when form is submitted, so you should not have to use the session to know whether form is submitted or not.
I'm trying get a form to redirect to a certain page depending on what option is chosen in a select div. When I press submit, it redirects to the home page. I realize it's because it doesn't change $industry_choice until after the I press the button... is there a method to change this the moment you select a value?
Let's say I choose banking, and $industry_choice = "banking".
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
}
<form method="post" action="http://localhost:8888/wordpress/<?php echo $industry_choice ?>">
<select name="industry">
(lots of options here)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
You are doing it wrong. I think you are not clear with the action attibute of the form.
<?php
if(empty($_POST['industry'])) {
$industry_choice = "";
} else {
$industry_choice = $_POST['industry'];
header('Location: http://localhost:8888/wordpress/'.$industry_choice);
}
?>
<form method="post" action="">
<select name="industry">
(options)
</select>
<input type="submit" class="select-industry" value="Select">
</form>
action attribute will redirect to the page you processed the input. After that you will redirect it to targeted location.
I am trying to set a session variable for the location selected in the dropdown menu when the user hits submit. The goal is to pass this variable to another php page so that I can display the drop down option they chose along with two values associated with it from a MYSQL table.
Below is a section of my code from my first php file. It shows the dropdown that is created from a list of values from the MYSQL database along with where I am trying to set the session variable for the selected option.
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
Below is my code from the second page where I call the session variables for display. As you can see I have input another variable for testing purposes. It is supposed to display "cat" and it does.
session_start();
echo $_SESSION['animal'];
echo $_SESSION['locations'];
It may also be important to note that locationsprocessing.php runs a redirect using header. But that is really all that file does.
Am I missing something? I have gone through countless different tuts and such and been staring at this code forever. I can not see why it wo
you need start session in every pages where you need to use session:
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
session_start();
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
This may be going wrong: When you submit your form in the first piece of code from the questions, you are sending the data entered in the form to /locationsprocessing.php. You say that all that file does is redirecting to another page. That means the data entered in the form is effectively lost.
I think what you want to do is to remove the form processing code from the first piece of code, so with only this remaining:
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
Then place that removed piece of code in /locationsprocessing.php, like this:
session_start();
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
header('Location: ' . $destination);
}
$destination would be the path for the page with the second piece of code in the question.
I have created one textfield and one dropdownlist and a button in my "index.php" page as follows:
<form value="indexform" action="" method="post">
<b>Number:</b><input type="text" name="number"/><br/>
<b>Network:</b>
<select name="network">
<option selected="">please...</option>
<option value="1">Bsnl</option>
<option value="2">Idea</option>
</select>
<input type="submit" name="proceed" value="submit">
</form>
and i have used the php code as follows for the above form:
<?php
if(isset($_POST['network']) && isset($_POST['number']))
{
$number = $_POST['number'];
$network = $_POST['network'];
}
if(!empty($_POST['network']))
{
switch($network)
{
case "1":header("Location:Bsnl.php");break;
case "2":header("Location:Idea.php");break;
default:exit();
}
?>
and i have crated two pages "Bsnl.php" and "Idea.php" Based on the selection taken in the dropdownlist the page will be redirected into either Bsnl/Idea.php
I have created the Bsnl/Idea.php page containg one textfield and submit button the code is as follows:
<form value="Bsnlform action="default.php" method="post">
<b>Amount:</b><input type="text" name="amount"/><br>
<input type="submit" value="submit">
</form>
php code is as follows:
<?php
if(!empty($_POST['amount']))
{
$amount = $_POST['amount'];
}
?>
atlast i want to print the entire form details(indexform as well as Bsnl/idea form) on the default.php page can any one help me how to do to print the values on default.php upto now i have tried the php code as follows:
<?php
echo $amount;
echo $network;
echo $number;
?>
First off, you need to enable session_start();
and change to this like so....
<?php
//Enable session first
session_start();
//Flush any existing session variables, you can choose where/if you wanna do this
session_unset();
if(isset($_POST['network']) && isset($_POST['number']))
{
$_SESSION['number'] = $_POST['number'];
$_SESSION['network'] = $_POST['network'];
}
if(!empty($_POST['network']))
{
switch($_SESSION['network'])
{
case "1":header("Location:Bsnl.php");break;
case "2":header("Location:Idea.php");break;
default:exit();
}
?>
Then change this to this....
<?php
//Session enable again
session_start();
if(!empty($_POST['amount']))
{
$_SESSION['amount'] = $_POST['amount'];
}
?>
Then in your next file....
<?php
session_start();
echo $_SESSION['amount'];
echo $_SESSION['network'];
echo $_SESSION['number'];
?>
Of course on your other page you wont echo $_SESSION['amount'], because it wont be set yet...
So, here I have page1.php:
<form action="action_form.php" method="post">
<select name="font_syle">
<option value="tahoma">Tahoma</option>
<option value="arial">Arial</option>
</select>
<input type="submit" value="Done" />
</form>
Here action_form.php:
<?php
session_start();
$font_style = $_POST["font_syle"];
$_SESSION["font_syle"] = $font_style;
if($_SESSION["font_syle"] == 'tahoma') $font_style = 10;
else if($_SESSION["font_syle"] == 'arial') $font_style = 20;
$total = $font_style;
echo $total;
?>
And here page.php
<?php
ob_start();
include 'action_form.php';
ob_end_clean();
echo $total;
?>
I don't know why the value of "$total" is not printed on page.php
page.php includes action_form.php. That sets the value of $font_style to:
$font_style = $_POST["font_syle"];
Since page.php hasn't just been posted through a form, it's setting $font_style to an empty string. So when you come to echo it out, there's nothing there to echo.
You can do echo $_SESSION["font_syle"]; in the page.php to print it
The reason is your form is going to action_form.php and store the data inside the variable $_SESSION.
When you open page.php the data doesn't exist anymore because $total does not move between page.
The solution here is to change :
<form action="action_form.php" method="post">
for
<form action="page.php" method="post">
OR
Print out the session variable instead.