<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
When I submit this form, I stay on the same page, but I want a the same time to keep the option selected previously option visible.. How can I do that?
I tried:
document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";
But it did not work..
here:
foreach($allemp as $row){
echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
Try this :
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
added a line above echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)
Added '.$selected.' in side options.
Your code becomes : Copy paste below code instead of yours
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
} ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
Related
I am having troubling getting a simply if/else conditional for options in an html select form. If the value for bid is 'N', then I want one option for the select field to change to "Y" and vice versa. I know my db connection is good and other php works and the form was updating the db fine until I tried to work in the php conditional code within the so I just need to get the syntax correct.
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)){
?>
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
<?
}
if statement expect expression and you have syntax error in your statement
if (echo $row['bid']; == 'N')
the syntax error because of echo and the semicolon ;
you should change your condition to
if ($row['bid'] == 'N'){
if $row['bid'] string then you need to compare with strcmp , like this
if (strcmp($row['bid'] , 'N') == 0){
also you need to move <input> tag outside the <select>
Your <input type="submit"> was inside your <select>, so I moved it outside.
It is also incorrect to use the if statement in this manner:
if (echo $row['bid']; == 'N')
correct would be this:
if ($row['bid'] == 'N')
Here is the corrected code:
<form method="post" name="update" action="update.php" >
<select name="bid">
<?php
echo '<option value="'.$row['bid'].'" selected="selected">'.$row['bid'].'</option>';
if ($row['bid'] == 'N') echo '<option value="Y">Y</option>';
else echo '<option value="N">N</option>';
echo '</select>';
?>
<input type="submit" name="Submit" value="update" >
</form>
Your condition is bad, you must remove echo and ;. it should like this
<? if ($row['bid'] == 'N') { ?>
And your submit button should be presented out side the select field.
Your codes should look like this:
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?=$row['bid']?>" selected="selected">
<?=$row['bid']?>
</option>
<?php if ($row['bid'] == 'N') {
echo '<option value="Y">Y</option>';
} else {
echo '<option value="N">N</option>';
}
?>
</select>
<input type="submit" name="Submit" value="update" >
</form>
Your if condition has bad syntax. Remove the semicolon and echo statement.
<form method="post" name="update" action="update.php" >
<select name="bid">
<option value="<?php echo $row['bid']; ?>" selected="selected">
<?php echo $row['bid']; ?></option>
<? if ($row['bid'] == 'N') {
?>
<option value="Y">Y</option>
<? } else {
?>
<option value="N">N</option>
<? }
?>
<input type="submit" name="Submit" value="update" >
</select>
</form>
I want a first radio button to be checked by default. When I click get schedule I want to fetch the schedule of that radio selected train number using a WHERE clause.
<?php
while ($res = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type = radio />" . $res['Train_no'] . "</td>";
?>
<form action="schedule.php" method="POST" class="form-inline">
<input type="submit" value="Get Schedule"/>
</form>
<?php
}
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('traindb', $con);
} else {
die('Could not connect: ' . mysql_error());
}
$selected_val = $_POST['Train_no']; // Storing Selected Value In Variable
echo "You have selected :" . $selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>
You can try this:
if(isset($_POST['Submit'])){
$selected_val = $_POST['Train_no']; // Storing Selected Value In
echo "You have selected :" .$selected_val; // Displaying Selected Value
$result = mysql_query("SELECT * FROM train_detail WHERE
Train_No='$selected_val'");
}
Here is one example of "select":
<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
<!-- <option value="disable" selected="">Please Select</option> -->
<option value="">Please select</option>
<option value="Under 35">Under 35</option>
<option value=">35 - 44">35 - 44</option>
<option value=">45 - 54">45 - 54</option>
<option value=">55 - 59">55 - 59</option>
<option value=">60 - 64">60 - 64</option>
<option value=">65 - 69">65 - 69</option>
<option value=">70 - 74">70 - 74</option>
<option value=">75 - 79">75 - 79</option>
<option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
$age = $_POST['age'];
if (isset($_POST['age']) && $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>
If you want embed PHP in select options, do it like this:
<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>
For radio button it is like this:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other<br><br>
<input type="submit">
</form>
</body>
</html>
I have select box in PHP and want text selected item of that:
<form action="" method="get" target="" dir="rtl" class="w3-container">
<?php
echo " <select id='p1' name='p1' >";
echo "<option value='*'>Choose person</option>";
sql code
$result2 = mysql_query($query2,$con);
while($row2 = mysql_fetch_array($result2))
{
$row2['name'];
echo "<option value='".$row2['id']."'>".$row2['name']."</option>";
}
echo " </select>";
mysql_close($con);
?>
<input class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
</form>
<?php
if(isset($_GET['p1']) and $_GET['p1'] != "*")
{
if ($name_insert=trim($_GET["p1"]))
{
if ($strWhere == ""){
$strWhere .= "name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
else{
$strWhere .= " and name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
}
}
?>
I want code that get text of select box in $name_insert. This code gets value of that.
You can use javascript and save selected option text in a hidden field.
Example
<form action="" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
PHP
$getOptionText = $_POST['test_text'];
Replace your code with this code
<form action="" method="get" target="" dir="rtl" class="w3-container">
<select id='p1' name='p1' onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value='*'>Choose person</option>
<?php
$result2 = mysql_query($query2,$con);
while($row2 = mysql_fetch_array($result2))
{
echo "<option value='".$row2['id']."'>".$row2['name']."</option>";
}
mysql_close($con);
?>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
<input class="w3-btn w3-hover-yellow" name="" type="submit" value="جستجو">
</form>
<?php
if(isset($_GET['p1']) and $_GET['p1'] != "*")
{
$getValueOfText = $_GET['test_text'];
echo $getValueOfText;
if ($name_insert=trim($_GET["p1"]))
{
if ($strWhere == ""){
$strWhere .= "name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
else{
$strWhere .= " and name_insert='".trim($name_insert)."' ";
$getPage .= "&name_insert=".($name_insert);}
}
}
?>
I have this form:
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
echo '<option value=' . $hej . '>' . $hejsan[$hej] . '</option>';
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
When I press the submit button the select/option will reset to the default, how can I change this and make it stay as the one I selected?
Try this
if (isset($_POST['taskOption'])) {
$Option = $_POST['taskOption'];
}
while($hej < count($hejsan)) {
if($hej==$_POST['taskOption']){
$selected = "selected=selected";
}else{
$selected = "";
}
echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
$hej++;
}
You will need to look at the option you have selected and make the corresponding option selected. Since your form leads to the same page, you can just look at the contents of $_POST.
P.S.: also, you will find it useful to echo a newline after each option to make the output more readable.
Try this:
<?php
$taskOption = '';
if (isset($_POST['taskOption']) {
$taskOption = $_POST['taskOption'];
}
?>
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
if ($hey == $taskOption) {
echo "<option value={$hey} selected>{$hejsan[$hej]}</option>";
} else {
echo "<option value={$hey}>{$hejsan[$hej]}</option>";
}
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
At a if else statement to it. If the option value is equal to the select, then add the selected="selected"
This is an example code. could be that the operator is wrong, im always confused with the greater then and smaller then operators.
<?php
$option_value = 5;
for($i=0; $i > 10; $i++){
if($option_value == 5){
echo '<option value="'.$i.'" selected="selected">Item number'.$i.' is selected</option>';
} else {
echo '<option value="'.$i.'">Item number'.$i.'</option>';
}
}
?>
I have got the following form:
<form action="" method="get" target="_self">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
<select name='Manufacturer' class="dropmenu" onChange="this.form.submit()" >
<?php
echo '<option value="">Any</option>';
while ($row = mysql_fetch_array($RS_Search1)) {
$selected = $_GET['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>';
} ?> </select>
<select name="Color" class="dropmenu" onChange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
while ($Color = mysql_fetch_array($RS_Search2)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?> </form>
Second form to submit
<form action="search2.php" method="get"> </select><input name="Category" type="hidden" value="<?php echo $_GET['Category']; ?>">
<input name="Manufacturer" type="hidden" value="<?php echo $_GET['Manufacturer']; ?>">
<input name="Color" type="hidden" value="<?php echo $_GET['Color']; ?>">
<select name="price" class="dropmenu">
<?php
echo '<option value="">Any</option>';
while ($Price = mysql_fetch_array($RS_Price)) {
$selected3 = $_GET['price_range'] == $Price['price_range'] ? 'selected' : '';
echo '<option '.$selected3.'>' . $Price['price_range']. '</option>';
} ?>
</select><input name="Search2" type="submit" id="Search2" value="Submit">
</form>
As you can see the option values are pulled from a db, after the first value is chosen. What the form does at the minute is reload the whole page. Is there a way to just reload the form in stead of reloading the whole page. I need to keep the values that are submitted on change of a option value to be able to fill in the next drop down menu.
Any help welcome.