Im trying to retrieve the value from a select box after the selected index has been changed. I keep getting an undefined index variable.
The form reloads the page so that I can update a table elsewhere on the page. The options are filled from the results of an SQL query.
The select box code.
<form action="" method="post">
<label>Select School</label>
<select class="form-control" name="schoolSelect" onchange="this.form.submit()">
<?php
foreach ($faculty as $key) { ?>
<option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>
<?php } ?>
</select>
</form>
PHP used to retrive value
if (isset($_POST['schoolSelect'])){
$selectedSchool = $_POST['schoolSelect'];
$result = executeUserSelect($sqlUserBySchool, $db, $_POST['schoolSelect']);
}
EDIT
var dump =
array (size=1)
'schoolSelect' => string 'Plymouth Business School' (length=24)
Select box text = Plymouth Business School
Thanks in advance
Tony
<body>
<?php
if (isset($_POST['schoolSelect'])){
$selectedSchool = $_POST['schoolSelect'];
echo $selectedSchool;
}
else {
?>
<form action="" method="post">
<label>Select School</label>
<select class="form-control" name="schoolSelect" onchange="this.form.submit()">
<?php
foreach ($faculty as $key) { ?>
<option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>
<?php } ?>
</select>
</form>
<?php } ?>
</body>
Related
I have one dropdown with a foreach loop which will pass to model using post method.
<div class="form-group" ">
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php
foreach ($relationship as $row) {
?>
<option value="<?php echo $row->relID; ?>"><?php echo $row->relCat; ?></option>
<?php
}
?>
</select>
</div>
and in the model it is getting the proper values using post method. As
$rel = $_POST['rel'];
Here the problem is when the user select one option ,I want to get two values like
"<?php echo $row->relID; ?>" and "<?php echo $row->relCatID; ?>"
like
$rel = $_POST['rel']; //this for the $row ->relID
$relcat = $_POST['relcat'];//this for the $row ->relCatID
I want to get both from one selection without adding any visble dropdown or element..
Try bellow code with ajax call
In Javascript code get value
var name = $('#rel option:selected').attr('data-cat_id');
var id = $('#rel').val();
<div class="form-group" >
<select class="form-control" id="rel" name="rl[][rel]" >
<option></option>
<?php foreach ($relationship as $row) { ?>
<option value="<?php echo $row->relID; ?>" data-cat_id="<?php echo $row->relCatID; ?>"><?php echo $row->relCat; ?></option>
<?php } ?>
</select>
</div>
This question already has answers here:
Keep values selected after form submission
(12 answers)
Closed 4 months ago.
I have the selection menu which takes the value from the txt file. I want the selected value to remain selected even after the form submission.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
For keeping the value selected,I tried this:
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'" if($_POST['$lines']) echo $_POST['$lines'];">$lines</option>";
}?>
But it is not working, may be I am using echo inside the echo. Please correct me.
You can use this code. Although I haven't tested the code but let me share my logic with you to make you a better understanding of it. I have added a condition that if the form is submitted then it should display the form with $_POST['toolchain'] selected otherwise it should display it in normal ways.
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
if(isset($_POST['toolchain'])
{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="<?php echo $_POST['toolchain']; ?>"><?php echo $_POST['toolchain']; ?></option>
<?php foreach($eachlines as $lines){
if($lines!=$_POST['toolchain'])
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
else{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>
</form>
<?php }
?>
It's a normal but a lengthy way. You can also add a condition inside form. If the form is submitted mean if(isset($_POST['toolchain'])) then you can make the option selected for the $_POST['toolchain'] inside foreach loop
Update
I have put the condition inside the form. Kindly update if you face any error as I haven't tested the code
<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<?php if(isset($_POST['toolchain']))
{
?>
<option value="base">Please Select</option>
<?php foreach($eachlines as $lines){
if($_POST['toolchain']==$lines))
{
echo "<option selected value='".$lines."'>$lines</option>";
}
else {
echo "<option value='".$lines."'>$lines</option>";
}
}
}
else {
?>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}
}
?>
</select>
</form>
on calculatePC.php, I have this code to display the finish_product
Select product:
<select class="itemTypes">
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
Let's say I have chosen Table as the finish_product.
On docalculate.php, I would like to display what I've chosen based on the dropdown list I've selected.
I tried this but there is error.
<?php echo $_POST['finish_product'] ?>
May I know how to display the result?
This doesn't exist:
$_POST['finish_product']
because you don't have a form element named "finish_product" in your markup. Add that name to the form element:
<select name="finish_product" class="itemTypes">
You need to do two things:-
Create a form before select and give it an action
give name attribute to your select box, then only you can get data in $_POST
So do like below:-
<form method="POST" action = "docalculate.php"> // give the php file paht in action
<select class="itemTypes" name="finish_product"> // give name attribute to your select box
<?php while ($row1 = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row1['finish_product']; ?>">
<?php echo $row1['finish_product']; ?>
</option>
<?php } ?></select>
<br/><br/>
<input type ="submit" name="submit" value ="Submit">
</form>
Now on docalculate.php:-
<?php
echo "<pre/>";print_r($_POST['finish_product']); // to see value comes or not
Note:- through Jquery its also possible. Thanks
On form submit I want all my input fields to be remembered, input and textarea's already work but I can't get this selection to work properly.
This is my selection
<select name="signalering">
<option value="Bezoek" selected>Bezoek</option>
<option value="Meerwerk">Meerwerk</option>
<option value="Stelpost">Stelpost</option>
<option value="Verrekenpost">Verrekenpost</option>
<option value="Levering">Levering</option>
<option value="Aandachtspunt">Aandachtspunt</option>
<option value="Tekortkoming">Tekortkoming</option>
<option value="Opname werk">Opname werk</option>
<option value="Overig">Overig</option>
</select>
If anyone knows a simple sollution to remember this dropdown selection I would be sooo happy :)
You could just check upon submission on those tag. Check is submitted value is equal to the value, then echo selected attribute:
Rough example:
<?php $options = array('Bezoek', 'Meerwerk', 'Stelpost', 'Verrekenpost', 'Levering', 'Aandachtspunt', 'Tekortkoming', 'Opname werk', 'Overig'); ?>
<select name="signalering" onchange="this.form.submit()">
<?php foreach($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_POST['signalering']) && $_POST['signalering'] == $option) ? 'selected' : ''; ?>>
<?php echo $option; ?>
</option>
<?php endforeach; ?>
</select>
Sample Output
Sidenote: This is just an example. You do not need onchange="this.form.submit()" on the select tag.
im guessing your posting something, just stick a script at the top of the page which checks if the value exists and loop through it accordingly
if( isset($_POST['value']) )
{
//do loop here
}else{
//output default select code
}
<form id="form" name="form" action="" method="post" enctype="multipart/form-data">
<?php
$signalering = addslashes(trim($_POST['signalering']));
$options = array('Bezoek', 'Meerwerk', 'Stelpost', 'Verrekenpost', 'Levering', 'Aandachtspunt', 'Tekortkoming', 'Opname werk', 'Overig');
$sel_output = '<select name="signalering" onChange="this.form.submit()">';
$sel_output .= '<option value="">Select</option>';
foreach($options as $option)
{
if($signalering == $option){$selcted = 'selected';}else{$selcted = '';}
$sel_output .= '<option value="'.$option.'" '.$selcted.'>'.$option.'</option>';
}
$sel_output .= '</select>';
echo $sel_output;
?>
</form>
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'];
}