Session not passing dropdown selected element to other php script - php

First php script -
<?php
session_start();
?>
<html>
<head>
</head>
<body><form method = "post">
<select name="feature" id="feature">
<?php
?>
<option value > Select Feature</option>
<?php
foreach($newFeature as $feat)
{
?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
<?php
}
?>
</select>
</form>
</body>
</html>
<?php
$_SESSION['feature'] = $feature;
?>
second php script -
<?php
session_start();
echo $_SESSION['feature'];
?>
When I run second php script, I get Array as echoed element instead of the element I selected.
What is wrong with the logic here ?
Please guide.

You have to submit the select. It is impossible to set $feature at that moment because the user hasn't yet selected anything.
<form method = "post">
<select name="feature" id="feature">
<option value > Select Feature</option>
<?php foreach($newFeature as $feat) : ?>
<option value="<?php echo $feat;?>" <?= $feat == $_SESSION['feature'] : " selected = 'selected'" : "" ?>><?php echo $feat;?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" name="mySubmit" />
</form>
When you hit 'send' you can get the value by using $_POST['feature']; on the same page. If you want to go to another page you have to set the form action property.
session_start();
$_SESSION['feature'] = $_POST['feature'];
After the submit the page will 'reload'. Check if mySubmit is set and set the $_SESSION['feature'](don't forget to start your session at top of the page):
if (isset($_POST['mySubmit'])){
$_SESSION['feature'] = $_POST['feature'];
}

Related

HTML form POST data not transferring to next page

I have a form with a drop-down box. The user selects an option from the dropdown box and hits submit. On submit, a new page opens up with a new form. The option that was selected is not being transferred to the next page. My form code is:
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname" type="input">
<option></option>
<?php foreach ($data as $row): ?>
<option>
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="heroname">Submit</button>
</div>
</form>
The next page loads and displays some test variables at the top, but the variable from the POST is empty. My code on the page is
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
include('header.php');
$username = $_SESSION['username'];
echo $_POST['heroname'];
echo $username;
$test1 = "test 1, before the if statement";
echo $test1;
I get no errors on the page. The username variable and the test1 variable echo normally. The heroname variable doesn't. I need help figuring out why the selection from the form is not transferring to the next page. Thanks in advance.
You have forgotten the value attribute from the <option> elements. Whatever is in the value attribute is what is returned to the server script.
You have a type="input" attribute on the <select> element, that does not belong there.
You have called the button <button type="submit" name="heroname"> which is also what you called the dropdown <select name="heroname"> you cannot use the same name more than once. So change the button name to something else like <button type="submit" name="submithero"> for example
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit" name="submithero">Submit</button>
</div>
</form>
the button does not need a name attribute
<form method="post" action="hero_modify_form.php">
<div>
<label>Select a hero to add or modify: </label>
<select name="heroname">
<option></option>
<?php foreach ($data as $row): ?>
<option value="<?php echo $row['Hero_Name'] ?>">
<?php echo $row['Hero_Name'] ?>
</option>
<?php endforeach ?>
</select>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
First, I removed the value attribute from select. Second, I added a value attribute to option. Third, I changed the name for the button to match the IF statement on the next page. The heroname is now being passed to the next page. I have a whole new set of errors now about undefied index, which I will research first, and if needed, post a new question. Thank you all for the help!!

retain select option value in php

<select class="form-control" id="oid">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<?php
echo $row['id'];
?>
i want to echo this select option value in php , it will catch easily in javascript but cant echo in php
it will catch easily in javascript but cant echo in php
PHP is a server-side scripting language, which means that in your case you could only echo the option value of the selection after submitting it to the server. Here echo $row['id']; will just print the last id fetched from your database.
(Client-side) JS executes in the browser, this allows you to track user selections as they are happening.
<form action="#" method="post">
<select class="form-control" id="oid" name="userselection">
<option>--Select--</option>
<?php
while($row=mysqli_fetch_array($run))
{
?>
<option value="<?= $row['id'];?>">
<?= $row['first_name'];?> #<?= $row['mobileno']; ?>
</option>
<?php
}
?>
</select>
<input type="submit" name="submit" value="Submit selection" />
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['userselection'];
}
?>

