I have a dropdown menu with selected options from my database. They are linked to a table which when submit is pressed will display the table with results from the dropdown. e.g if the dropdown states snake, a submit will show me a table with just snakes. When I press the back browser button however i am unable to see previous results. e.g if i click snake then deicide to click crocodile , if i then press back browser button i cannot see the table displaying snakes. I want to be able to click snake then crocodile then when going back be able to see previous selected results e.g table with snakes.
<option selected='true' disabled='disabled'>Select option </option>
<option value='All'>All</option>
<?php
$s = "SELECT DISTINCT species,status from animal";
$result = $con->query($s);
if($result = mysqli_query($con, $s)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
if($row['status']=='rare'){
$output = "<option value='".$row['species']."'"; //keeps select option on dropdown the same upon page refresh
if($_GET['fetching'] == $row['species']){
$output .= " selected='selected'";
}
$output .= ">".$row['species']."</option>";
echo $output;
if($_GET['fetching']){
// $output .= " selected='selected'";
$_SESSION['animal6'] = $_GET['fetching'];
header("Location: room.php?pn=1"); //added 12.04.22
exit;
}
}
}
}
}
?>
</select>
<input id='submit' name='submit' type='submit' value='submit' ></input>
</form>
Related
With a SELECT query I obtain an array of 7 names, create a dropdown list and each name is a table which is then hidden.
I want to click on the name in the dropdown list and display the table.
I am not receiving any errors but the following code only displays the first table in the array no matter what option is selected. I have little knowledge of javascript and the function has been cobbled together from various queries on the site.
Javascript:
function changeOpt(clicked) {
var x = document.getElementById("optChange");
var optVal = x.options[x.selectedIndex].value;
for(var i =0; i<x.length;i++){
if(optVal = document.getElementById('datath'))
document.getElementById('data').style.display = 'block';
}
}
HTML/PHP:
while($row = mysqli_fetch_array($result))
{
$array = $row;
//echo '<pre>'.print_r($array).'</pre>';
echo '<table id="data" style="display:none;">';
echo '<tr><td>You searched for:</td></tr>';
echo '<tr><th id="datath">'.$row[0].'</th><th>'.$row[1].'</th><th>'.$row[2].'</th>';
echo '</tr>';
echo '</table>';
$option .= '<option value = "'.$row['1'].'">'.$row['1'].'</option>';
}
?>
<form method="post" action="#" id="my_form" >
<select id="optChange" name="opt" onchange="changeOpt(this.id)">
<option><--select a name--></option>
<option><?php echo $option; ?></option>
</select>
<input type="button" name="submit" value="submit" >
</form>
Am I completely off-beam ? Can someone give me some guidance please to get my code straight.
I am stuck in getting value from drop down. Drop down is dynamically filled from sql server database.
Dropdown 1 displays product name and it is dynamically filled.
Dropdown 2 displays environment name and it is filled by HTML.
I am getting value of environment but not product.
Please help me. Thanks
Here is the code:
<form action="" method="post">
//Dropdown 1
<p>Product Name:
<select name="productname">
<option value="">Select</option>
<?php
if( $conn )
{
$sql_dd = "SELECT ProductName from Product";
$stmt = sqlsrv_query( $conn, $sql_dd );
if( $stmt === false) {die( print_r( sqlsrv_errors(), true) );}
$rows = sqlsrv_has_rows( $stmt );
if ($rows === false)
echo "There is no data. <br />";
else
{ while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
echo "<option value=''>".$row['ProductName']."</option>";
}
}
?>
//Dropdown 2
Client Type:
<select name="environment" style="width: 10%;">
<option value="">Select</option>
<option value="en1">en1</option>
<option value="en2">en2</option>
<option value="en3">en3</option>
</select>
<input type="submit" class="theme-btn" value="Search" name="submit"/>
<?php
if(isset($_POST['submit']) )
{
$productname = $_POST['productname'];
$environment= $_POST['environment'];
echo "productname: ".$productname." environment: ".$environment;
}?>
The value is not added
this:
echo "<option value=''>".$row['ProductName']."</option>";
should be
echo "<option value='" . $row['ProductName'] . "'>". $row['ProductName'] ."</option>";
Some Background information:
I have a table with two fields, TECHNAME and TECHCOLOR
What I am trying to do is:
Have a SQL generated Drop down menu based on TECHNAME (DONE)
Have TECHCOLOR update in text box when TECHNAME is selected (ISSUE IS HERE)
What is wrong
Currently, the textbox is showing TECHNAME insead of TECHCOLOR
Code
JavaScript:
<script type="text/javascript">
function load_value(value)
{
document.getElementById("test").value=value;
}
</script>
HTML/PHP:
<table>
<form method="post" action="">
<?php
$select_box='<select name="edittech" id="edittech" onchange="javascript:load_value(this.value);">';
$input="";
$result = $conn->query("select * from techs");
while ($row = $result->fetch_assoc()) {
$select_box .='<option id="name" value="'.$row["TECHNAME"].'">'.$row['TECHNAME'].'</option>';
}
$input ='<input type="text" name="test" id="test" value="" />';
echo $select_box."</select>";
echo $input;
?>
Thanks in advance! :D
When you submit the form, is the value for your edittech dropdown list used at all? If it isn't then you can simply change the value part of your dropdown to be TECHCOLOR and it will still show TECHNAME on the dropdown.
while ($row = $result->fetch_assoc()) {
$select_box .= "<option id=\"name\" value=\"{$row['TECHCOLOR']}\">{$row['TECHNAME']}</option>";
}
<fieldset>
<legend>Available Areas</legend>
<select id="availableAreas">
<?php
$sql = "SELECT * FROM workArea";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$area = $row['name'];
echo "<option value='$id'>$area</option>";
}
?>
</select>
<input id="addArea" type="button" value="Add area" /><br />
<select id="workAreas" style="min-width: 200px" required multiple name="workAreaId"></select>
</fieldset>
The code above produces this output:
I haven't put the JavaScript in as it's not relevant to the question but clicking 'Add area' would add the area into the <select> element (the multiple list below the drop down).
When the user submits the form I need to send all of the <option>s that the user added to the list. Is there a way I can do this?
Thanks
Assuming you are using jQuery:
$('#yourForm').submit(function() {
$('#workAreas option').prop('selected', 'selected');
});
This should select all items in your multiple select when the form is submitted.
Another option would be to add a hidden input field for every Option. When you add [] to the name of the field it will be available on the server side as an array.
<input type="hidden" name="workareas[]" value="X" />
I have a text input (z-index:1) which i put in front of the drop down box. So that the user can input text and also choose from drop down list. I did hide the down arrow button for the drop down list. So, I want when user click on the text input, the drop down listing will automatic display. I plan to used Jquery to display the listing, but i don't know what function that I need to use.
Here my code
<span id="plantimefromdd" style="position:relative;">
<input id="textin1" name="textin1" type="text" style="width:100px;position:absolute;top:-4px;left:13;z-index:1;padding:3;margin:0;opacity:0;" value="<?php echo $todisplay; ?>" onclick="this.style.opacity=1;">
<?php
//display dropdown time for from interval 5 minutes
$start = strtotime('12:00am');
$end = strtotime('11:55pm');
echo '<select name="plantimefromdd" style="width:100px;height:26px;-webkit-appearance:none;border-width:2px;border-color:#D8D8D8;" onchange="$(\'input#textin1\').val($(this).val());">';
for ($i = $start; $i <= $end; $i += 300){
$timerfrom = date('H:i', $i);
if($todisplay == $timerfrom) {$temp = "selected";} else {$temp = "";}
echo '<option '.$temp.' >' . $timerfrom ;
}
echo '</select>';
?>
</span>
Thank you for your helping.
First add id to your drop-down and then for the text-box make the following from:
onclick="this.style.opacity=1;"
To:
onclick="$('#dropdown').show()" onblur="$('#dropdown').hide()"