I have a trouble about select option value choosing with PHP
Multiple select box is showing for state and city. Jquery loads as ajax method for database. And I need to reach "option value" inside value. I will show the problem inside below codes.
<?php
function load_country() {
include 'includes/db.php';
$sql = "SELECT * FROM ilce ORDER BY name";
mysqli_set_charset($con, "UTF8");
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$output .= '<option name="' . $row['name'] .'" value="' .
$row["ilce_id"].'">' . $row["name"]. '</option>';
}
return $output;
}
?>
<select class="form-control" name="state" id="state">
<option>Choose State</option>
<?php
echo load_country();
?>
</select>
<?php
$state = mysqli_real_escape_string($con, $_POST['state']);
echo $state;
?>
<option value="5">New York</option>
$state is showing for me Example : "id" => 5
Thus, I want to reach "New York", inside value.
Please help me
Thanks
The text content of an <option> element is not posted to the server, only its value.
If you're inserting the selected state into a database (like in a user table), you might just insert the numeric state ID and join the two tables when you need to display the information.
Alternatively, you can fetch an array of all states indexed by their IDs and then reference that array:
while ($row = mysqli_fetch_array($result)) {
$allStates[$row['ilce_id']] = $row['name'];
}
$stateId = $_POST['state'];
$thisState = $allStates[$stateId];
echo $thisState;
Or just fetch the one row that you need by its ID, depending on what you need.
A less desirable solution is to set the <option> value to the row's name rather than its ID:
$output .= '<option value="' . $row["name"] . '">' . $row["name"] . '</option>';
But that seems to defeat the purpose of using a relational database.
Also see Get Text From Tag Using PHP.
Related
<?php
$get = mysqli_query($conn, "SELECT supplier.*,product.* FROM
supplier,product ORDER BY supplier.supplierid ASC");
$option = '';
while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC)) {
$option .= '<option value = "' . $row['productname'] . '">' .
$row['suppliername'] . '</option>';
}
<label style="margin-left:25px;margin-bottom: 10px; ">Supplier:
</label><select style="margin-bottom:10px ;margin-left: 50px;width:
200px"> <?php echo $option; ?></select>
Hello Maam And Sir I have A problem About that data Output In a Select Box. The thing that happen when i use this code or use two multiple table, the data output also double but if 1 table it is working the way i want. But I don't want to have a dirty codes that why i to put in a single query.
I am trying to create a dropdown menu that will have as options certain fields recovered from a database using a for loop.
I am doing the following:
for ($article_id=1; $article_id <=30; $article_id++)
{
$sqltitles = "SELECT title FROM articles WHERE article_id='$article_id'";
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
$row = mysqli_fetch_row($cursor);
$titles = $row[0];
echo $titles;
echo "</br>";
}
Which draws all the titles from the database and shows them one at a time.
Is there any way to make all those titles appear as options to a dropdown menu, so the user can select which one to read?
Something like this should work. I made a modification to how the query is working. If you specify article IDs for a range of articles in your query rather than just one specific ID, you should be able to execute only one query instead of one for each ID you want to retrieve. Regardless of whether or not you decide to use the approach I suggested, the syntax for creating the dropdown menu should be the same.
<select>
<?php
$sqltitles = "SELECT title FROM articles WHERE article_id BETWEEN 1 AND 30" ;
$cursor = mysqli_query($db, $sqltitles) or die($sqltitles."<br/><br/>".mysql_error());
while ($row = mysqli_fetch_row($cursor)) {
echo '<option value ="' . $row[0] . '">' . $row[0] . '</option>';
}
?>
</select>
Here is a good reference for how to create HTML <select> tags.
try something along the lines of...
echo '<select name="dropdownname">';
foreach ($titles as $key => $val) {
echo('<option value="' . $val .'">' . $val . '</option>');
}
echo '</select>';
I am facing difficulties while selecting values from drop down list based on the selected value from another drop down list that too retrieve from sql database
My PHP code is embedded with html, here is the code i am trying to do with select:
Country Name: <select name="status" style="width: 150px;">
<option value="select_country" selected>select</option>
<?php
$c_id="";
include 'dbconfig.php';
$sql = "select * from Country";
$result = sqlsrv_query($conn,$sql);
while($row = sqlsrv_fetch_array($result))
{
echo("<option value = '" . $row['Country'] . "'>" . $row['Country'] . " </option>");
$c_id=$row['CountryId'];
}
?>
</select> Create New Country
<br /><br />
State Name: <select name="status" style="width: 150px;" >
<option value="select_State" selected>select</option>
<?php
$s_id="";
$sql = "select * from State where CountryId = $c_id";
$result = sqlsrv_query($conn,$sql);
while($row = sqlsrv_fetch_array($result))
{
echo("<option value = '" . $row['State'] . "'>" . $row['State'] . " </option>");
$c_id=$row['State'];
}
?>
</select> Create New State
Please suggest me how to overcome this and thanks in advance..
You should use AJAX if you want the second select shown options based on the first option list picked value.
You have to use JAAX upon selection of first drop down list and replace the options of second drop down list.
I have a database user with a table user(name(varchar(25)),status(int(1)). The values in status will be just 1 or 0. My query is select user.name where status=1 and I just want the result of it to be put in the option tag. I have tried the codes below but it only appears the select box and no options.
<select>
<?php
include'connect.php';
$res = mysql_query("SELECT name from user where status=1 ");
while($row=mysql_fetch_array($res))
{
?>
<option value="<?=$row['name']?>"><?=$row['name']?></option><?php } ?>
</select>
Change your code to this. Note you are doing a while returning $row but your original code was referencing $res['name']. It should all be $row with $row['name'] being set in the <option> now:
<select>
<?php
include 'connect.php';
$res = mysql_query("SELECT name FROM user WHERE status='1'");
while($row = mysql_fetch_array($res)) {
echo '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';
}
?>
</select>
Also note how I am creating a whole PHP block to run through the results. This makes more sense, is easier to read & easier to debug when compared to template formatting where PHP code snippets are mixed in with HTML.
I have read tutorials about how to populate an entire Drop down list with MySQL, but the problem I am running into is that I only want to grab the one from the database and have that be the selected one. So I would have like a drop down with three items (Item1, Item2, Item3) in the database its stored in a column called itemschoice which has a value of 'Item2'. How do I go about getting item2 to be selected when I load the drop down box?
In your <option> element add the selected attribute for the value that is in itemschoice.
Crude example using a made up function to get the choice:
$choice = get_items_choice();
$results = mysqli_query($sql);
echo '<select name="whatever">';
while($row = mysqli_fetch_array($results)) {
if ($row['choice'] === $choice) {
echo '<option value="' . $choice . '" selected="selected" />';
} else {
echo '<option value="' . $choice . '" />';
}
}
echo '</select>';
This is just an example, don't copy & paste this without adding some kind of error verification!