i gonna get select option all values(not only selected values) in fact i have two list and the first list is my source and user add their selected values to second list. so i gonna get the second list all values:
my list is here:
<form method="post" action="a.php">
<select name="ap" size="5" style="width:100px;">
<option>op1</option>
<option>op2</option>
<option>op3</option>
<option>op4</option>
<option>op5</option>
</select>
<input type="submit" />
</form>
i have researched and find out that we can use the $_POST and $_REQUEST i have used both of them like this but i don't know why i don't get result:
the following code print nothing:
<?php
echo $_REQUEST['ap'];
?>
and also using the following code but get no result:
<?php
foreach( $_REQUEST['ap'] as $key=>$val)
{
echo $val;
}
?>
but get Warning: Invalid argument supplied for foreach() in warning message.
and also trying this but get the warning as same as above;
foreach( $_REQUEST['ap'] as $key)
{
echo $key;
}
You need to assign a value to the tags, like this:
<option value="test">Test</option>
Then you can get that in PHP.
The options need values. The value sget transmitted to the server, the value in the tag gets displayed to the user.
It's distinguished to enable developers to show text to the user, but transmit ids etc.
<form method="post" action="a.php">
<select name="ap" size="5" style="width:100px;">
<option value="op1">op1</option>
<option value="op2">op2</option>
...
</select>
<input type="submit" />
</form>
Forms only ever submit the selected options.
You say users can add values to this list somehow. I assume this uses javascript (though the same principle would apply if you used form submit buttons & PHP).
You need to modify the code that adds values to the list. As well as populating the visible list, you can populate an invisible list which will provide the desired information.
I would try creating hidden inputs, as in <input type="hidden" name="ap_all[]" value="op1">.
(I think the [] is already mentioned somewhere here - it tells PHP to build $_POST['ap_all'] from all the values, instead of just using the last one).
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.
I have searched many solutions and almost all over the internet but found no answer.
I am having a form like:
<form action="index.php" method="get">
<select id="store" type="text" name="s">
<option valA="value1" valB="value2">Selection 1</option>
<option valA="value3" valB="value4">Selection 2</option>
</select>
<input id="submit" type="submit" value="Submit" />
</form>
This form is extended via PHP with MySQL which takes valA and valB from database for each "Selection" in while loop and generate form with that form (given above).
I know that sending it via POST will reload my website with value given by value name. But I need to do it with two values.
So my link when I select one of the options will be:
http://mywebsite.com/index.php?valA=value1&valB=value2
Form is still active every time website reloads, so when I choose another option, I want to change it with submit and change URL.
Everything I found is to use multiple option and send two of them at the same time, but I have no multiple selection, but one selection with many parameters each (for me - two).
Is it possible to do it via HTML/Form, or I need to use PHP, but how?
I need to reload website - not change it only, because based on valA and valB website will load datatables script based on it and will produce table from database one of the values is just a key to table, another to database.
Maybe you can join your required values with some delimiter, for example:
<select id="store" type="text" name="s">
<option value="value1:value2">Selection 1</option>
<option value="value3:value4">Selection 2</option>
</select>
Your url becomes:
http://mywebsite.com/index.php?s=value1:value2
On server side you can:
$parts = explode(':', $_GET['s']);
You will need javascript to achieve that without php
If your php is whats receiving the call then why don't you have a value seperated by a delmiter value="value1|value2"
then in your php I would do the following
$values = explode("|",$_GET['s']);
then you have $values[0] and $values[1] - containing the two values
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 got an HTML form which allows a user to select multiple options in a dropdown. I then pass that data on as post data to a PHP backend (I'm using codeigniter for the backend, and the data is being passed to a model).
In javascript, I can log the value being passed, and if there are multiple values, it shows as a proper, comma separated string of values. But if log the value in PHP, it only lists the last of the multiple values. How do I get it to retain all selections? This is my code:
FORM:
<form action="http://localhost:8888/index.php/control_form/add_all" method="post" accept-charset="utf-8">
<label for="sel_dep">Member Dependencies</label>
<select id="sel_dep" name="sel_dep" multiple>
<option value=""></option>
<option value="4">Soundgarden</option>
<option value="5">Rage Against the Machine</option>
<input type="submit" name="submit" value="Submit" />
</select>
</form>
PHP Codeigniter Model:
function edit_member(){
if (isset($_POST['submit'])) {
$v_memberdep = $this->input->post('sel_dep');
log_message('debug', 'DEP: ' . $this->input->post('sel_dep'));
}
}
Set the name of your select box to f_memberdep[] the [] will tell PHP that it should be passed as an array so you will receive all values.
Also should point out that your logging a field called sel_dep when your select box is called f_memberdep but that's probably just a formatting thing.
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.