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.
Related
I am fetching some 1000 records precisely from MySQL using PHP in a drop-down box, data loads on page reload and it is showing in console too, but whenever I click data shows in drop-down really slowly/delay.
PS: Cannot opt for caching Redis or other.
SQL Query is like- 'select user from table' //users are 1000
PHP Script:
<select name="wname" id="Publication" class="form-control" required>
<option value=" ">Select Publication</option>
<?php
$select = "";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<? echo $row['Id']; ?>"<? if($row['Id']==$select){ echo "selected"; } ?>>
<?
echo $row['users'];
?>
<?php
}
?>
</option>
</select>
Can you use a different approach?
You must be render the dropdown as a search box. When your user starts typing, you make an ajax call to the DB, for example at start of 3rd chars.
E.g.
When your user type "sin" you populate with "sineverba" and "sinology", when user continues with "sine" you print only "sineverba" and so on.
Currently setting up a form for users to select aircraft information from. Currently, the form allows users to select an airplane and in return when the form is submitted it passes the value"aircraft type" to an API. Due to this, I cannot set the values to be the registration of the airplanes so have put this into the ID for now (open to suggestions).
<select name="type" id="plane-list">
<option selected="selected">Choose one</option>
<?php
foreach($allaircraft as $item){
?>
<option id="<?php echo $item->registration; ?>" value="<?php echo $item->icao; ?>"><?php echo $item->name; ?> - <?php echo $item->registration; ?></option>
<?php
}
?>
</select>
As you can from above these are all dynamic values which change over time so hard coding the options are not possible.
So summed up I need to pull the ID or $item->registration; instead of ICAO code. This then needs to be set as a variable so that I can call a function with this code.
Let me know if you need me to clear up anything. Don't normally questions on here so be nice.
include('connection.php');
$con=mysql_query("select * from province"); ?>
<select name="pid" style="float:left; width:135px;">
<option value="">Select Country</option>
<?php while($row=mysql_fetch_array($con)) {
$pid=$row['province_id'];
?>
<option value="<?=$pid?>" ><?php echo $row['province_name'] ?>
</option>
<?php } ?>
</select> -
<?php
$pid=$_POST['$pid'];
include('connection.php');
$con=mysql_query("select * from city where city_state ='$pid' ") or die(mysql_error()); ?>
<select name="cid" style="float:left; width:135px;">
<option value="">Select City</option>
<?php while($row=mysql_fetch_array($con)) {
$cid=$row['city_id'];
?>
<option value="<?=$cid?>" ><?php echo $row['city_name'] ?> <?php }?>
</option>
</select>
first table is country table, that coutry id is province_id,
second table is city, that Province id is city_state,
so, post value one to another dropdown
For this, you could use AJAX. In your country dropdown box, just echo all the countries in <option> elements.
Then attach a change event on this dropdown box using jQuery, so that it will take the user's input (I mean selected country) and do an AJAX request(jQuery has various shorthand methods for AJAX too) to fetch all the provinces belonging to this country. So this request will be sent to a PHP page on your site, that will pull the details from your db using the country id that the user selected.
Then the result from the AJAX request, which would be list of provinces with their IDs is dynamically appended to the second dropdown box(ie. for province). And for this province dropdown box also you would attach a change event to it, and do the similar thing like I mentioned above to fetch all the cities under this province, using AJAX.
Do a quick research on AJAX, in Google. There are lots of tutorials to help you.
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?
Im trying to create a drop down list/menu of users from my SQL database table. Could anyone help me with the code please? The code below is just for a normal list with options 1 & 2. How do i make the list retrieve all the items in a specific table using html/php/sql. Sorry im not very experienced with this, thanks in advance.
<select name="user" id="user">
<option value="a" selected="selected">1</option>
<option value="b">2</option>
</select>
there are several ways but i'll show mine
First, take the data you want to retrieve from query:
$query = mysql_query("SELECT name, id FROM users");
Then echo a select followed by a while cycle to iterate the various options:
<?php
echo "<select name='user'>";
while ($temp = mysql_fetch_assoc($query) {
echo "<option value='".$temp['id']."'>".$temp['name']."</option>";
}
echo "</select>";
?>
The result should be:
<select name="user">
<option value='1'>User name 1</option>
<option value='2'>User name 2</option>
</select>
Hope this was useful for you :)
PHP Dynamic Drop-down