Retain the option in the dropdown list in php - php

I have a dropdown with the following options:
i.)All
ii.)Public User
Where The option public user is selected from the database
<select name="desc" id="selectbox">
<?php
echo '<option value="all" >All</option>';
$a=$obj->getPublicUser();
echo '<option value="PublicUser" >'.$a.'</option>';
?>
</select>
$a has getPublicUser() :
public function getPublicUser()
{
$publicuser=self::PUBLIC_USER;
$result= mysql_query(" select category_desc from user_category where category_id=4");
if($result)
{
while ($row = mysql_fetch_assoc($result))
{
$user=$row['category_desc'];
}
}
return $user;
}
I have used
My Submit button is as follows:
<input type="submit" name="GETREPORT" value="Get Report" />
My problem is On selecting option ALL and clicking on submit button the all option is retained. But on selecting Public user and then clicking on submit button the option resets to "all". How can I make the public user retain even after the submit ? Plz help

you can try with this
if (isset($_GET['desc']) && $_GET['desc'] == "PublicUser") selected="seleceted"

Please Try this, Hope it help you:-
<html>
<form name = "client_question">
<select name="desc" id="selectbox">
<?php
echo '<option value="All" >All</option>';
echo '<option value="PublicUser">PublicUser</option>';
echo '<option value="NormalUser">NormalUser</option>';
?>
</select>
<input type="submit" name="GETREPORT" value="Get Report" />
</form>
<script>
<?php if(isset($_GET['desc']) && $_GET['desc']) {?>
document.getElementById("selectbox").value = "<?php echo $_GET['desc'] ?>";
<?php } ?>
</script>
</html>

Related

Button to reset the selected option in dropdown list

