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)
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.
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'];
I don't know if what I want to do is possible (or too complicated).
I have a form that inserts the data into my DB. In it I have a select. I want to hide or remove the options that are already in my DB (so the user can't see them).
Every user has his own .php page where I edit the form with new options after the old ones are used.
PHP (In the same page, above the form):
<?php
if ($registro != "") {
include("conexao.php");
$conexao = #mysql_connect($host,$user,$pass)
or die("Cadastro");
$query = "INSERT INTO certificados VALUES ('$id','$registro','$name', now() )";
mysql_select_db($db);
mysql_query($query,$conexao);
}
else {
?>
HTML:
<form name="inserir" action="<? echo $PHP_SELF; ?>" method="post">
<select name="registro" id="registro">
<option value="">Select one</option>
<option value="11891">11891</option>
<option value="11892">11892</option>
<option value="11893">11893</option>
<option value="11894">11894</option>
<option value="11895">11895</option>
</select>
<input type="text" name="name" placeholder="Name">
<button type="submit" id="submit" name="B3">Send</button>
</form>
Edit 1: I'll try to explain better what I want to do.
Imagine that the user selected the first option value "11891" and submitted the form (thus inserting this value in the DB). After the page reloads, this value is no longer listed for him in the select. So based on the form that I posted here, now he would only have the options "11892", "11893", "11894" and "11895".
PS: Sorry for any mistakes or use of outdated parameters. My father wrote this code (he has some knowledge of php). In my case, my knowledge is almost nonexistent.
Thank you all for the help.
You should have 2 more tables. One 'tb_options' where you retrieve all the options. Another one 'tb_users_options' where you store all option based on user ID. So when retrieving the options from tb_options you select only those not present in tb_users_options Taking into account the user ID. This is a logic matter.
I'm using this following simplified HTML code integrated in a PHP code, which is left out.
The user can choose between two options of the dropdown selection and then enters an input to be submitted.
The problem is that whenever clicking on the submit button after an input is entered and option2 is selected, then the dropdown selection is changed back to option1.
Now, I want option1 to be the default when the page is first loaded, but I'd also like that whenever option2 is selected - that it would be "anchored", that is it wouldn't be changed unless the user selects a different option or when the page is refreshed....
Does anybody know how to do that?
Thanks in advance!
P.S - don't know if this matters but in the original code, I entered the PHP file name in the "action" attribute inside the form tag.
<html>
<head>
</head>
<body>
<form action="" method="post" >
Category:
<select id="searchType" name="searchType" size="1">
<option value="1" class="bold-option">
Option1
</option>
<option value="2">Option2</option>
</select>
<br/>
<input type="text" name="name" class="input_field" id="input_txt" dir="auto" >
<input type="submit" value = "submit!" class="submit_button"/>
</form>
</body>
</html>
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.