PHP can't POST the selected value from a dropdown - php

<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<?php
echo "<div class = 'dropdown_container'><select class = 'dropdown select xs-mt1' name = 'line' id = 'line'>";
foreach($ptv_json_bus as $item)
{
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
?>
<option value = "<?php echo $stopid;?>"> <?php echo $stopname;?></option>
<?php
}//end foreach
?>
<input type = "hidden" value ="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type = "hidden" value ="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
</select>
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>
Hey guys. Above is the code I'm working with.
What I am trying to achieve is to POST the selected value's stop ID and stop NAME (from a dropdown) to another file which then adds the data into mySQL.
What my problem is right now is that the current code I have is actually only POSTing the last set value of $stopid and $stopname which is not what I need.
I've been stuck on this for quite a while and I would love it it somebody here could point me in the right direction!!
Thank you

Move your <input> elements outside of the <select> element.
You can only put <option> elements inside of your <select> element.
I've improved your version a bit.
<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<div class = 'dropdown_container'>
<select class="dropdown select xs-mt1" name="line" id="line">
<?php
foreach ($ptv_json_bus as $item) {
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
echo "<option value='$stopid'>$stopname</option>";
}
?>
</select>
<input type="hidden" value="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type="hidden" value="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>

Related

How to use GET to retrieve check box selected value in php

I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)

HTML Form $_POST does not work

menus.php
<?php
$con=mysqli_connect("localhost","root","","mmogezgini");
$menuler = mysqli_query($con,"SELECT * FROM menuler");
while($menu = mysqli_fetch_array($menuler)) {
$isim = $menu["isim"];
$url = $menu["url"];
?>
<form method="post">
Menü İsmi :<input name="menu_isim" value="<?php echo $isim; ?>" disabled> |
Menü URL : <input name="menu_url" value="<?php echo $url;?>"disabled>
<button type="submit" formmethod="post" formaction="menu_duzenle.php">Düzenle</button>
<button formaction="menu_sil.php">Sil</button>
</form>
<br>
<?php
}
?>
edit_menu.php
<form method="post" action="menu_duzenle_islem.php">
Şuanki İsim : <input name="menu_isim" value="<?php echo $_POST['menu_isim'] ?>"disabled>
Yeni İsim : <input name="yeni_menu_isim" placeholder="yeni menü ismini giriniz.">
</form>
My Problem is form in menus.php wont send $_POST['menu_isim'] to *edit_menu.php*.need more to write for details
Disabled inputs are not considered "valid" for being passed through a form.
To make an input read-only, use the readonly attribute instead.
Keep in mind, however, that this kind of thing CANNOT be trusted. You should save the value in a $_SESSION variable and retrieve it from there.
Remove the disabled attribute. If you want the user not to edit the input use readonly attribute.

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

Can I submit from a second form and retain POST data from the first?

Hopefully I'm making this more difficault then it has to be. Here is what I'm trying to do. I have a form that does a POST and returns data. I have a second form that then asks the user a yes/no question based on data from the first form. Is it possible to capture the POST data from the first form submission and pass it along with the second form POST?
Here is my scenario
if ($_POST['button_1']) {
$params = $_POST;
print_r($_POST);
// process form data
}
if ($_POST['button_2']) {
// Retain the POST data from the first submission
$new_params = $params . $_POST;
print_r($new_params);
// process form data and do some additional stuff
}
<form id="form_1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_1" id="button_1"/>
</form>
<form id="form_2" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_2" id="button_2"/>
</form>
Is there a way to do this easily or am I over complicating this?
You can either use a hidden field in your second form, or sessions. Start reading here:
http://www.php.net/manual/en/book.session.php
Yes, the standard way is to use type = "hidden" fields to pass this context data onto the second form. There are lots of worked examples to see how to do this. View the source of this HTML page or any other with forms on them and search for "hidden" to see how the applications do this.
There are a few ways to "fake" this.
Have the first form submit to itself and just load the $_REQUEST variables and use them to populate the second form with the appropriate data/options.
have the 2nd form loaded via ajax once the first is submitted and use javascript to grab the current form variables and provide them to the ajax function.
Which method would you prefer?
UPDATE:
This is long, but a working example
<!DOCTYPE html>
<html>
<body>
<form name="first" method="post">
<input type="hidden" name="action" value="firstformdone">
<div style="width:100px;float:left;">
Age:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="age">
</div>
<div style="clear:both;"></div>
<div style="width:100px;float:left;">
Name:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="name">
</div>
<div style="clear:both;"></div>
<br>
<?PHP
if (!$_REQUEST['action'] == "firstformdone") {
?>
<input type="submit" value="contine">
<?PHP
}
?>
</form>
<?PHP
if ($_REQUEST['action'] == "firstformdone") {
?>
<form name="second" action="something_else.php" method="post">
<input type="hidden" name="age" value="<?PHP echo $_REQUEST['age']; ?>">
<input type="hidden" name="name" value="<?PHP echo $_REQUEST['name']; ?>">
<div style="width:150px;float:left;">
Preferred games:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<select name="games">
<option value="">Select games</option>
<?PHP
if ($_REQUEST['age'] <= 10) {
?>
<option value="tlddlywinks">Tiddly Winks</option>
<option value="Jacks">Jacks</option>
<option value="Go-Fish">Go-Fish</option>
<option value="Hid-And-Go-Seek">Hid-And-Go-Seek</option>
<?PHP
} else {
?>
<option value="Halo">Halo</option>
<option value="StarWars">The Old Republic</option>
<option value="LaserTag">Laser Tag</option>
<option value="spin-the-bottle">spin-the-bottle</option>
<?PHP
}
?>
</select>
</div>
<div style="clear:both;"></div>
<br>
<input type="submit" value="Next!">
</form>
<?PHP
}
?>
</body>
</html>
You need to package the data from the first form up for resubmission. You can use a hidden fields for that job:
foreach ($_POST as $key => $value) {
print "<input type='hidden' name='".htmlspecialchars($key, ENT_QUOTES, "UTF-8")."' value='".htmlspecialchars($value, ENT_QUOTES, "UTF-8")."'>";
}

How do I receive post from a form where I name an input as <?php echo $var ?>?

I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />

Categories