I have a page with 2 select option dropdown lists and I can only use one of them each time I have to submit the selected option but if I use two and click on submit i get an error Only use one list and thats how it should work.
for example:
I have 2 drop lists like this one
if i choose both like the pic above and click on submit I get an error and the selected options are shown as the deffult option and thats what I want,
but when I click on the reset button it need to reset them to the default value but nothing happens.
like this
The Options for the dropdown list is from two different arrays.
$test1 and $test2 are two different arrays
<form name="rechner" method = "POST">
<select name="one">
<option value="" selected hidden>Choose One</option>
<?php foreach ($test1 as $one => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['one'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<select name="two">
<option value="" selected hidden>Choose one</option>
<?php foreach ($test2 as $two => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['two'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<input type="submit" name="calc" value="Submit" />
<input type="reset" name="reset" value="Reset">
</form>
if ($_POST['calc'] == "Berechnen") {
/// do something
}
found the answer on other question
Here
<script type="text/javascript">
function resetForm($myform) {
$myform.find('input:password, input:file, select, textarea').val('');
$myform.find('input:radio,input:checkbox')
.removeAttr('checked').removeAttr('selected);
}
</script>
<form name="rechner" method = "POST" id="myform">
/////html code/////
<input type="button" value="Reset" onclick="resetForm($('#myform'));">
</form>

Retrieve data/record from the database using dropdown list without reloading

<form method="post" action="a.php">
<select name="taskOption">
<?php
include 'orderSelect.php';
echo '<option>View Order</option>';
while($row = mysqli_fetch_array($result)):;?>
<option value='<?php echo $row[0]; ?>'><?php echo $row[1]; echo " ";
echo $row[2]; ?></option>
<?php endwhile; ?>
<input type="submit" name="submit" value="click">
</select>
</form>
I want to retrieve a record/ data from the database when I select something from the dropdown list. Any advice on what to look up or code for this to be successful.
As suggested in OP tag you should be using ajax Example with JQuery below:
$('select["name=taskOption"]').change(function() {
var selected = $(this).val();
$.get('YOUR_FILE_WHICH_PROCCESS_RECORD.php',{selectVal:selected},function(data){
console.log(data);//this is the value from your php file.
})
})

send the selected value from a dropdown menu to another page

I have a dropdownlist populated by a MySql database that shows the titles of books stored in my database.
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
I want the option I choose to send it to another php page search.php
This search.php I want it to get the title and search for this specific book details.(title, price, author.... etc) .I tried to do it with but it ruins the page.
Add below code in form that should work for you.
<form action='search.php' method='post'>
<select id="titles" name='titles'>
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
<input type='submit' value='submit'>
</form>
in search.php:
$title = $_POST['titles'];
You just need to add the form above the select tag and need to give the NAME attribute in the select tag to post the data on another page. You can try with the following code:
<form method="post" action="search.php">
<select id="titles" name="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
and on the search.php page, you can get the value of the dropdown by this:
$title = $_POST['titles'];
Try as below :
<form method="post" action="YOURPAGEPATH">
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
Without submit button :
<form method="post">
<select id="titles" onchange="this.form.submit();">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
</form>
Surround that with a form with the appropriate action and add a submit button. Otherwise use something like jQuery to listen for the value of that to change and submit the form.
For example:
<form action="search.php" method="GET">
<select id="titles" name="title">
<?php /* put your stuff here */ ?>
</select>
</form>
And then in jQuery:
$(function(){
$('#titles').on('change', function(){
$(this).closest('form').submit();
});
});
Or you could go real old-school and attach the event listener to the select like this:
<form action="search.php" method="GET">
<select id="titles" name="title" onchange="this.parentNode.submit()">
<?php /* put your stuff here */ ?>
</select>
</form>
Just in case if you don't want to use the Submit Button
<script language="Javascript">
function books(book)
{
var url="http://www.example.com/search.php/?q="+book;
window.open(url, "_self");
}
</script>
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option onClick="books('.$row['title'].')" value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
Here the list will call the Books function on Click and pass the arguments to the function which will redirect you to search.php
To retrieve the book name use
$_GET["q"]
Change the URL as required.
And if the problem is solved don't forget to Mark the answer.

Creating dropdown list with data from sql and when post is called, keeping the tab selected

Ok, heres my problem:
I can create the dropdown list, but when I try and set the last used tab as selected it runs through the while query and selects the bottom option.
Heres my code:
<?php
$sql="SELECT id, description FROM sites";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$thing=$row["description"];
$thing2 = $_POST['thing'];
$options.="<OPTION VALUE='if (isset($_POST['thing'])){ echo $thing2; 'selected='selected''}else{ echo $id}'>".$thing;
}
?>
<form action="" method="post" />
<SELECT NAME="thing" id="thing">
<OPTION VALUE=0>All
<?=$options ?>
</SELECT>
<input type="submit" name="submit" value="Update"/>
</form>
How do i set the selected tab to the one it is on after the submit button is clicked?
$options.="<OPTION VALUE='if (isset($_POST['thing'])){ echo $thing2; 'selected='selected''}else{ echo $id}'>".$thing;
needs to change to
$is_selected = isset($_POST["thing"]) && $_POST["thing"] == $id;
$options.="<OPTION VALUE='".$id."'".($is_selected?" selected='selected'":"").">".$thing."</OPTION>";

how to get selected username from dropdown list to be the var for URL

How do i get the selected variable to show up in the URL from the
<select> ...
my code is:
print "<select name=\"assignedby\" multiple size=\"10\">";
while ($data = dbResult($qh)) {
print "<option name=\"$data[name]\"";
print ">$data[name]</option>\n";
}
print "</select>";
print "<br><a href='".$_SERVER['PHP_SELF']."?action=add'>Add</a> || <a href='".$_SERVER['PHP_SELF']."?origname=$data[name]'>Edit</a>\n";
When someone clicks on "EDIT" link its showing up as: http://www.site.com?origname=
i want it to show up with the actual selected origname from the drop down list...
like:
http://www.site.com?origname=$selecteduser-fromdroplist
please help!
Why don't you just make the form method = get?
<form id="select_name" action="" method="get">
<select name="origname">
<?php while ($data = dbResult($qh)): ?>
<option value="<?php echo $data[name]; ?>"><?php echo $data[name]; ?></option>
<?php endwhile; ?>
</select>
Add || <input type="submit" name="submit" value="Edit">
</form>
But as for why your code doesn't work, you're calling the $data[name'] item outside the while loop.

Categories