HTML Options from SQL Database - php

I have a HTML DataList input box, and I would like to have the options in the DataList to be things from my SQL database. I have been trying to make this work, but for some reason I cant. Does anyone know how this could be achieved?
Here is my code:
<?php
include_once 'connect.php';
$sql="select * FROM table";
?>
<form>
<input list="list" name="name">
<datalist id="datalist">
<?php
foreach ($dbo->query($sql) as $row) {
echo "<option value="$row[name]"/>";
}
?>
</datalist>
<input type="submit">
</form>
The option I get with this is literally $row[name]
Could someone tell me what I'm doing wrong?

Datalist id has to match the input list attribute and change the loop to "<option value=" . $row['name'] . "/>"

Related

How to make the Html dropdown list selected value to be printed as a CSV?

I have this html code and I want when I select name from table category with export.php to export all the data only for this category name.Can you help me please?
<select>
<option disabled selected></option>
<?php
include "config.php"; //
$products_cat = mysqli_query($con, "SELECT name From category");
while ($product_categoty = mysqli_fetch_array($products_cat)) {
echo "<option value='" . $product_categoty['name'] . "'>" . $product_categoty['name'] . "</option>";
}
?>
</select>
<form method="post" action="export.php">
<input type="submit" name="expo" value="Export Category">
</form>
</form>```
I think Select should be inside the form and please follow these steps to export data as csv.
https://www.codexworld.com/export-data-to-csv-file-using-php-mysql/

HTML PHP: Why do two identical select elements produce different HTML displays?

Could someone kindly help out with this rather basic issue: In a simple HTML form (functionally all good) two select elements are included.
Code:
<select id="dept" name="dept" required>
<?php
foreach($stddept as $item0) {
echo "<option value='$item0'";
if ($_POST['dept'] == $item0) echo 'selected="selected"';
echo ">$item0</option>";
}
?>
</select>
<br><br>
<select id="lev" name="lev" required>
<?php
foreach($stdlev as $item1) {
echo "<option value='$item1'";
if ($_POST['lev'] == $item1) echo selected="selected"';
echo ">$item1</option>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>`
These two drop-down boxes behave differently in that only the second one honours the 'required' attribute. The first one can be submitted blank, which is obviously not right.
They also appear differently (see image) in that the first displays the first option in the list (from MySQL DB) but the second displays a blank. I have checked everything I could to see what else could cause the difference - without any joy.
Does anyone know what I am missing here? Thank you.
You can do this using jquery/javascript by getting the id of the first selected value and then select the result for the second box based on that id.
I think the problem with the code ( as it is shown ) is that you are incorrectly echoing the option in the second dropdown - it is missing quotes. You can streamline the code somewhat like this:
<select id="dept" name="dept" required='required'>
<?php
foreach($stddept as $item0){
$selected=$_POST['dept'] == $item0 ? " selected='selected'" : "";
echo "<option value='$item0'{$selected}>";
}
?>
</select>
<br /><br />
<select id="lev" name="lev" required='required'>
<?php
foreach($stdlev as $item1){
$selected=$_POST['lev'] == $item1 ? " selected='selected'" : "";
echo "<option value='$item1'{$selected}>";
}
?>
</select>
<input type='submit' id='submituser' name='submituser' value='Submit'>

Posting the value of a drop down menu to a PHP page via a HTML form

