I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.
Related
I have a page with 2 select option dropdown lists and I can only use one of them each time I have to submit the selected option but if I use two and click on submit i get an error Only use one list and thats how it should work.
for example:
I have 2 drop lists like this one
if i choose both like the pic above and click on submit I get an error and the selected options are shown as the deffult option and thats what I want,
but when I click on the reset button it need to reset them to the default value but nothing happens.
like this
The Options for the dropdown list is from two different arrays.
$test1 and $test2 are two different arrays
<form name="rechner" method = "POST">
<select name="one">
<option value="" selected hidden>Choose One</option>
<?php foreach ($test1 as $one => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['one'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<select name="two">
<option value="" selected hidden>Choose one</option>
<?php foreach ($test2 as $two => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['two'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<input type="submit" name="calc" value="Submit" />
<input type="reset" name="reset" value="Reset">
</form>
if ($_POST['calc'] == "Berechnen") {
/// do something
}
found the answer on other question
Here
<script type="text/javascript">
function resetForm($myform) {
$myform.find('input:password, input:file, select, textarea').val('');
$myform.find('input:radio,input:checkbox')
.removeAttr('checked').removeAttr('selected);
}
</script>
<form name="rechner" method = "POST" id="myform">
/////html code/////
<input type="button" value="Reset" onclick="resetForm($('#myform'));">
</form>
I am beginner in PHP. In edit section I have defined a Do-While loop to get the information from data base. But when the user choose from the drop-down and submit
the selected item disappears and fist option appears on drop-down. How Can I retrieve and echo selected items after submitting.
<?php
$cat_sql= "SELECT * FROM cars";
$cat_sql_query=mysql_query($cat_sql);
$cat_sql_row=mysql_fetch_assoc($cat_sql_query);
?>
<form action="MSPEditstock.php" method="POST">
<span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
<select id="sortmethod" name="editstock" >
<?php do{?>
<option value="<?php echo $cat_sql_row['id']; ?>" > <?php echo $cat_sql_row['Brand'].$cat_sql_row['Model'];?> </option>
<?php ; }while($cat_sql_row=mysql_fetch_assoc($cat_sql_query))?>
</select>
<input type="submit" value="Start Editting" class="fvisual">
</form>
If I understood correctly you wish for the value selected by a user before submitting the form to be selected in the dropdown menu once the page has reloaded? Hopefully the following will give an idea as to how you could achieve that.
<?php
$cat_sql= "SELECT * FROM cars";
$cat_sql_query=mysql_query( $cat_sql );
$cat_sql_row=mysql_fetch_assoc( $cat_sql_query );
$editstock=!empty( $_POST['editstock'] ) ? $_POST['editstock'] : false;
?>
<form action="MSPEditstock.php" method="POST">
<span style="font-size: 18px; margin:10px;">Please Choose your car from the dropdown:</span>
<select id="sortmethod" name="editstock" >
<?php
while( $cat_sql_row=mysql_fetch_assoc( $cat_sql_query ) ){
$id=$cat_sql_row['id'];
$brand=$cat_sql_row['Brand'];
$model=$cat_sql_row['Model'];
$selected=$editstock && $editstock==$id ? ' selected ' : '';
echo "<option value='{$id}'{$selected}>{$brand}{$model}";
}
?>
</select>
<input type="submit" value="Start Editting" class="fvisual">
</form>
I'm having a hard time to fix and how can make my codes work well.
My textbox echo correctly while my dropdown box is not.
Can anyone help me and also clean my codes?
I wanna know how did you do it and can u please explain it to me.
Thank you so much.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Dropdown: <select name="drpdown1" value="<?php echo $drpdown1;?>">
<option></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
</select>
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$drpdown1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$drpdown1 = $_POST["drpdown1"];
}
?>
You're not echoing the value of $drpdown1 correctly:
// this is wrong for a select:
<select name="drpdown1" value="<?php echo $drpdown1;?>">
// etc.
If you want to select automatically the previously selected value, you need to add the selected attribute:
<select name="drpdown1">
<option value="1" <?php if ($drpdown1 === '1') { echo "selected='selected'"; } ?>>Mark</option>
<option value="2" <?php if ($drpdown1 === '2') { echo "selected='selected'"; } ?>>Extreme</option>
// etc.
you have to know more about the dropdown box because you can not put the value inside the
<select value="<?php echo $drpdown1;?>">
you have to compare the value inside the option directly. example
<select name="drpdown1">
<?php
if($drpdown1 == ""){
?>
<option selected></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
<?php
}else if($drpdown1 == "1"){
?>
<option></option>
<option value="1" selected>Mark</option>
<option value="2">Extreme</option>
<?php
}
?>
</select>
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
I have three multiple select menus. I am trying to select the options returned from a database query. It is not setting any options. I console log the arrays returned by php and they are displayed as:["5233", "7148"]["5233", "5437", "5316"]["7029", "7852", "5525"].
I am not concerned about the form submission at this point. I only want to display the values returned from the db query as selected. I am sure there is something I am missing but can't seem to figure it out. I appreciate all the help I get. Thank you in advance!!
First are my 3 database arrays that are created:
Second is my html:
Third is my javascript/jQuery:
<?
$huntNum = $hunts[$i][$fm_hunt_fields['__id']];
$cookRole = 'Cook';
$cookVols = get_volunteersByHunt($huntNum, $cookRole);
foreach ($cookVols as $cVols) {
//create new cook array that only contains ID of person
$exCooks[] = $cVols['_id_acct'];
}
$guideRole = 'Hunt Guide';
$hgVols = get_volunteersByHunt($huntNum, $guideRole);
foreach ($hgVols as $hVols) {
$exHg[] = $hVols['_id_acct'];
}
$supportRole = 'General Support';
$gsVols = get_volunteersByHunt($huntNum, $supportRole);
foreach ($gsVols as $gVols) {
$exGs[] = $gVols['_id_acct'];
}
?>
<script>
var existing_cooks = <?= json_encode($exCooks); ?>
var existing_hgs = <?= json_encode($exHg); ?>
var existing_gss = <?= json_encode($exGs); ?>
</script>
<form action="" name="vol-update" id="vol-update" class="vol- update" method="post">
<input type="hidden" id="id_hunt" name="id_hunt" value="<?= $huntNum; ?>" />
<input type="hidden" name="action2" id="action2" value="1" />
<table class="tyhp-account">
<tr>
<td>
<div class="floatL cont_lft_side">
<label for="cooks"><strong>Cooks</strong></label>
<select name="cook[]" class="form-control vCooks" multiple="multiple">
<?php
foreach ($cooksArray as $cook) {
?>
<option
<?
if (in_array($cook['ID'], $exCooks)) {
echo "selected='selected'";
}
?>
value="<?= $cook['ID']; ?>" >
<?= $cook['name_last'] . $cook['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<td>
<div class="floatL cont_lft_side">
<label for="hunt_guides"><strong>Hunt Guides</strong></label>
<select name="huntGuide[]" class="form-control vHg" multiple="multiple">
<?php
foreach ($guidesArray as $guide) {
?>
<option
<?
if (in_array($guide['ID'], $exHg)) {
echo "selected='selected'";
}
?>
value="<?= $guide['ID']; ?>" >
<?= $guide['name_last'] . $guide['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
<?php
$allVols = getAllVolunteers();
?>
<td>
<div class="floatL cont_lft_side">
<label for="supp"><strong>General Support</strong></label>
<select name="gsupport[]" class="form-control vSupp" multiple="multiple">
<?php
foreach ($allVols as $allVol) {
?>
<option
<?
if (in_array($allVol['__id'], $exGs)) {
echo "selected='selected'";
}
?>
value="<?= $allVol['ID']; ?>" >
<?= $allVol['name_last'] . $allVol['name_first']; ?>
</option>
<?
}
?>
</select>
</div>
</td>
</tr>
<tr>
<td> </td>
<td style="text-align:center">
<input type="submit" name="action" id="upVol" class="btn btn-primary" value="Update Volunteers">
</td>
<td> </td>
</tr>
</table>
</form>
//Volunteer information for hunts
<script>
var cooks = existing_cooks;
var hunt_guides = existing_hgs;
var gen_support = existing_gss;
console.log(cooks);
console.log(hunt_guides);
console.log(gen_support);
//Cooks multiSelect
$(".vCooks").val(cooks);
$('.vCooks').multiselect({
columns: 2,
placeholder: 'Select Cooks'
});
//Hunt Guide multi-select
$(".vHg").val(hunt_guides);
$('.vHg').multiselect({
columns: 2,
placeholder: 'Select Hunt Guides'
});
//General Support multi-select
$(".vSupp").val(gen_support);
$('.vSupp').multiselect({
columns: 2,
placeholder: 'Select General Support'
});
return false;
</script>
I'm making a rock paper scissors game and players need to choose if they want to play best out of 1, 3, 5 or 7 before they start the game and i need it to work with a Select field and a submit button.
I am very new to the select tag but it would suit me best if the selected number could be exctracted with $_POST or $_GET
This is the form:
<form method="post">
<h1>Best out of </h1>
<select>
<option value="one">1</option>
<option value="three">3</option>
<option value="five">5</option>
<option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>
This is the PHP:
<?php
if(isset($_POST['start']) && isset($_POST['one']))
{
echo "do something 1";
};
if(isset($_POST['start']) && isset($_POST['three']))
{
echo "do something 2";
};
if(isset($_POST['start']) && isset($_POST['five']))
{
echo "do something 3";
};
if(isset($_POST['start']) && isset($_POST['seven']))
{
echo "do something 4";
};
?>
Give your select a name attribute
<form method="post">
<h1>Best out of </h1>
<select name="my_select">
<option value="one">1</option>
<option value="three">3</option>
<option value="five">5</option>
<option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>
After submitting your form in the PHP script you can get select value using $_POST array with index my_select ( name attribute value of the select element):
<?php
if(isset($_POST['start']) && isset($_POST['my_select']))
{
if($_POST['my_select'] === 'one'){}
....
}
?>