how to use post variable before submit button - php

If I given isset() undefined error problem solved. but after submit the selected values not displayed. without isset() of how to use $_GET['ddlpub'] in php 7.2.18. it is working in php 5.2.3
<select name="ddlpublisher" id="ddlpub" class="ddltxt" >
<option value="%">--ALL--</option>
<?php
include('dbconfig.php');
$query = "Select distinct pub from jd order by pub";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result))
{
if($row['pub']== isset($_GET['ddlpub']))
{
echo '<option value="'.$row['pub']. '" selected="selected">'.$row['pub']. '</option>';
}
else
{
echo '<option value="'.$row['pub']. '">'.$row['pub']. '</option>';
}
}
?>
</select>

You have used $_GET not post.
Also you have to check that $_GET['ddlpub'] is set and then you have to compare it as below
while ($row = mysqli_fetch_array($result))
{
if( isset($_GET['ddlpub']) && $row['pub'] == $_GET['ddlpub'])
{
echo '<option value="'.$row['pub']. '" selected="selected">'.$row['pub']. '</option>';
}
else
{
echo '<option value="'.$row['pub']. '">'.$row['pub']. '</option>';
}
}

Related

Set previously selected value in Dropdown box using PHP

So, what I'm trying to achieve that after the user hits submit, the dropdown retains the value that the user have chosen. This is my code for the dropdown, I understand that I have to selected = 'selected', but I couldn't figure out how it fits in my code.
if (mysqli_num_rows($result) > 0) { // output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;
You cannot just add selected = selected inside the loop because every option will be selected.
You might need the if statement to select the option of your choice.
For example:
if($row_branch["0"] == 'bar'){
$menu_branch .= "<option value='".$row_branch["0"]."' selected>" .
$row_branch["0"]. "</option>";
}else{
$menu_branch .= "<option value='".$row_branch["0"]."'>" .
$row_branch["0"]. "</option>";
}
When the user select an option, Where you submit this information?
If this information return from a database
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
$select = ($row_branch["0"]==$value_db)? "selected='selected'" : "";
$menu_branch .= "<option value='".$row_branch["0"]."' $select>" .
$row_branch["0"]. "</option>";
}
}
if i understand right:
// $yourvalue is previous dropdown value
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row_branch = mysqli_fetch_array($result)) {
if ($row_branch["0"] == $yourvalue)
$selected = "selected";
else
$selected = "";
$menu_branch .= "<option value='".$row_branch["0"]."' $selected>" .
$row_branch["0"]. "</option>";
}
}
echo $menu_branch;

PHP: select current option

I have a DB table with all categories (id, category) and table with all of events (id, event, categoryID).
And on the event editing form I have the select field with all of the categories (getting from the DB). But sinse I'm developing an editing form, I need to select current category by default.
This is my select field (PHP method that gets all the categories from DB and puts them in the following order):
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
Let's say, current event is under category 3, so I need the following HTML to be generated:
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3" selected>Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>
How do I achieve it with PHP, if I have the catID?
Hopefully, this question is clear enough. Sorry for my bad explanation
UPD: This is my PHP code that generates category list:
public function getCatList($conf) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
}
Appending to your while iteration a condition will solve this for you:
while($row = mysqli_fetch_array($result)) {
$isSelected = $row['id'] == $catID;
echo '<option '.($isSelected ? 'selected="selected"' : '').' value="' . $row['id'] . '">' . $row['category'] . '</option>';
}
You're making a comparison if the current value is the same as that stored in $catID - and store the boolean result in a variable. In the echo you're just doing a conditional forking and appending the selected attribute if the value was true, otherwise not appending any empty string.
You can do it like
while($row = mysqli_fetch_array($result)) {3
echo '<option value="' . $row['id'] . '"';
//if condition is met then make the option selected
if($row['categoryID'] == 3) {
echo " selected='selected' ";
}
echo '>' . $row['category'] . '</option>';
}
you should add the selected catID as a parameter to your getCatList-function. Then just change the creation of your HTML to include the selected-attribute:
public function getCatList($conf, $catID = 0) {
$mysqli = $this->dbConnect($conf);
// Quering...
$query = "SELECT * FROM categories";
$result = $mysqli->query($query);
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"' . ($row['id']==$catID?' selected':'') . '>' . $row['category'] . '</option>';
}
}
now you can just pass the catID to your function:
$myConf = "something";
$myCatID = 3;
getCatList($myConf, $myCatID);
//query for the categories and options
$current_value = 3;
$total_num_of_options = mysql_num_rows($your_query_result);
for($count = 1; $count <= $total_num_of_options){
echo "<option value='".$count."' ";
if($count == $current_value)
echo "selected";
echo ">cat".$count."</option>";
}

