I have the following code:
<td>Province</td>
<td>
<select name="prov">
<?php
$sql1 = mysqli_query($connect, "SELECT * FROM Provincetbl order by Prov_Desc desc");
while ($row1 = $sql1->fetch_assoc()){
?>
<option value="prov1"><?php echo $row1['Prov_Desc']; ?> </option>
<?php
}
?>
</select><br><br></td></tr>
<td>Distrect</td>
<td>
<select name="dist">
<?php
$sql2 = mysqli_query($connect, "SELECT * FROM Distrecttbl order by Distrect_Desc desc");
while ($row2 = $sql2->fetch_assoc()){
?>
<option value="dist1"><?php echo $row2['Distrect_Desc']; ?> </option>
<?php
}
?></select></td></tr>
There are two listboxes prov and dist. I have been trying to figure out how to read the value of either listbox (i.e get it in a variable). I have found a post which states that the value of a listbox is read after the form is posted (in the variable $_Post). However, I am dealing with a listbox which is not sending anything to the server, so why is the value of the listbox read after the form is posted?
Second, I do not have a form element in my page to begin with..?
Other posts have stated that I can read the value of listbox in a variable $prov or $dist. However I cannot seem to echo the contents of these variables to the screen.
Thank you for your help.
I think all the options in the prov listbox will have the same value that you've wrote 'prov1' and the second listbox will have dist1 in all the options
What I recommande you to do is the following :
<option value="$row1['Prov_Desc']"><?php echo $row1['Prov_Desc']; ?> </option>
and for the second one use this :
<option value="$row2['Distrect_Desc']"><?php echo $row2['Distrect_Desc']; ?> </option>
Related
I am a complete novice but trying to design my first site in Wordpress. I am trying to retain the value of a dropdown that pulls values from a database after a form is submitted. I have searched many questions and tried many different ways of trying to write selected="selected" in my code but none of them seem to work. Please help.
Here is my code that just pulls from the database but isn't trying to retain the selected value:
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
?>
<option value="<?php echo $ddrow->Field1; ?>"><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
How can I add the code to keep the value selected after form submit? Any help much appreciated, thank you.
Try this. It checks if box1 was submitted. Then it compares that value to the items in the options loop. A match will set $selected to the correct attribute, otherwise it will stay an empty string by default. (NB: if form is using get method, then change $POST to $_GET)
<select name = "box1" class="searchbox">
<option value = "">All Values</option>
<?php
global $wpdb;
$ddresult = $wpdb->get_results("SELECT Field1 FROM pc_table ORDER BY Field1 ASC");
foreach($ddresult as $ddrow) {
$selected = '';
if(isset($_POST['box1'])){
if($ddrow->Field1==$_POST['box1']){ $selected = 'selected="selected"'; }
}
?>
<option value="<?php echo $ddrow->Field1; ?>" <?php echo $selected; ?>><?php echo $ddrow->Field1; ?> </option>
<?php
} ?>
</select>
This example of course does not include if you want to save the submitted value to the database and then re introduce it in the output at a later time. If that's what you would like to know, then please leave a comment.
I am new to PHP and trying to make dynamic list box using SQL
<label> Select Sport</label>
<select name = "Sport">
<option value = "">Select Sport</option>
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"></option>
<?php
}
?>
</select>
But its printing BLANK space
Try this:
<select name="">
foreach($result as $Sport){
?>
<option value ="<?php echo $Sport['Sport']; ?>"><?php echo $Sport['Sport']; ?></option> // The value between <option> value </option> is the value which is to be shown in the dropdown, without this it is showing blank
<?php
}
</select>
You probably shouldn't use SELECT *, but since you're learning, I digress. Also, I personally have a hard time reading a lot of opening and closing php tags scattered throughout plus they often give weird results than what you're expecting, so this is how I'd write it.
<?php
$all = "SELECT * FROM EventTable";
$result = $pdo->query($all);
foreach($result as $sport)
{
echo "<option value =" . $sport['Sport'] . ">" . $sport['Sport'] . "</option>";
}
?>
The blank spaces indicates that you are actually hitting the loop and iterating, but the values for that array item are null. Are you sure the field name isn't 'sport' instead of 'Sport'?
currently i am working on a system. here i have a drop down list and its values are populating from database. now what i need is when ever some one selects a record from the list the selected value should be get stored in a session variable which ll get displayed in an another page. can we do so. i am coding in php.
my code is:
<td align="center">
<?php
$sql_dep=" Select * from places_tbl";
$row_dep = $db->Execute($sql_dep);
$total_dep = $row_dep->RecordCount();
?>
<select name="place" class="txtbx08" id="place">
<option value="">--Please Select City--</option>
<?php if($total_dep>0) {
while(!$row_dep->EOF)
{
?>
<option value="<?php echo $row_dep->fields["place_id"];?>">
<?php echo ucfirst($row_dep->fields["place_name"]); ?>
</option>
<?php
$row_dep->MoveNext();
}}
?>
</select></td>
I'm not sure how you're retrieving the selected value (assumingly through POST), but the normal procedure would be:
session_start();
$_SESSION['place'] = $_POST['place'];
I have a form that stopped sending some field values after I added AJAX to the mix.
<select name="showId" id="showId" onChange="getClasses('findclasses.php?showId='+this.value)">
<option value="">Select a Show</option>
<?
$sql = "select * from shows order by ShowName";
$result = mysql_query($sql);
while ($show = mysql_fetch_array($result)) { ?>
<option value="<?=$show['Id']?>"><?=$show['ShowName']?></option>
<? } ?>
</select>
<div id="classdiv"> //contents reconstructed with AJAX when the show changes above
<select id="classId" name="classId">
<option value="">Select Class</option>
</select>
</div>
When the show changes, an AJAX function is called, and the contents of the classdiv are replaced with another select field (also named classId) that has the classes associated with the show.
After I click submit, I checked the $_POST variable, and classId was not in the list but the other form fields were. Any ideas on why and how to fix it?
Please check that when the contents of classdiv are replaced by ajax the all the option with select tag having their different values or not?
Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
Greetings.
I am coding a basic form that uses a SELECT list I populate from my database.
However, my user needs the form to be dynamic and wants to be able to select either MasterTable1 or MasterTable2 or MasterTable3...
Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...
I can output <div id='txtHint'></div> in my page and it shows the correct table name that was picked.
But how do I pass the correct table name to my query that will populate my SELECT list???
I tried
<select name="DBFilename" id="DBFilename" size="0">
<option value="">Select Filename</option>
<?php
$sql="select distinct filename from "."<div id='txtHint'></div>";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option>
<?php } ?>
</select>
But to no avail. This is confusing since I can do this...
...
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gettable.php?q="+str,true);
xmlhttp.send();
}
</script></head>
<body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)">
<option value="">Select Data Table</option>
<option value=""> </option>
<option value="MasterTable1">up to 31 days old</option>
<option value="MasterTable2">62 days old</option>
</select>
</form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
</body></html>
And the name of my table will be displayed in the SELECT list 'tablist'.
How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!
(Pastebin =>form code)
m8, ajax is mainly used for user experience and populating a select list is so easy mode that it shouldnt be bothered with ajax in the first place!
You should use ajax if you want to use some methods on user submitted data and create an illusion of seamless data exchange between the server and the client, and based on the return results render a corresponding view or an element of the view.
unless you load every element of the view with ajax, you should populate your html with php from the start!
<select name="DBFilename" id="DBFilename" size="whatever since style belongs to css">
<option selected="selected">Select Filename</option>
<?php
$sql="SELECT DISTINCT filename from 'wherever'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['filename']; ?>"><?php echo $row['filename'];?>
</option>
<?php } ?>
</select>
Create a separate php script which returns a list of select options -> values depending on the table name given to it. You must remember to protect against sql injection. Then use ajax to retrieve the list and insert it into the select.