I've saved the value of the apple from the select drop down to the mysql.
Now in the edit page, i'd like to show the respective value.
But i dont seems to get it right
<?php $food = $mysqli->real_escape_string($_POST['food']); ?>
<select id="food" required name="food" >
<option value="" disabled selected>Select a Food</option>
<option value="apple" <?php if($food == apple)
echo "selected='selected'"; ?>Apple</option>
<option value="kiwi" <?php if($food == kiwi)
echo "selected='selected'"; ?>Kiwi</option>
</select></div>
<select id="food" required name="food" >
<option value="" >Select a Food</option>
<option value="apple" <?php if($food == "apple")
echo "selected='selected'"; ?>Apple</option>
<option value="kiwi" <?php if($food == "kiwi")
echo "selected='selected'"; ?>Kiwi</option>
</select></div>
String should be enclosed by single quotes or double quotes if($food == 'apple')
<select id="food" required name="food" >
<option value="" disabled selected>Select a Food</option>
<option value="apple" <?php if($food == 'apple')
echo "selected='selected'"; ?>Apple</option>
<option value="kiwi" <?php if($food == 'kiwi')
echo "selected='selected'"; ?>Kiwi</option>
</select></div>
Related
i am trying to get the value of an option with the value set.
via javascript though I get the text instead of the value
is there something i'm doing wrong?
<div id="scheda">
<form name="scheda">
<fieldset class="gruppo">
<fieldset class="row"><label>Tipologia contratto</label>
<select name="contratto" id="contratto" class="select">
<option value="0">--</option>
<option value="1">Contratto 1/2 giorni</option>
<option value="2">Contratto 3 giorni</option>
<option value="3">Contratto 4 giorni</option>
<option value="4">Contratto 5 giorni</option>
<option value="5">Non in attività</option>
</select>
</fieldset>
</fieldset>
</form>
</div>
this is the js code
var tipocontratto = document.scheda.contratto.value;
the console log instead of giving me the value set gives me the text of the option
function getValue() {
console.log(window.contratto.value)
}
<div id="scheda">
<form name="scheda">
<fieldset class="gruppo">
<fieldset class="row"><label>Tipologia contratto</label>
<select name="contratto" id="contratto" class="select">
<option value="0">--</option>
<option value="1">Contratto 1/2 giorni</option>
<option value="2">Contratto 3 giorni</option>
<option value="3">Contratto 4 giorni</option>
<option value="4">Contratto 5 giorni</option>
<option value="5">Non in attività</option>
</select>
</fieldset>
</fieldset>
</form>
<button onCLick="getValue()">Get Value</button>
</div>
Since contratto is an id you can access it directly via the window object.
So window.contratto.value should do the trick.
i found the problem.
the select is repeated further down to retrieve the data of a user that has already been saved, in that select the values inside the options were missing.
<fieldset class="row"><label>Tipologia contratto</label>
<select name="contratto" class="select">
<option <?php if($ut->tipo_contratto=="0"){?> selected="selected" <?php }?> >--</option>
<option <?php if($ut->tipo_contratto=="1"){?> selected="selected" <?php }?> >Contratto 1-2 giorni</option>
<option <?php if($ut->tipo_contratto=="2"){?> selected="selected" <?php }?> >Contratto 3 giorni</option>
<option <?php if($ut->tipo_contratto=="3"){?> selected="selected" <?php }?> >Contratto 4 giorni</option>
<option <?php if($ut->tipo_contratto=="4"){?> selected="selected" <?php }?> >Contratto 5 giorni</option>
<option <?php if($ut->tipo_contratto=="5"){?> selected="selected" <?php }?> >Non in attivita</option>
</select>
</fieldset>
replaced with
<fieldset class="row"><label>Tipologia contratto</label>
<select name="contratto" class="select">
<option value="0" <?php if($ut->tipo_contratto=="0"){?> selected="selected" <?php }?> >--</option>
<option value="1" <?php if($ut->tipo_contratto=="1"){?> selected="selected" <?php }?> >Contratto 1-2 giorni</option>
<option value="2" <?php if($ut->tipo_contratto=="2"){?> selected="selected" <?php }?> >Contratto 3 giorni</option>
<option value="3" <?php if($ut->tipo_contratto=="3"){?> selected="selected" <?php }?> >Contratto 4 giorni</option>
<option value="4" <?php if($ut->tipo_contratto=="4"){?> selected="selected" <?php }?> >Contratto 5 giorni</option>
<option value="5" <?php if($ut->tipo_contratto=="5"){?> selected="selected" <?php }?> >Non in attivita</option>
</select>
</fieldset>
I have 2 step form and I'm passing the first name in the URL from step 1 to step 2, but I'm not sure how to do the same thing for a select field. As an example I have this:
In the URL:
?firstname=Bob
Form Field:
<input type="text" id="firstname" name="firstname" value="<?php echo $_GET['firstname']; ?>" required>
There is a variable passing in the URL for the select field which is:
&colortype=Black+and+White
But the Select options look like this:
<label for="colortype">Colors...</label>
<select id="colortype" name="colortype">
<option value="" selected disabled hidden>Select an option</option>
<option value="Purple and Yellow">Purple and Yellow</option>
<option value="Red and Blue">Red and Blue</option>
<option value="Black and White">Black and White</option>
</select>
I have tried adding the option:
<option value="<?php echo $_GET['colortype']; ?>">
<?php echo $_GET['colortype']; ?>
</option>
But no luck. Is there a way to automatically select the colortype that comes through the URL?
Use this
<label for="colortype">Colors...</label>
<select id="colortype" name="colortype">
<option <?php if ($_GET['colortype'] == '' ) echo 'selected' ; ?> value="" disabled hidden>Select an option</option>
<option <?php if ($_GET['colortype'] == 'Purple and Yellow' ) echo 'selected' ; ?> value="Purple and Yellow">Purple and Yellow</option>
<option <?php if ($_GET['colortype'] == 'Red and Blue' ) echo 'selected' ; ?> value="Red and Blue">Red and Blue</option>
<option <?php if ($_GET['colortype'] == 'Black and White' ) echo 'selected' ; ?> value="Black and White">Black and White</option>
</select>
<label for="colortype">Colors...</label>
<select id="colortype" name="colortype">
<?php $color = $_GET['colortype']; ?>
<option <?php if ($color == '' ) { ?> selected="selected" <?php } ?> value="" >Select an option</option>
<option <?php if ($color == 'Purple and Yellow' ) { ?> selected="selected" <?php } ?> value="Purple and Yellow" >Select an option</option>
<option <?php if ($color == 'Red and Blue' ) { ?> selected="selected" <?php } ?> value="Red and Blue" >Select an option</option>
<option <?php if ($color == 'Black and White' ) { ?> selected="selected" <?php } ?> value="Black and White" >Select an option</option>
You can try this code
Colors...
<option value="" selected disabled hidden>Select an option</option>
<option value="1">Purple and Yellow</option>
<option value="2">Red and Blue</option>
<option value="3">Black and White</option>
value="Black and White">Black and White
i have a table from array in php so i put select option that have javascript to control disable/enable, .my problem is , javascript only working at 1st row of table..and the other rows are not working..can somebody help me with this thank you in advance...my code as :
$(document).ready(function() {
$('#Status1').change(function() {
if (($(this).val() === 'T') || ($(this).val() === 'M')) {
$(‘#uia’).attr('disabled', 'disabled');
} else {
$(‘#uia’).attr('disabled', false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Status1" name="Status1[]">
<option value="T" >PILIHAN 1</option></option>
<option value="M" >PILIHAN 2</option></option>
<option value="S" >PILIHAN 3</option></option>
</select>
<select id="uia" name="uia[]">
<option value="E" >PILIHAN 1</option></option>
<option value="D" >PILIHAN 2</option></option>
<option value="K" >PILIHAN 3</option></option>
</select>
first of all you have some errors in your syntax.
you have 2 closing tags for the option elements (<option value="T" >PILIHAN 1</option></option>).
you have invalid single quotes in your jQuery selectors (use ' instead of ’).
As #ADyson has noticed, use .prop( propertyName, value ) instead of .attr( attributeName, value ) for disabling and enabling elements.
$('#Status1').change(function(){
if(($(this).val() === 'T') || ($(this).val() === 'M')){
$('#uia').prop('disabled', 'disabled');
}else{
$('#uia').prop('disabled', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Status1" name="Status1[]" >
<option value="T" >PILIHAN 1</option>
<option value="M" >PILIHAN 2</option>
<option value="S" >PILIHAN 3</option>
</select>
<select id="uia" name="uia[]" >
<option value="E" >PILIHAN 1</option>
<option value="D" >PILIHAN 2</option>
<option value="K" >PILIHAN 3</option>
</select>
Note that this doesn't disable the #uia element on load.
To achieve this you could set the #uia element to disabled by default or add an onload event to the element.
<tbody>
<?php do {?>
<?php $Sellcount++ ; ?>
<tr>
<script type="text/javascript">
$(document).ready(function() {
$('#Status1').change(function(){
if(($(this).val() === 'T') || ($(this).val() === 'M')){
$('#uia').attr('disabled', 'disabled');
}else{
$('#uia').attr('disabled', false);
}
});
});
</script>
<td clas ="TD-Ubah2"><?php echo $Sellcount; ?></td>
<td class ="TD-Ubah2" nowrap><?php echo $row_rsSell['Nama']; ?></td>
<td class ="TD-Ubah2"><?php echo $row_rsSell['IC'];?></td>
<input type="hidden" id="IC" name="IC" value="<?php echo $row_rsSell['IC'];?>">
<td class ="TD-Ubah2"><input id="notel" name="notel" type="text" value="<?php echo $row_rsSell['NoTel_HF']; ?>"></td>
<td class ="TD-Ubah2">
<select id="Status1" name="Status1" >
<option value="" >SILA PILIH</option>
<option value="A" <?php if($row_rsSell['Status'] == 'A'){ echo "selected"; }?>>PILIHAN 1</option>
<option value="L" <?php if($row_rsSell['Status'] == 'L'){ echo "selected"; }?>>PILIHAN 2</option>
<option value="M" <?php if($row_rsSell['Status'] == 'M'){ echo "selected"; }?>>PILIHAN 3</option>
</select>
</td>
<td class ="TD-Ubah2">
<select id="uia" name="uia" >
<option value="" >SILA PILIH</option>
<option value="P" <?php if($row_rsSell['uia'] == 'P'){ echo "selected"; }?>>PILIHAN 1</option>
<option value="K" <?php if($row_rsSell['uia'] == 'K'){ echo "selected"; }?>>PILIHAN 2</option>
<option value="H" <?php if($row_rsSell['uia'] == 'H'){ echo "selected"; }?>>PILIHAN 3</option>
</select><br>
</td>
</tr>
<?php } while ($row_rsSell = mysql_fetch_assoc($rsSell));?>
</tbody>
i just amend my code..and still the other rows not run that java script..
Is there any chance that I can get dependent drop down list in php - without using jquery or javascript - please help me through few options or suggestions if available
/group 1 /
<div class="form-group">
<label for="return_ori">Return scenarios group A signals</label>
<select name="return_ori" class="form-control" id="Site Orientation">
<option value="East">none</option>
<option value="GA">hello1 </option>
<option value="GB">hello2 </option>
<option value="GC">hello3 </option>
<option value="GD">hello4 </option>
<option value="GE">hello5 </option>
<option value="GF">hello6 </option>
<option value="GG">hello7 </option>
<option value="GH">hello8 </option>
<option value="GI">hello9 </option>
<option value="GK">hello10 </option>
<option value="Gl">hello11</option>
<option value="GM">hello12</option>
<option value="GN">hello13</option>
<option value="GO">hello14</option>
</select>
</div>
/ group 2 /
<div class="form-group">
<label for="returnb_ori">Return scenarios group B signals</label>
<select name="returnb_ori" class="form-control" id="Site Orientation">
<option value="reb1">None </option>
<option value="reb2">congrats1 </option>
<option value="reb3">congrats2</option>
<option value="reb4">congrats3</option>
<option value="reb5">congrats4</option>
<option value="reb6">congrats5</option>
<option value="reb7">congrats6</option>
<option value="reb8">congrats7 </option>
<option value="reb9">congrats8</option>
<option value="reb10">congrats9 </option>
<option value="reb11">congrats10 </option>
<option value="reb12">congrats11</option>
<option value="reb13">congrats12</option>
<option value="reb14">congrats13</option>
</select>
</div>
Is there any chance to have drop down for group 1 and group 2 and dependent drop down for other elements as sub categories - thank you
If you don't want to use js in your dependable dropdowns, then you have to refresh the page with some query string.
You just have submit the form but use some logic to tell the form how to build itself based on previous selected options. Here is a basic example.
function dropDown($title='cat')
{
ob_start();
echo '<select name="'.$title.'">';
for($i = 0; $i < 10; $i++) {
$name = rand(100,999);
echo '<option value="'.$title.$name.'">'.$title.$name.'</option>';
}
echo '</select>';
$data = ob_get_contents();
ob_end_clean();
return $data;
}
?>
<form method="post">
<?php echo dropDown(); ?>
<?php if(!empty($_POST) && empty($_POST['subcat'])) {
echo dropDown('subcat');
?><input type="text" name="subcat" value="true" />
<?php }
?>
<input type="submit" value="<?php echo (!empty($_POST['cat']))? 'SAVE':'GET SUB'; ?>" />
</form>
Page load:
<form method="post">
<select name="cat">
<option value="cat649">cat649</option>
<option value="cat801">cat801</option>
<option value="cat974">cat974</option>
<option value="cat519">cat519</option>
<option value="cat914">cat914</option>
<option value="cat799">cat799</option>
<option value="cat968">cat968</option>
<option value="cat687">cat687</option>
<option value="cat403">cat403</option>
<option value="cat355">cat355</option>
</select>
<input type="submit" value="GET SUB" />
</form>
After submit:
<form method="post">
<select name="cat">
<option value="cat545">cat545</option>
<option value="cat442">cat442</option>
<option value="cat733">cat733</option>
<option value="cat947">cat947</option>
<option value="cat446">cat446</option>
<option value="cat327">cat327</option>
<option value="cat706">cat706</option>
<option value="cat190">cat190</option>
<option value="cat143">cat143</option>
<option value="cat254">cat254</option>
</select>
<select name="subcat">
<option value="subcat455">subcat455</option>
<option value="subcat728">subcat728</option>
<option value="subcat478">subcat478</option>
<option value="subcat883">subcat883</option>
<option value="subcat756">subcat756</option>
<option value="subcat765">subcat765</option>
<option value="subcat978">subcat978</option>
<option value="subcat589">subcat589</option>
<option value="subcat820">subcat820</option>
<option value="subcat234">subcat234</option>
</select>
<input type="text" name="subcat" value="true" />
<input type="submit" value="SAVE" />
</form>
I would like to hide an option value if one value is selected in first dropdown.
I have added below the dropdown code:
<select name="time_hour" class="span6">
<option value="00" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '00'){ echo "selected";} ?>>00</option>
<option value="01" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '01'){ echo "selected";} ?>>01</option>
<option value="02" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '02'){ echo "selected";} ?>>02</option>
<option value="03" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '03'){ echo "selected";} ?>>03</option>
<option value="04" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '04'){ echo "selected";} ?>>04</option>
<option value="05" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '05'){ echo "selected";} ?>>05</option>
<option value="06" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '06'){ echo "selected";} ?>>06</option>
<option value="07" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '07'){ echo "selected";} ?>>07</option>
<option value="08" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '08'){ echo "selected";} ?>>08</option>
<option value="09" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '09'){ echo "selected";} ?>>09</option>
<option value="10" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '10'){ echo "selected";} ?>>10</option>
<option value="11" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '11'){ echo "selected";} ?>>11</option>
<option value="12" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '12'){ echo "selected";} ?>>12</option>
<option value="13" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '13'){ echo "selected";} ?>>13</option>
<option value="14" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '14'){ echo "selected";} ?>>14</option>
<option value="15" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '15'){ echo "selected";} ?>>15</option>
<option value="16" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '16'){ echo "selected";} ?>>16</option>
<option value="17" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '17'){ echo "selected";} ?>>17</option>
<option value="18" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '18'){ echo "selected";} ?>>18</option>
<option value="19" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '19'){ echo "selected";} ?>>19</option>
<option value="20" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '20'){ echo "selected";} ?>>20</option>
<option value="21" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '21'){ echo "selected";} ?>>21</option>
<option value="22" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '22'){ echo "selected";} ?>>22</option>
<option value="23" <?php if(date('H',strtotime($time_gap[0]->time_gap_time)) == '23'){ echo "selected";} ?>>23</option>
</select>
And the second dropdown is
<select name="time_min" id="time_min" class="span6">
<option value="00" <?php if(date('i',strtotime($time_gap[0]->time_gap_time)) == 00){ echo "selected";} ?>>00</option>
<option value="15" <?php if(date('i',strtotime($time_gap[0]->time_gap_time)) == 15){ echo "selected";} ?>>15</option>
<option value="30" <?php if(date('i',strtotime($time_gap[0]->time_gap_time)) == 30){ echo "selected";} ?>>30</option>
<option value="45" <?php if(date('i',strtotime($time_gap[0]->time_gap_time)) == 45){ echo "selected";} ?>>45</option>
</select>
So, for example, If I select 00 in first dropdown, 00 should be hide in second dropdown. I am trying to get this possibility. Please help.
use JQuery and set id="time_hour" attribute for first select tag:
$("#time_hour").change(function (){
$("#time_min").find("option").css('display','')
$("#time_min").find("option[value="+$(this).val()+"]").css('display','none')
});