Mark an option as selected

I am populating a dropdown menu with data in a database, using this code:
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
I want to add the selected=selected to the option selected and then use that option in another query, but I have problem adding the selected tag in the actual selected entry.
More INFO:
I am using dropzone.js to upload images, and I want to select the category on the fly. The category has to be selected from the dropdown menu and used in the INSERT query.
if you have a form with POST then the code is
$selected = "";
if($_POST['galleries']==$name) $selected=" selected='selected'";
echo '<option value="' .$name. '"'.$selected.'>' .$name. '</option>';
Just as an example try this,
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
if( $i == 1 )
{
$sel = 'selected="selected"';
}
else
{
$sel = '';
}
echo '<option value="' .$name. '"'. $sel .' >' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
If you want to select a specific option then you are going to have a variable containing that specific option example:
$conditionalName = "Mark";
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '"'; if($name=$conditionalName){ echo ' selected=selected';} echo '>'. $name. '</option>';
}
echo '</select>';
echo '</label>';
}

Find previously selected option in while loop

I'm trying to get a drop down menu to keep its selected value when the user hits submit, but it fails due to errors on the form.
I have a while loop returning values from a database to build the options for the drop down, but how do I echo "selected" on the right option?
I have tried if($district == $row["name"]) { echo "selected";} as you see below, but it doesn't work.
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="{$row["name"]}"'; if($district == $row["name"]) { echo "selected";} ; echo '>' . $row["name"] . "</option>";
}
?>
Sorry for the delay. None of the suggested answers worked for me. Any other ideas?
Can you try this,
<?php
$result = mysql_query("SELECT dist.name FROM districts AS dist JOIN int_bd AS ibd ON dist.id = ibd.districts_id WHERE banners_id = 6 GROUP BY dist.id ORDER BY dist.id ASC", $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
$district = $_REQUEST['name']; // You need pass the value you have been submitted
while ($row = mysql_fetch_array($result)) {
$selected ="";
if(trim($district) == trim($row["name"])) { $selected = "selected";}
echo '<option value="{$row["name"]}" '.$selected.' >' . $row["name"] . "</option>";
}
?>
Try this..
if($district == $row["name"])
{
echo "<option value='$district' selected>$district</option>";
}
I just found the answer. This is what I did:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row["name"] . '"';
if($row["name"] == $district) { echo 'selected';} ;
echo '>' . $row["name"] . '</option>';
}
It seems to have been this line
echo '<option value="{$row["name"]}"';
that was causing the problem.

how can i make a default value if the option value of a select tag isnt a specific value?

<select name="corequisite">
<option value="" selected="selected">No Corequisite</option>';
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res)){
echo'<option value="'.$row['Code'].'"';if($row['Code']==$result['corequisite']) echo'
selected="selected"';;echo'>'.$row['Name'].'</option>';
}
echo'</select>';
I want the 'No Corequisite' value to be selected if no other options will be match with $result['corequisite']. But the first item in the table will be selected!
How can I fix it?
It may be fixed by inserting 'corequisite' as the first record. A good way?
Answering your last question, probably a bad way.
Try this code, currently on the laptop so haven't tested it.
<select name="corequisite">
<?php
$res = mysql_query("SELECT * FROM course");
$rows = mysql_num_rows($res);
if ($rows == 0)
{
$sel = 'selected';
} else
{
$sel = '';
}
echo '<option '.$sel.' value="">No Corequisite</option>';
while($row = mysql_fetch_array($res))
{
$sel = '';
if ($row['Code'] == $result['corequisite'])
{
$sel = 'selected';
}
echo '<option '.$sel.' value="'.$row['Code'].'">' . $row['Name'] . '</option>';
}
?>
</select>
I haven't tested it, but this should work
<select name="corequisite">
<?php
// this is your string that will return all your results
$to_echo = '';
// this will check if any value inside is selected ( this will be useful later )
$any_item_selected = false;
$res = mysql_query("SELECT * FROM course");
while($row = mysql_fetch_array($res))
{
if($row['Code']==$result['corequisite']) // OK! so one item is selected, remember it!
{
$to_echo .= '<option selected="selected" value="'.$row['Code'].'">'.$row['Name'].'</option>';
$any_item_selected = true;
}
else
$to_echo .= '<option value="'.$row['Code'].'">'.$row['Name'].'</option>';
}
// Now we can check if anything were selected, and prepend the "No corequisite" option, selected or not! :)
if( ! $any_item_selected)
$to_echo = '<option value="" selected="selected">No Corequisite</option>' . $to_echo;
else
$to_echo = '<option value="">No Corequisite</option>' . $to_echo;
echo $to_echo;
?>
</select>

Categories