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'
?>
Related
I have kind of mastered input boxes. I put them as gaps in a text in a form, the student fills in the gaps and sends the form, which calls the php.
<form action="php/thankyou18BEsW1.php" method="POST" name="myForm" onsubmit="return checkForExpiration();" >
Some text<INPUT TYPE="Text" NAME="G1" size="15"> some more text.
My php script has at the top:
//should mail the contact form
<?php
$studentnr = $_POST['sn'];
$q1 = $_POST['G1'];
and sure enough, the value in the textbox G1, and all the others, get sent to me OK.
Soemtimes, I would like to use a drop-down box. The students should choose a word. Here on stackoverflow I found the following. I hope that is the right way to do this!
<select >
<option value="" disabled selected>Please select a word...</option>
<option>this</option>
<option>that</option>
</select>
I'm not quite sure where to put NAME="G1". In <select> or in <option> ???
When I do this with radio buttons I have banks of radio buttons like this:
<input type="radio" name="G30" value="A">A
<input type="radio" name="G30" value="B">B
<input type="radio" name="G30" value="C">C
<input type="radio" name="G30" value="D">D
and that works. So I'm thinking I should put name="G1" in
<option name="G1">this</option>
<option name="G1">that</option>
<option name="G1">whatever</option>
Will that work like that? Is there some other, better way?
<select name="country_code">
<option value="us">United States</option>
<option value="fr">France</option>
</select>
and then in PHP:
$countryCode = $_POST['country_code'];
if ($countryCode === 'fr') { /*...*/ }
i could design website. it have textbox input and selection input types. i need to populate the selection option value depends on user input value.how can i do?
thanks in advance!
<input type="text" name="name">
<?php if(isset($_POST['name'])=="abcd"){$cir="1";}
elseif(isset($_POST['name'])=="efgh"){$cir="2";}
elseif(isset($_POST['name'])=="ijkl"){$cir="3";} ?>
<label>Select Circle:</label>
<select id="cir" name="cir">
<option value="1" <?php if(isset($_POST['cir'])&&$_POST['cir']=="1")
{echo 'selected="selected"';}?>>tamil</option>
<option value="2" <?php if(isset($_POST['cir'])&&$_POST['cir']=="2")
{echo 'selected="selected"';}?>>english</option>
<option value="3" <?php if(isset($_POST['cir'])&&$_POST['cir']=="3")
{echo 'selected="selected"';}?>>hindi</option>
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
i stuck with this. I want to replace an editbox if a form with a select options. I have the following code that i use to take the valuefrom the editbox
<input type="text" name="toheat" size="30" class="inputbox" maxlength="50" value="<?php echo $obj->toheat; ?>" />
How can i do it so the user can choose from certen 2-3 options?
Can you give me a clue how to continue?
Thanks in advance
You can use a select box in the form :
<select name="toheat" id="toheat">
<option value="">select</option>
<option value="toHeat1" <?php if($obj->toheat == "toHeat1"){?> selected="selected" <?php } ?>>toHeat1Name</option>
<option value="toHeat2" >toHeat2Name</option>
//and so on
</select>
I hope this can be of some help
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"