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.
Related
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.
i need to populate another drop down based on previous selected drop down. please help.My code is below. Now, how i pass the previous selected value to next dropdown where clause?
<td align="right">Country</td>
<td>
<select type="text" name="Country">
<option selected value="">Country Code</option>
<?php $result=mysql_query("SELECT CountryName FROM tbl_country");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[1]</option>";}
?>
</select>*
</td>
<td align="right">Area</td>
<td>
<select type="text" name="Area">
<option selected value="">Area</option>
<?php $result=mysql_query("select Area from tbl_area where CountryName='???'");
while($row_result=mysql_fetch_row($result))
{ echo "<option value=\"$row_result[0]\">$row_result[0]</option>";}
?>
</select>*
</td>
How can i set the selected country name into the second queries where clause?
Thanks in advance.
As PHP is run on the server you will need to request the server to run the second query for you by either having the page refresh or do it via AJAX so that the page refreshes but without looking like it has.
The easiest solution would be to do it across 2 pages, if you want to do it on the one then AJAX is the way forward.
Give me a minute and i'll look for an example.
I would like to know how to submit two drop down menus w/o a submit button. I want to populate the second drop down menu on selection of the first and then be able to echo out the selection of the second drop down menu. Data for the second drop down menu is obtained from a mySQL database. I am using two forms on my page, one for each drop down menu.
<form method="post" id="typeForm" name="typeForm" action="">
<select name="filterType" onchange="document.getElementById('typeForm').submit()">
<option <?php if ($_POST['filterType'] == 'none') print 'selected '; ?> value="none">Filter by...</option>
<option <?php if ($_POST['filterType'] == 'employee') print 'selected '; ?> value="employee">Employee</option>
<option <?php if ($_POST['filterType'] == 'taskName') print 'selected '; ?> value="taskName">Task</option>
</select>
<noscript><input type="submit" value="Submit"/></noscript>
</form>
<form method="post" id="categoryForm" name="typeForm" action="">
<select name="filterCategory" onchange="document.getElementById('categoryForm').submit()">
<option <?php if ($_POST['filterCategory'] == 'none') print 'selected '; ?> value="none"></option>
<?
$count2 = 0;
echo $rowsAffected2;
while ($count2<$rowsAffected2) {
echo "<option value='$filterName[$count2]'>$filterName[$count2]</option>";
$count2 = $count2 + 1;
}
?>
</select>
<noscript><input type="submit" value="Submit"/></noscript>
</form>
I can submit the first form with no problem. It retrieves values into the second drop down menu successfully.But on selecting a value from the second menu the page refreshes and I'm left with an two unselected drop down menus. I tried echoing the $_POST['filterCategory'] and didn't get a result. I tried using onchange="this.form.submit();" in both forms and I still get the same result. Is there a way to do this without using AJAX, JQuery or any complex Javascript script? I require to this completely in PHP.
I want to avoid the second refresh but still be able to gather the $_POST[''] data from the second selection.
use the same form for both selects
You have two ways of doing that.
You can either submit the form using AJAX; that will prevent the whole page from refreshing, and hence, losing the data in both select elements; that is also the coolest way to have it done.
OR
Using a single form, when both the first and second select elements are submitted, use their values to re-create the select elements.
The cleaner way to do it is still the first option; at least, that's what I'll use if I have to do it.
I'm working on a forum software right now, and I'm doing the administration panel. I have a part with a dropdown box of all the current forums. It works, but it doesn't show the first forum in the table.
This is my code
$select_forums = "SELECT id,name FROM forums";
$run_select_forums = mysql_query("$select_forums");
$row = mysql_fetch_array($run_select_forums, MYSQL_ASSOC);
<form action='index.php' method='post'>
<select>
<?php while ($row = mysql_fetch_array($run_select_forums, MYSQL_ASSOC)) { ?>
<option value="<?php echo $row["id"] ?>" name="selected"><?php echo $row["name"] ?>
</option>
<?php } ?>
</select><br/>
<input type='submit' name='submitdelete' value='Delete' />
</form>
Also I wondered how I can retrieve what item was selected from the list?
It isn't displaying the first one because you're calling mysql_fetch_array() once before your while loop begins:
$select_forums = "SELECT id,name FROM forums";
$run_select_forums = mysql_query("$select_forums");
// Don't call mysql_fetch_array() here...
$row = mysql_fetch_array($run_select_forums, MYSQL_ASSOC);
Additionally, you should wrap these values in htmlentities() in the attributes and htmlspecialchars() outside the attributes to escape them properly as HTML attributes (as well as against XSS attacks)
<option value="<?php echo htmlentities($row["id"], ENT_QUOTES) ?>" name="selected"><?php echo htmlspecialchars($row["name"]) ?>
</option>
I understand your $row['id] is likely an integer not needing escaping, but it is a good habit to get into.
Your first $row = mysql_fetch_array($run_select_forums, MYSQL_ASSOC); pulls the first row from the table. When you start your while loop the mysql_fetch_array picks up with the next row. The easiest thing to resolve that problem is just not to make that first call.
To retrieve the selected element, give your select tag a name attribute, and it will appear in the $_POST array in the server script that's accepting the submission from the client - in this case index.php, thusly:
with html like
<select name="forum">
<option>...</option>
...
</select>
in php, $_POST['forum'] will contain the value between the <option></option> tags of the selected item.
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.