When I try to read the dropdown with $data['fieldname'] , I expect to get back the text that’s displayed in the field, but that isn’t what happens. Dropdowns always point to either another record instance or an element of a list; these instances and elements all have an internal ID, and that internal ID is what I get back.
But what if I actually want the text that’s displayed in the dropdown?
When submitting a form, what you get is just the value of the selected option, and not the text. However, there is a way to bypass it if you really need the text. The strategy is to add a hidden input to your form, and fill it with the text of the selected option whenever a new option is selected. It will be something like the following code:
<form action="somewhere.php" method="POST">
<select name="field" id="field" onchange="document.getElementById('content1').value=this.options[this.selectedIndex].text]">
<option value="1">First Value</option>
<option value="2">Second Value</option>
</select>
<input type="hidden" name="fieldname" id="content1" value="" />
<input type="submit" name="submit" value="submit">
</form>
Explanation: here we have a dropdown named field, and there is an onchange event for this dropdown. Whenever a new option is selected, we put the text of that option into the hidden input which is called content1. The name of this hidden input is fieldname, so when this form is submitted, you can get the text in the post variable: $_POST['fieldname'];
Related
I'm making a system where a user can search through a library by specific fields.
I currently have a PHP document called index.php that contains a drop down box, a text box, and a submit button. The user selects from the drop down box which field they want to search through (for example Author or Title), then enters their search criteria in the text box (for example "Tolkien" or "The Hobbit") and then clicks on the submit button which then loads simpleSearch.php. The text the user searches for and the field they are searching through are defined on simpleSearch.php as follows:
$queryString = $_GET["userSearch"]; // get user's search string
$queryType = $_GET["searchType"]; // get user's search type
What I would like to have is a different layout but the same functionality on the index.php page. The layout above the double blue line is the current layout. The layout below the double blue line is my desired layout.
Instead of choosing a field from the drop down box and clicking on Submit the user would enter their search criteria and then click on one of the fields. The button would act as the drop down selector AND the submit button at the same time.
Here is my code for the current layout:
<form action="simpleSearch.php" method="get">
<select name="searchType">
<option value="all">All Fields</option>
<option value="author">Author</option>
<option value="title">Title</option>
<option value="subject">Subject</option>
</select>
<input type="text" name="userSearch">
<input type="submit">
</form>
Any help at all would be really appreciated! Thanks!
what about use:
<form action='simpleSearch.php' method='get'>
<input type="text">
<input type="submit" value="all" name="userSearch">
<input type="submit" value="author" name="userSearch">
<input type="submit" value="title" name="userSearch">
<input type="submit" value="subject" name="userSearch">
</form>
And in simpleSearch.php u can get value by $_GET['userSearch']
and dont forget to SQL inject danger ;)
Is there any possibility to have a drop-down-menu where you can add an own value by typing it directly into the drop-down-field?
The Idea is to have all rows in a database to be listet in this drop-down-menu and to have the posibility to add a new row.
The php-File for saving all the Data is allready working.
Rightnow my code for the drop-down-menu looks like this:
<select id="name" name="name" onChange="enableOther();" onFocus="enableOther()">
<option> </option>
<option>Neu</option>
<?php
while($row = mysql_fetch_object($ergebnis))
{
echo('<option>'.$row->Material.'</option>');
}
?>
</select>
i hope there is any solution for my problem...
This isn't supported natively in HTML, but there are many JavaScript UI toolkits that provide an editable combo box widget, or you could try coding one in JS yourself (essentially, using a text input with a custom-coded drop-down list).
Alternatively, a simple approach would be something like this:
<select name="name" ...>
<option value=''>Custom (use text field)</option>
... etc ...
</select>
<input type='text' name='customName'>
Then at the server side, if the submitted value of name is a blank string, but the submitted value of customName is not, use customName. You could also add JavaScript to enable/disable the customName field when the user changes the select box as appropriate.
Edit: HTML5
Incidentally, this is possible in HTML5, but that, of course, is not supported by all browsers:
<input list="suggestedNames" name="name">
<datalist id="suggestedNames">
<option value="...">
...
</datalist>
i am working with multiple form in one page. Basically i have a form in which there are some element and when a user select one of the category from drop-down list in the form, it adds addition elements on the form depending on the category chosen. my problem occurs when i try to get the value from user input and save it into database. After fiddling with the code i realised that the code is unable to catch the value of the form after one div tag has occurred. let me explain through code.
<form>
<input type="text" name="" >
<select name="category" id="category" onchange="show()">
<option name="Phone" value="Phone" >Smartphone</option>
<option name="Laptop" value="Laptop" >laptop </option>
</select>
//(show() function will make one of the div tag visible(depending on category selected) and so it will display on screen.)
<div id="phone" style="visibility:hidden">
<form>
<some input elements....>
</form>
</div>
<div id="laptops" style="visibility:hidden">
<form>
<some input elements....>
</form>
</div>
</form>
now in php when i try to get the input value using $_POST i can only access the value of first div tag which is with id="phone" but when i chose category laptop, even though it display on screen correctly i am not getting the value of user input. i have checked the forms and there is no error since when i swap the forms around, it have same problem.
You have forms contained by other forms. This is not allowed in HTML and usually produces inconsistent and undesired results.
You also have at least one other error (which you repeat). Test your HTML with a validator.
i can only access the value of first div tag which is with id="phone"
Where you have vaguely said <some input elements....>, you might have inputs which share a name. Invisible elements still exist, so the elements in the first (illegal) nested child form will always be submitted.
When PHP populates $_POST and friends, only the first set of data associated with a given name will be stored, the rest will be discarded. (Unless you name the inputs with [] characters).
This question already has answers here:
Get Text From <option> Tag Using PHP
(6 answers)
Closed 8 years ago.
in the following:
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
</form>
in test.php, I can get 1 or 2 as follow:
$result=$_POST['test'];
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using php
This is not something that can be done through PHP alone. The PHP script can only "see" the information which is posted (the value for the selected option that is posted). You can use javascript to alter a hidden input field with the text contents of a selected option, and this will be included in the $_POST array:
<form action="test.php" method="POST">
<select id="test" onchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
<input type="hidden" name="test_text" id="text_content" value="" />
</form>
This will make the $_POST['test_text'] available with the selected index (but you should also force the onchange() function when the page loads so that it will be populated even if the user leaves the select field at the default value.
Only the value of the form control will be sent to the server. An easy way to send both is to simply include both in the the value:
<select name="test">
<option value="1|Test one">Test one</option>
<option value="2|Test two">Test two</option>
</select>
And then:
$test = explode('|', $_POST['test']);
Then you'll end up with $test[0] being "1" and $test[1] being "Test one".
You can't; that information is not sent back to the server. You will need to look at how you generated the HTML in the first place and get the text from there.
It is not sent so the only way to get it is having an array mapping values to titles in your PHP code.
This is a solution I could use except I already have the onChange calling a function to show a hidden block based on the selected building type in a statement.
I have building types I need the user to select. Several building types have a unique set of questions, e.g. Bank Branch, Data Center, Courtroom, etc. But there are many that have the same set of questions which I call Other Type. Each option has their associated value, e.g. Bank Branch has a value of "Bank_Branch", Data Center has "Data_Center", but Other Type has a value of "Other_Type". The text for "Other_Type" differs based on the building type, such as Convention Center, Museum, Performing Arts, etc. So I need the value "Other_Type" to "show" the questions in the "Other_Type" DIV block while using the text value to send in an email identifying the type of building, e.g. Convention Center, Museum, Performing Arts, etc.
Any way to use PHP to get the text value of the selected item? I'm already using the following inside the HTML
var sele = document.getElementById('building_type');
var seleVal = sele.options[sele.selectedIndex].value;
var seleTxt = sele.options[sele.selectedIndex].text;
document.getElementById("other_text").innerHTML = seleTxt;
I'm not seeing a way to do this.
SOLVED: I can simply create the hidden div and in my check for Other Type set the innerHTML for the hidden div.
The text for the selected option is not passed through the HTML form. If you want the text, you have to store that in a hidden HTML input on your page, or you need to add some business logic in your PHP code to translate the value of ìd into the text (through a switch statement, or by querying a database, etc.)
Unfortunately when you submit a form and a variable it only takes one parameter which is it's value. You would need to make the value. Test One the value in order for it to pass on to the PHP script. What is the purpose of value="1" cause you can probably use it in a different attribute?
Im not a experienced php programer, but u can check the value selected in SELECT tag with php.
i dont know why people say u can not.
if ( $_POST['test'] == 1)
{ do something }
else
{ option value 2 is selected, do something else }
Im sure this will work.
Try it if 1, 2 not needed. you will get required text as value
<form action="test.php" method="POST">
<select id="test" name="test">
<option value="Test One">Test One</option>
<option value="Test Two">Test Two</option>
</select>
</form>
I've been looking around on SO and I don't -think- this is something that's up here, either that or it's something I've missed. If so, please point me there and I'll simply mark this question as answered.
I have a completely working PHP form where my users input information into input boxes and there's a button at the bottom that submits the information to the MySQL database, so that part works perfectly. What I'm trying to do now is this: have 2 drop down menu's, each one with a static list of choices and I'd like those choices to also get sent to the database.
For instance, currently in my form I have First Name (frame) and Last Name (lname) that the user can input and then if I query the database it spits out First Name and Last Name perfectly. So now I'd like to add to my form a drop down box where the user can pick, for example, Boy or Girl, and then after doing that click the submit button that's already there (I don't want the drop-down to submit the data, and I don't want the drop-down to be populated from the database.)
I'm guessing I need to use Javascript for this part? But I really don't know.
Any advice would be appreciated.
<html>
<head>
<title>MyForm</title>
</head>
<body>
<form id="form" name="form" action="" method="post">
<Label>First Name/Organization</label>
<input type="text" name="firstname" value="<?php echo $firstname; ?>"/>
<input type="submit" name="submit" value="Add entry">
</form>
</body>
</html>
So, as part of the same form, you simply treat it just like the input boxes. Here's an example:
If the dropdown is named:
<select name="gender">
<option value="boy">Boy</option>
<option value="girl">Girl</option>
</select>
Then in your php, you would simply get the value for gender:
$gender = $_POST["gender"];
And add to your SQL statement where you are saving the first/last name the additional field and value for gender.
Of course, you would have to first modify the table to have the column for gender....
Hope that helps!
It sound s like you just want a simple html drop down as part if your form. Just use
<select name = 'gender'>
<option value = 'boy'> Boy </option>
< option value = 'girl'> Girl </option>
</select>
Then access $_POST['gender'] in the receiving script to put it into the database (after validation of course)