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>
Related
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'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>
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
i have a php page that is a sort of submition form with 3 dropdown sections, that have to send the selections from the dropdowns when you click a button. The form is as follows:
<div class="statsValPlate">
<label>Main Score</label>
<select name="scoreSelect">
<option value="301">301</option>
<option value="501">501</option>
<option value="701">701</option>
<option value="1001">1001</option>
</select>
</div>
<div class="statsValPlate">
<label>Legs Format</label>
<select name="legSelect">
<option value="3">2 of 3</option>
<option value="5">3 of 5</option>
<option value="7">4 of 7</option>
<option value="9">5 of 9</option>
</select>
</div>
<div class="statsValPlate">
<label>Sets Format</label>
<select name="setSelect ">
<option value="3">2 of 3</option>
<option value="5">3 of 5</option>
<option value="7">4 of 7</option>
<option value="9">5 of 9</option>
</select>
</div>
<form method="get" action="scoreboard.php">
<button id="submit" name ="submit" class="push_button red">Start Game</a><br>
</form>
<?php
$_SESSION['gameScore'] = $_POST['scoreSelect'];
$_SESSION['legScore'] = $_POST['legSelect'];
$_SESSION['setScore'] = $_POST['setSelect'];
?>
and i want for instance the selection of the first form has to show up as a label value here:
<label class="scorePlate" id="num1" value="<?php echo $_SESSION['gameScore']?>">
<!--<?php echo $_SESSION["player1Score"]?>-->
<script>
document.write(new_score_player_1);
</script>
</label>
but for some reason it doesn't work. What's the problem
First, start the session - session_start().
Second, put all those selects inside the form. When they are outside the form, the values selected won't be sent on form submission.
Second, make the form's method POST, not GET.
Your code should look something like this:
<?php
session_start();
if (isset($_POST['submit']) {
$_SESSION['gameScore'] = $_POST['scoreSelect'];
$_SESSION['legScore'] = $_POST['legSelect'];
$_SESSION['setScore'] = $_POST['setSelect'];
}
?>
<form method="POST" action="scoreboard.php">
<div class="statsValPlate">
<label>Main Score</label>
<select name="scoreSelect">
<option value="301">301</option>
<option value="501">501</option>
<option value="701">701</option>
<option value="1001">1001</option>
</select>
</div>
<div class="statsValPlate">
<label>Legs Format</label>
<select name="legSelect">
<option value="3">2 of 3</option>
<option value="5">3 of 5</option>
<option value="7">4 of 7</option>
<option value="9">5 of 9</option>
</select>
</div>
<div class="statsValPlate">
<label>Sets Format</label>
<select name="setSelect ">
<option value="3">2 of 3</option>
<option value="5">3 of 5</option>
<option value="7">4 of 7</option>
<option value="9">5 of 9</option>
</select>
</div>
<button id="submit" name ="submit" class="push_button red">Start Game</a><br>
</form>
And on the other page:
<?php
session_start();
?>
<label class="scorePlate" id="num1" value="<?php echo $_SESSION['gameScore']; ?>">
<!--<?php echo $_SESSION["player1Score"]?>-->
<script>
document.write(new_score_player_1);
</script>
</label>
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>