HTML dropdown list not showing 1st item in table - php

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.

Related

Dyanmic dropdown data is loading really slow in PHPusing MySQL

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.

Is it possible to send an array in a POST, or assign multiple values in one input tag, and how?

I want to send more than one value in the POST but use only one type of input tag.
For example, is it possible to set the value of the option tag to both $data['username_id] and $data['name'] and send it via the POST
<select name="owner" id="owner">
<option value="NULL" selected="selected"></option>
<?php
$sql = 'SELECT * FROM users'
. ' ORDER BY name';
$query = mysqli_query($connect,$sql) or die (mysqli_error($connect));
while ($data = mysqli_fetch_array($query))
{
?>
<option value="<?php echo $data['user_id']; ?>"><?php echo $data['name']; ?></option>
<?php
}
?>
</select>
I can't seem to retrieve on the other end. I have tried:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
But still no luck.
Any help would be useful.
For example, is it possible to set the value of the option tag to both $data['username_id]' and $data['name'] and send it via the POST
Yes, this can be done, carefully.
Working with your example:
<option value="<?php echo $data['user_id','name']; ?>"><?php echo $data['name']; ?></option>
Can be simply rewritten as:
<option value='<?php echo $data['user_id']."--".$data['name']."'>"
.$data['name']; ?></option>
At the other end:
On the form receiver PHP page:
$parts = explode("--",$_POST['owner']);
/***
* $parts[0] = user_id
* $parts[1] = name
***/
BUT as you are grabbing the data from a database row anyway, this is fairly pointless, you might as well JUST transport the user ID and simply use it on the receiving end to grab the 'name' data value from the Database.
There is also potential issues if the splitter (--) appears more than once, so be carefully to choose a splitter that does not apper in either of the values you are trying to send.
Working Freehand:
<input name="whatever[]" value="one">
<input name="whatever[]" value="two">
The square bracket means that the data passed in $_POST will be an array of values.
As pointed out in comments by Kainaw you can also simply use the multiple selection to reach the same effect in your <select> input.

Dynamic Dropdown List PHP MYSQL

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'?

Do I still have to use JQuery to populate my second drop down menu even if the values are the same as the first drop down menu?

At the moment, I am trying to create two drop down menus that will act as time intervals. My goal is to set it up where if the user selects a year in the first drop down menu, then the next menu will automatically populate with values that are >= the first menu selection. Here is both drop down menus that gather years by querying to my database.
<p class = 'year'>
<label for="year">
Please select a year
</label>
<select name= 'year'>
echo "<option value= ''> </option>";
<?php
$query = "select distinct year from table";
$result = $conn->query($query);
while($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
</p>
<p class = 'year'>
<label for="year">
Please select a year
</label>
<select name= 'year2'>
echo "<option value= ''> </option>";
<?php
$query = "select distinct year from table";
$result = $conn->query($query);
while($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
</p>
Both of these drop down menus gather years from my database. Instead, I want my year2 menu to grab only the years >= the year value that the user selects in the first drop down menu. My question is how do I go about doing this? Do I still have to create three separate files, JQuery file, function file, and this file, or, is there a simpler way to go about this? Any help would be greatly appreciated.
I would try to convert all that logic into a single query that returns all the data you need at once. Then pass it to a JavaScript variable and play around 100% client-side. No need to hammer your DB constantly for something this simple.

Can I pass Ajax var result to PHP code?

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.

Categories