Using select dropdowns in a form - php

I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you

This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.

<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>

You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option

Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"

Related

Get value of select, from a list of users

i have a table of users, where a user has a name, a surname, a right, and a "commit changes" button. This way, i want to assign rights to a person. Every column has the userID in it, and the whole thing is created from a database.
This is my dropdown to assign a right.
<form id="myForm" method="post" autocomplete="off">
<select name="rightsDropdownOptions" method="post">
<option value="admin">Admin</option>
<option value="masterEditor">MasterEditor</option>
<option value="modulEditor">ModulEditor</option>
<option value="teacher">Lehrer</option>
<option value="teacherAndEditor">Lehrer+Editor</option>
</select>
</form>
This is my php-code, which is executed by clicking the commitchangestorightsbutton
if ( isset($_POST['commitChangesOnRightsButton']) ){
$user = $_POST['commitChangesOnRightsButton'];
echo "<script>alert(".$user.");</script>";
$var = $_POST['rightsDropdownOptions'];
if($var == "1"){
echo "<script>alert(".$var.");</script>";
}
}
If i click the button now i get the following error:
Undefined index: rightsDropdownOptions in C:\xampp\htdocs\Iiigel\PHP\AdminGivePermission.php
I've been working on this for a while now, and i dont knwo how to solve this, id be glad if someone could help.
As highlighted by #Dharman you are making mistake in your code firstly you have to define post parameter "commitChangesOnRightsButton" into your code like this:
<form id="myForm" method="post" autocomplete="off" action"">
<select name="rightsDropdownOptions" method="post">
<option value="admin">Admin</option>
<option value="masterEditor">MasterEditor</option>
<option value="modulEditor">ModulEditor</option>
<option value="teacher">Lehrer</option>
<option value="teacherAndEditor">Lehrer+Editor</option>
</select>
<button type="submit" value="Assign" name="commitChangesOnRightsButton" />
</form>
Hope this will help!

jQuery and PHP to update Select Box value in Text field

I know this question has been asked a lot of times in several other ways but none of them helped me. So, I have formatted it in my own words.
Supppose I have a select box like this:
<select name="something">
<option value="1"><?php echo $value1; ?> for <?php echo $value2; ?>
</select>
<input type="text" name="sometext" value="">
I want to have the <?php echo $value1; ?> in the text field updated live on change of the select box option. To be clear, I DO NOT want the value="1" in the text field and I need to have that value="1" there for some reason and I cannot replace it with value="<?php echo $value1; ?>". I strictly want the inside value <?php echo $value1; ?> to be replaced in the text field. But I do not know how I can achieve it. Please help me experts. For live changing jQuery preferred.
Try below code:
<select name="something">
<option value="1" data="<?php echo $value1; ?>"><?php echo $value1; ?> for <?php echo $value2; ?></option>
</select>
<input type="text" name="sometext" value="">
See below working HTML
jQuery(document).ready(function($){
$('#something').change(function(){
$('#sometext').val($(this).find(':selected').attr('data'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="something" id="something">
<option value="1" data="value1">value1 form value01</option>
<option value="1" data="value2">value2 form value02</option>
<option value="1" data="value3">value3 form value03</option>
<option value="1" data="value4">value4 form value04</option>
</select>
<input type="text" name="sometext" id="sometext" value="">
Hope this will work for you,
$(document).ready(function() {
$('select').change(function(){
var selectedOption = $("select option:selected").text(); //Get the text
selectedOption = selectedOption.split("for"); //split the 2 value by using for
alert(selectedOption[0]); //Get the value before for
$('input').val(selectedOption[0]);
});
});
You need to fetch the selected option text instead of the usual option value, it is pretty easy to do.
You don't need to add any data attributes to achieve this, as there is a method to fetch the actual option text.
Here is a working example:
PHP for populating a select element (below I show the static HTML)
<select id="something" name="something">
<?php foreach($selectList as $value=>$text){ ?>
<option value="<?php echo $value;?>"> <?php echo $text;?> </option>
<?php } ?>
</select>
HTML
<select id="something" name="something">
<option value="1"> Value 1 </option>
<option value="2"> Value 2 </option>
<option value="3"> Value 3 </option>
</select>
<input type="text" id="sometext" name="sometext" value=""/>
jQuery
$(document).ready(function(){
$('#something').on('change',function(){
var selectedText = $(this).children(':selected').text();
$('#sometext').val(selectedText);
});
});
This approach is fast and easy to maintain.
And of course a jsfiddle to see it in action: https://jsfiddle.net/kzgapkt5
Hope it helps a bit

PHP Form Multiple Inputs

I am having issues with php and form handling. I want to be able to click a dropdown box(like you can do with a form in html, with the different options tags) in php, and allow the selection to take the designated value.
Here is what I have :
<input value="<?php echo isset($results['example']) ? $results['example']: ''; ?>" class="form-control" name="data[example]" placeholder="Example">
That populates with a place you can type in input text, which is accepted perfectly in post, but I do not want the user typing in data, only selecting from options, such as this html form does :
<form action="example.php">
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</form>
Here is an edit using the data given :
<form>
<select name="example">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">oranges</option>
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">bananas</option>
</select>
</form>
I did not add the action because it will be posted with other data, but did try both ways(with and without action="post.php") and it does not work...
Defining your options inside a select tag
<select>
<option value="11">apples</option>
<option value="12">bananas</option>
<option value="13">oranges</option>
</select>
Should give you in your post the position (value) of the selected item. Then in your php pass value to the selected items *11,12,13 = apples,bananas,oranges**
Hope it help
You need a <select name="name"> </select> around your options
I think you need something like that
<form action="example.php">
<select name="fruit">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>">apples</option>
</select>
</form>
I'd say the issue is not PHP, I think you just need to use the SELECT and OPTION form tags:
You are specifying an INPUT tag, which by definition allows the user to input data.
<form action="example.php">
<select class="form-control" name="data[example]">
<option value="<?php echo isset($results['example']) ? $results['example']: ''; ?>"">
</select>
</form>
So you are looking for a dropdown I guess:
<form action="example.php">
<select name="fruit">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Orange</option>
</select>
</form>
On submiting the form you can get the value like: $_POST['fruit'] (in this case).
use type="checkbox", so you can select one more
<input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Car"> I have a car<br>
<input type="checkbox" name="vehicle[]" value="Boat" checked> I have a boat<br>
in php
<?php
$vehicleArray='';
foreach($_POST['vehicle'] as $value) {
$vehicleArray.=$value.',';
}
//output 'Bike,Car,Boat'
?>

How to get link url when using select box form method get?

I have a sample code:
<form action="index.php" method="get">
<select name="id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="submit" value="submit" name="submit" />
</form>
When I submit form is url is "index.php?id=1" // or index.php?id=2
=> How to fix it is result "index.php?id=1&name=One" // or index.php?id=2&name=Two
I think the best approach is to populate the options from a db. Use the values as db ID numbers. Then you can store as much info as you want for each option. example:
//some sort of loop{
<option value="
<?php echo $row_option['id'];?>">
<?php echo $row_option['display_text'];?></option>
}
At the other end query db with value; use whatever data need with that value.

Simple PHP select option programming

I have a simple problem in php select option.
The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php
Then I select a option & press Send then It show Which option I select. But I need go specific webpage.
I have tried with this code but I have failed...
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>
";
?>
Anybody Can help me?
This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :
<?php
$test=$_POST['test'];
if(isset($test)){
header("Location: $test");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
As you've declared:
<select name="test[]" multiple="multiple">
test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.

Categories