send the selected value from a dropdown menu to another page - php

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.

Related

Displaying default value through select tag

I am having trouble passing & displaying default value for select tag. So, I have tried different variants of select and option tag but I am not able to get this right.
I want to fetch different options in form of a drop down menu. I want to display a default value in it. ($categorytemp[$i] in code). On displaying the form
A user can update this choice; or
A user can select the same choice again; or
A user can not change it at all.
Expected result for the above activity should be update, update, default value for $category
Below is the snapshot of my code
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
I tried using the option tag with selected attribute but that is making things more complicated. For example, if I use
<select name="category" value="<?php echo $categorytemp[$i] ?>">
<option selected="selected"><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>
It displays the form as desired but on making no selection, it passes null value in the category. I have gone through questions of similar type asked earlier, but they do not answer my query to my satisfaction
You do it with a "selected" attribute of a given <option> and not in the <select> tag with the value attribute.
Check these two:
http://www.w3schools.com/tags/tag_select.asp
http://www.w3schools.com/tags/tag_option.asp
Have you consider using Javascript on the form submit to achieve it?
Another option would be a hidden field with default value before the select and an hidden option of the select field:
<html>
<head>
<body>
<form method="POST" action="">
<input type="hidden" name="dropdown" value="default" />
<select name="dropdown">
<option selected disabled hidden style="display: none" value="default"></option>
<option>1</option>
<option>2</option>
<input type="submit" value="send" />
</form>
</body>
</html>
Is $categoryTemp[$i] an ID or a name? If it is the ID and you are wanting to check the result against it to determine the default option to display then something along these lines would select the option that is equal to the value of $categoryTemp[$i].
<option <?php echo ($categorytemp[$i] == $education_category ? 'selected' : ''); ?>
So if $categorytemp[$i] = 1 and $education_category = 1 then that option would be marked selected and be displayed when the page loaded
This did the trick. When no value is selected, pass the default value as value in option
<select name="category" >
<option value='<?php echo $categorytemp[$i]?>'><?php echo $categorytemp[$i]; ?> </option>
<?php
$sql = "SELECT * FROM education_details";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
{
$education_category = $row["id"];
$education_name= $row["name"];
?>
<option value='<?php echo $education_category?>'><?php echo $education_name;?></option>
<?php
}
?>
</select>

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

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 Keep the selected value of the select box after Form POST or GET

Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>

Categories