How to choose a <select> option value automatically from an URL parameter? - javascript

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

Related

javascript get text option instead value

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>

javascript enable disable in every row table withphp array

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..

Display selected dropdown value from database (i.e.mysql)

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>

Bring the Options from database table in 2nd Select box based on the value selected in 1st Select Box

I searched a lot about this topic and found difficult approaches mostly not working. I have 2 Select boxes (class & student) which should be dynamically generated. After selecting the Class, the 2nd Select Box should show the Option Values from the Students Table.
<form action="" class="form-horizontal" method="POST">
<h4>Issue Book: </h4><hr>
<label class="control-label" for="class">Class</label>
<select id="class" name="class" class="form-control">
<option value="">Select Class</option>
<?php
$class_query = "SELECT * FROM `classes` ";
$class_result = mysql_query($class_query);
do{
$class_row = mysql_fetch_array($class_result);
if($class_row){
$class_id = $class_row['id'];
$class_name = $class_row['class'];
?>
<option value="<?php echo $class_id; ?>"><?php echo $class_name; ?></option>
<?php
}
}while($class_row);
?>
</select>
<label class="control-label" for="student">Student</label>
<select id="student" name="student" class="form-control">
<option value="">Select Student</option>
</select>
<!-- Submit -->
<button type="submit" name="issue_book">Issue Book</button>
</form>
Kindly help with this, I have no knowledge of JS or JQuery. I shall be very thankful.
first this is your html code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form action="" class="form-horizontal" method="POST">
<h4>Issue Book: </h4>
<hr>
<label class="control-label" for="class">Class</label>
<select id="class" name="class" class="form-control" onchange="getStudentOption(this)">
<option value="">Select Class</option>
<option value="class1">Class 1</option>
<option value="class2">Class 2</option>
<option value="class3">Class 3</option>
<option value="class4">Class 4</option>
</select>
<label class="control-label" for="student">Student</label>
<select id="student" name="student" class="form-control">
<option value="">Select Student</option>
</select>
<!-- Submit -->
<button type="submit" name="issue_book">Issue Book</button>
</form>
added a onchange function
<script>
function getStudentOption(input) {
var class_name = input.value;
$.ajax({
type: "GET",
data: 'class=' + class_name,
url: 'demo.php',
dataType: 'text',
success: function (text) {
$('#student').html(text);
},
});
}
</script>
here demo is the file name which file give you all student data so you can change this file name and path also
and this is your demo file content
<?php
if (isset($_GET) && ($_GET['class'] != '')) {
/*$class_value = $_GET['class'];
$query = "SELECT * FROM students WHERE class_name=" . $class_value;
$result = mysql_query($query);
$total_refs = mysql_num_rows($result);
if ($total_refs > 0) {
?>
<option value="">Select Student</option>
<?php
while ($line = mysql_fetch_array($result)) {
$student_name = $line['student_name'];
$student_id = $line['student_id'];
?>
<option
value="<?php echo $student_id; ?>"><?php echo $student_name; ?></option>
<?php
}
?>
<?php
} else { ?>
<option value="">Select Student</option>
<?php
}*/?>
<option value="">Select Student</option>
<option value="1">Student 1</option>
<option value="2">Student 2</option>
<option value="3">Student 3</option>
<?php }
?>
i have added some demo content you can update data which code i have added, i am not sure which your table structure so just update condition value and filed value

how do you make a dependent drop down list in php

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>

Categories