I have a empty select box on a form.
<select id='essentialDocs[]' ></select>
through some script i add options to it. Basically it adds URL's. So at runtime html will look
like
<select id='essentialDocs[]' >
<option value='http://www.google.com' title='Google'>Google</option>
<option value='http://www.yahoo.com' title='yahoo'>Yahoo</option>
</select>
Now on submitting the form i want to get both of these key:value pairs
like Title:URL google:http://www.google.com
but on doing $_POST['essentialDocs'] i only get values and not title's. What modification would help get me both. Also another thing i have on the form is i can switch the ordering of url's on screen. Please suggest some solution
Try this. This may help you..
Just add title in value with : When you submit you get your desire value.
<form method="post" action="">
<select id='essentialDocs[]' name='list'>
<option value='google:http://www.google.com' title='Google'>Google</option>
<option value='yahoo:http://www.yahoo.com' title='yahoo'>Yahoo</option>
</select>
<input type="submit" name="Submit" value="submit">
</form>
Print post array in PHP
<?php
print_r($_POST);
?>
Output:
Array ( [list] => yahoo:http://www.yahoo.com [Submit] => submit )
Now you can get both title and URL.
If you need the title map the title to the URL on the server in e.g A dictionary or array.
IMHO it would be even better to do it the other way around. Set the title as a value and map it to a URL on the server.
$arr["Google"] ='http://www.google.com'
$arr["yahoo"] = 'http://www.yahoo.com'
then you also have the ability to maintain this array on the server and echo it (back) into the select.
more here
Related
I am sorry for the title. That was my best shot to explain the situation with the least words.
I am trying to make a php program that has a html part with select and option. I am not using ajax or mysql. Just using a JSON file and xampp for apache.
if you select one of the options,`
if(isset($_POST["choice"]))
this php code will work in the html, and show a series of input boxes where you can type in what ever you want. Each option has an array within a JSON file.
So, I have put it in
$file[$_POST["choice"]]
`
and iterated it with a key => value. and shoved it in the input box. The value of the input box would be initially the value of the JSON file I called. I wanted the user to erase that text and type in their own. There could be several input boxes depending on the choice the user makes.
The name of the input box would be the KEY.
Then if you hit the edit button which is a input type submit, the series of input boxes will disappear.
I wanted to get the return with a
$_POST[KEY]
But, whatever I choose, the $_POST[KEY] will just return me the very first option of the select option html.
IS there a way I can solve this?
I need to get the corresponding array of the selected choice.
My goal is to get the values of the input box and update a JSON file.
<select name = "muscle">
<option value = "chest">Chest</option>
<option value = "back">Back</option>
<option value = "leg">Leg</option>
</select>
<br>
<input type="submit" name="choice" value="choose">
<br><br>
<?php if(isset($_POST["choice"])) : ?>
<h3> Current Workout Program </h6>
<?php
foreach ($program[$_POST["muscle"]] as $key => $val):
?>
<p><?= $key. ":" . $val;?></p>
<input type="text" name="<?=$key?>" value="<?=$val?>">
<?php endforeach;?>
<br><br>
<input type="submit" name="edit" value="edit">
<br>
</form>
<?php endif;?>
The iteration of the Key value above works fine.
But if I do a
if (isset($_POST["edit"])){
print_r($program[$_POST["muscle"]]);
}
After submission, It will give me the array for "chest" only.
As I understood your code, if you submit the mentioned form the $_POST would be as the following box:
//$_POST is like the following
[
"muscle" => "chest",
"choice" => "choose"
]
So in the result page, if(isset($_POST["choice"])) condition check would be always true, And the progress will go right.
I think on the destination page (eg. edit page) you have to add a hidden input as the following to make sure you tell the system which muscle you are editing:
<input type="hidden" name="muscle" value="<?= $_POST['muscle'] ?>">
Please note that the input type is hidden and it's not shown to the person who is working with the form.
Check the solution out and let me know if there are any other issues with the response.
<select name = "lstTerminals" size = "5" id="lstTerminals"style="width:200px;">
<option value="C172"> >C172</option>
<option value="B737">B737</option>
<option value="D483">D483</option>
</select> <br/> <br/>
How can I get all the values inside of this list and store it into an array and use all these data onto another php script? After all that, how can I call these values in another php script? Would anyone please me with this? I would greatly appreciate it!
This is tricky with a client-side solution. Assuming you have a php-script generating the select in the question you should do this server-side.
If the values are on a user page your best bets are
A session variable.
A hidden form field.
For 1. Set the value of $_SESSION['lstTerminalValues'] to an array with the values. Do this when you create the original select.
For 2. you'd have something like
<input type="hidden" name="lstTerminalValues" value="C172,B737,D483"/>
then recreate the array with
$optionsArray = explode(',', $_QUERY['lstTerminalValues']);.
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>
If I have a combobBox with values red/white/blue and the users chooses blue then I want my form action php page to use the value 2, not `blue. I hope you see what I mean.
Is there any easy way to do that?
<form action='phpPage.php' name='bsForm' method='POST'>
<select name='TheColor' onSelect="bsForm.submit()">
<option value='0'>red</option>
<option value='1'>white</option>
<option value='2'>blue</option>
</select>
</form>
If your form looks anything like that, then your phpPage should easily be able to get the value 2 from $_POST['TheColor']
You do this by constructing your form appropriately and supplying a value attribute:
<option value="2">blue</option>
I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.