PHP. How to get $_POST value after table disappears from HTML - php

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.

Related

why i can not get the html select option values in php?

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).

get the text of the selected option using php [duplicate]

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>

PHP Dropdown button, MySQL update

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)

Need help regarding submitting values through a form with select Box?

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

PHP: Need a double check on an error in this small code

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.

Categories