Changing a PHP variable value with a select option list in ajax

I have a php variable called $photographerNum that i want the value of to change depending upon value in my drop down field on my page, what would the javascript look like for this as well as how to receive the new value to update the php variable?
<!DOCTYPE html>
<html>
<body>
<? $photographerNum = 1; ?>
<p>Select an item from the list.</p>
<select id="photographerNum" onchange="myFunction()">
<option value="Item1">Item1
<option value="Item2">Item2
<option value="Item3">Item3
<option value="Item4">Item4
</select>
<script type="text/javascript">
function myFunction() {
var itemSelected = document.getElementById("photographerNum").value;
document.getElementById("demo").innerHTML = "You selected: " + itemSelected ;
}
</script>
<p id="demo"> - photoNum=<? echo $photographerNum ?></p>
</body>
</html>
Try this, when you select a new item a form is triggered which outputs the value of the selected item, after that, you can send the variable and process it in server-side.
<?php
$photographerNum = 1;
echo 'Initial value photographerNum::' . $photographerNum;
?>
<form name="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="photographerNum" id="IdphotographerNum" onchange="myform.submit();">
<option value="0">Selecte an item
<option value="1">Item1
<option value="2">Item2
<option value="3">Item3
<option value="4">Item4
</select>
</form>
<?php
if (isset($_POST['photographerNum'])) {
echo 'New value photographerNum::' . $_POST['photographerNum'];
}
?>

make an option from select tag stays the same after reload or change the page, PHP

So im having problems on figuring out how to make option tag so when is pressed it will make the action on all the pages
This is what i have done:
index1.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
index2.php
include 'select.php'
<title>Ultra 2014 <?php if (isset($title)) {echo $title;} else{echo "Miami";}?> Area</title>
select.php
<div id="regContainer">
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option value="miami">miami</option>
<option value="new york">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
</div>
process.php
<?php if(!isset($_SESSION)){ session_start();}
if(isset($_POST['selectedRegion'])){
$region = $_POST['selectedRegion'];
if($region == "miami"){
echo "miami selected";
$title = "miami";
}
if($region == "new york"){
echo "new york selected";
$title = "new york";
}
}
?>
when I press the first option, it changes the title normally on the index1.php, but when i go to the index2.php... it does not change the title of that page, I have to press the first option button again to make the change.
Is there anyway to just press the first option from the selection and then change the title of all the pages without having to press the button again?
Any suggestion will help. Thanks
Using the code you currently have you could do the following
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_REQUEST['selectedRegion'] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_REQUEST['selectedRegion'] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
This will ensure the option is selected on page refresh. If you want something that also acts on page change then you will need to use a cookie
if(isset($_REQUEST['selectedRegion'])){
setcookie("region", $_REQUEST['selectedRegion'], time()+3600);
}
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option <?php if($_COOKIE["region"] == 'miami') echo "selected"; ?> value="miami">miami</option>
<option <?php if($_COOKIE["region"] == 'newyork') echo "selected"; ?> value="newyork">ny</option>
</select>
<input type="submit" value="Enter Region" />
</form>
You need to change
if(!isset($_SESSION)){ session_start();}
To just
session_start();
What you're saying currently is if you have a session set, don't load the session library to then use that session data.

PHP drop menu not sticking

I asked this question already but have since over hauled the code following the help I got. I am trying to make my php drop menu sticky but it clears to the top menu item every time after the submit button is pressed. I am not sure where I am going wrong so any help is greatly appreciated. Code as follows:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
if (isset($_POST['Question']))
{
$menuVar = $_POST['fontFamily'];
}
?>
<p id="info-req">How did you find about this site?</p>
<form name="TestMenu" method="post" id="marketing">
<select name="Question">
<option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
<option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
<option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
<option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
<option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>
</select>
<input type="submit" />
</form>
</body>
</html>
First of all, there is no $POST['fontFamily'] variable in your form. Why are you trying to use it?
You should use $_POST['Question'] in order to get this value.
So it should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
}
Also you should init $menuVar if there's no $POST in order not to get a Notice: Undefined variable $menuVar. So in the end you code should be:
if (isset($_POST['Question']))
{
$menuVar = $_POST['Question'];
} else {
$menuVar = "----------";
}

Categories