I'm creating a form in HTML which sends the question a user is asking, and the topic which the question should appear under to a PHP page. The topic names are pulled from a MySQL database using PHP.
I want to post the value of the drop down menu (the topic that the user has chosen) along with a HTML form to a PHP page. Here is my form code:
<form action="add_question.php" method="post">
Question:<input name="question_text" type="question"><br>
<select name="topic_name">
<option>Topic</option>
<?php
// Get each topic name from the database
include "connect_database.php";
$topicQuery = "SELECT topic_name FROM topics
ORDER BY topic_name";
$result = $conn->query($topicQuery);
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
// Make topic an option in drop down box
echo '<option>' . $row["topic_name"] . '</option>';
}
}
// Close connection
mysqli_close($conn);
?>
</select><br>
<button type="submit">Submit</button>
</form>
When this is posted to add_question.php, $_POST['topic_name'] has no value. I think there's a problem with my form, although I can't see what. Any help would be great.
Thanks!
You have to assign a value to your options like :
echo '<option value='.$row["topic_id"].'>' . $row["topic_name"] . '</option>';
Note: topic_id an exmaple of a value you may use any other value
You need to add value attribute to all your options
<option value="some value">some value</option>
<form action="add_question.php" method="post">
Question:<input name="question_text" type="question"><br>
<select name="topic_name">
<option>Topic</option>
<?php
// Get each topic name from the database
include "connect_database.php";
$topicQuery = "SELECT topic_name FROM topics
ORDER BY topic_name";
$result = $conn->query($topicQuery);
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{ ?>
<option value="<?=$row["topic_name"]?>"><?=$row["topic_name"]?></option>
<?php
}
}
// Close connection
mysqli_close($conn);
?>
</select><br>
<button type="submit">Submit</button>
</form>
Try the following:
<form action="add_question.php" method="POST">
Question: <input name="question_text" type="question"><br/>
<select name="topic_name">
<option>Topic 1</option>
<option>Topic 2</option>
<option>Topic 3</option>
</select>
<input type="submit" value="submit">
</form>
Note that you don't need a value as mentioned above and you are going to fill your options using your database of course. And notice the input type="submit".
Then on the page add_question.php do the following:
echo $_POST["question_text"];
echo "<br/>";
echo $_POST["topic_name"];
That prints out the correct items when I use it.

Select from drop-down menu and reload page

I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
I need to clarify that this will change that current data
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
I think form may be better, for example
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
$peer-id = $_POST['peer-id'];
Hope helps!
apply this code in select tag hope this works
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
I agree in using form, and with this you can echo back onto the page with a submit button (code tested):
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>

changing from dropdown menu to mutli select open box

I have a drop down menu that I want to change to a multiple select box. The code below is working if you only select 1 option (the way I had it before), but of you select 2 it will only show 1 of the two, how can I make it show both options selected, here is the code:
<?php $makes = array("volvo","Saab","Opel","Audi","BMW") ?>
<form method="post" name="store" action="<?php $_SERVER['PHP_SELF'] ?>" >
<select multiple="multiple" name="cars">
<?php foreach ($makes as $make){echo "<option value=\"$make\">". $make ."</option>"; $vehicles = $_POST['cars'];} ?>
<input name="submit" type="submit">
</select>
</form>
<?php
if($_POST['submit']){
echo $vehicles;
}
?>
</body>
</html>
I hope I read this correctly that you would like to retrieve an array of results from the HTML multi select box.
By the way your code snippit is not correct; the HTML <form> closing tag should be after your closing <select> tag and I'm not sure why you have the following in your PHP for() loop:
$vehicles = $_POST['cars'];
You will want to make the HTML tags' name attribute an array as follows (Note I did not test this code):
<select multiple="multiple" name="cars[]">
<?php
foreach ($makes as $make) {
echo "<option value=\"$make\">". $make ."</option>";
}
?>
</select>
<?php
if($_POST['submit']) {
print_r($_POST['cars']);
}
?>
PHP.net - How do I get all the results from a select multiple HTML tag?
It isn't completely clear from your question but I think you mean the following bit of code only echoes one value:
if($_POST['submit']){
echo $vehicles;
}
To turn your selected cars into an array you need to add [] onto the end of the name:
<select multiple="multiple" name="cars[]">
Then to echo each of the selections you can use a foreach loop:
foreach ($_POST['cars'] as $car)
echo $car.'<br />';
you can try with if($_POST['submit']){
print_r($vehicles);
}
please let me know if any issue then..

Categories