This question already has answers here:
Get $_POST from multiple checkboxes
(5 answers)
Closed 7 years ago.
I have a list of checkbox (I need to give an id to each), then to make POST to another page
I have a while loop that which brings me one checkbox for each item in the database, I need to make each checkbox to make the POST, on the other page one check or X displays
Edit page.php
while ($fila = mysql_fetch_array($rs)) {
echo "<td > <input type='checkbox' /> </td>}
AnotherPage.php
I guess I have to give an id to each checkbox to make the post and to get on another page.
This seems to be a duplicate of this question: Get $_POST from multiple checkboxes
See the checked answer there.
The main thing in PHP based applications is that a form element with a name with name[] (square brackets at the end) will result in an array in the receiving PHP script. That allows you to have all the values the user selected.
There must be something like this
<form action="AnotherPage.php" method="post">
<input type="checkbox" name="check1" value="1">
</form>
And then in php
if (!empty($_POST['check1'])) {
// make your logic
}
<?php
while ($fila = mysql_fetch_array($rs)) {
<td> <input type='checkbox' name='CheckBoxName[]' value="<?echo $fila['GiveValueHere'];?>"/> </td>
}?>
SomePage.php (Submit Page)
<?
$totalCheckBoxChecked=sizeof($_POST['CheckBoxName']);
for($i=0;$i<$totalCheckBoxChecked;$i++)
{
echo $checkBoxValue=$CheckBoxName[$i];
echo "<br>";
}
?>
Related
I have been working on my own PHP project. I have hit an obstacle. I am trying to retrieve the results of a database and print out a form for each result set.
I then wish to interact with one particular result either be deleting it or passing it into a function etc..
Heres is my current code :
<?php
while($row = mysqli_fetch_array($result)){
echo '<div id="post">'; ?>
<form action="" method="post">
<?php
echo "<font size=4>".$row['post']."<br>";
echo "posted by : ".$row['username']."<br>";
$id = $row['p_id']; ?>
<input type="submit" name="choice" value="Y">
<input type="submit" name="choice" value="N">
</form>
<br>
</div>
}
<?php
if($_POST['choice']=="Y"){
// progress
functionA();
}
else if($_POST['choice']=="N"){
// delete or remove
functionB();
}
?>
So my goal here would be click Y to progress that particular result or N to delete/remove the result.
However currently by clicking either button all results either get deleted or progress. I do know that the id should be used to differentiate between posts but I cant quite seem to get it to work. Once the button is pressed it passes all results to either function.
First of all, I suppose you want to know the record to delete. So, add an input to your form:
<input type="hidden" name="id" value="<?php echo $row['p_id']; ?>">
Then, in your script, call:
if( $_POST['choice'] == "Y" )
{
// progress
functionA( $_POST['id'] );
}
elseif( $_POST['choice']=="N" )
{
// delete or remove
functionB( $_POST['id'] );
}
Additional problem: how you can use your db connection inside the functions? Assuming your mysqli connection is named $conn, call the function(s) in this way:
functionA( $_POST['id'], $conn );
Side note: First process $_POST values, then retrieve db records and print it.
Side note 2: take a look at prepared statement.
Read more about variable scope
Read more about prepared statements
Use two different <input type="radio">s for each item with the id in the value and let the user select which to delete and which to process.
So for an item with id 1 the outputted HTML would look like:
<input type="radio" name="items[1]" value="delete"><input type="radio" name="items[1]" value="process">
The name forces the items to go to _POST as an array with the ids as keys and the selected action as their values.
Example html: https://jsfiddle.net/gu1gkwod/
I am doing project using Zend framework. here within the form I add field using radio buttons. after the form post. it doesn't send that radio button value(but other fields (eg -text field can post)). this is my code in the view.
<form class="custom" method="post">
<?php
foreach ($answers as $answer) {
echo '<input name="q_answer" value="'.$answer.'" type="radio" >'.$answer;
}
?>
<input class="small secondary button" type="submit" value=" Ok ">
</form>
this is my code within controller
if($request->isPost()){
$ans = $_POST['q_answer'];
}
so when I post the form. it gives Undefined index: q_answer error. what is the wrong. please help me.( within the controller I print posted values using var_dump but 'q_answer' value not available)
If no option is selected this field is not appear in $_POST. So you should first check with isset() if it is present and the try to process. And while you are using ZF, you should use getPost()instead of digging directly in $_POST:
$ans = getPost( 'q_answer', 'default-value-if-no-element-is-found' );
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Submit an HTML form with empty checkboxes
I'm try to submit some data to the database. I'm trying to submit the value for a checkbox in the form to the database as follow:
<input type='checkbox' name='continue' <?php if ($_POST[continue] == TRUE) echo "value='y' checked='checked'"; else echo "value='n'"; ?> />
when I submit the data when the checkbox is checked, the value will be 'y'. but when the checkbox is checked, there is no data sent to the database.
Please advise.
Solution 1: (the better way)
<input type="checkbox" name="continue"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> />
In your database (?) file:
$continue = isset($_POST['continue']) ? 'y' : 'n';
.
Solution 2: (not recommended)
<input type="hidden" name="continue" value="<?php echo isset($_POST['continue']) ? 'y' : 'n'; ?>" />
<input type="checkbox" name="continue_x"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> onclick="document.getElementsByName('continue')[0].value=this.checked?'y':'n';" />
First of all, when you are trying to validate the checkbox using PHP is not going to do the live check unlike JavaScript that you can do it right away without the need to interact with the server side.
So what were you trying to do I assume that you want to insert a data from the form to the database.
<input type='checkbox' name='continue' <?php if (isset($_POST[continue])) echo
"checked='checked'"; ?> />
So what you are doing with the above code is to check using isset function if the checkbox is checked and you recall the form again the checkbox will still preserve the state and it will still be checked.
Now come to the part where you need to get the value to insert into your database.
if (isset($_POST[continue]))
$cont = 'y';
else $s = 's';
You will need this before you are going to insert the value to the query and instead of doing $_POST[continue] now just use $cont as a value in the query and you will get the correct value.
Try this
<input type='checkbox' name='continue' <?php if ($_POST['continue']==TRUE) { echo "value='y' checked='checked'"; } else { echo "value='n'"; } ?> />
Checked checkboxes are successful (and submit their values), unchecked checkboxes are not (and don't).
You need to give the checkbox a fixed value and then determine if it was checked or not based on the presence (or absence) of the data, not the specific value of data.
The value only matters when you have multiple checkboxes with the same name (e.g. for "Tick every mode of transport you use on a regular basis" type questions.)
I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can I get the name of submit button in another form?
Hi,
I have a form which has 3 submit buttons. Their names are generated and assigned in a loop. Now if I use a post method, how can access the name of the submit button which was clicked.
The following is the example of my form:
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
?>
<button type="submit" name="<?php echo $i ?>" value="<?php echo $i ?>" >
</button>
<?php
}
?>
</form>
May be I can use onsubmit attribute for the button tag in one.php, but I am unable to get the output. Any suggestions?
You can examine the $_POST array to see which number was sent through.
How you do that is entirely up to you, it could be as basic as a few isset() checks.
if (isset($_POST['1'])) {
echo "Clicked button 1";
}
if (isset($_POST['2'])) {
echo "Clicked button 2";
}
if (isset($_POST['3'])) {
echo "Clicked button 3";
}
It would probably make more sense to use the same name and different values for multiple submit buttons.
<form name="one" method="post" action="two.php">
<?php
while($i=1;$i<=3;$i=$+1)
{
echo "<input type='submit' name='submitButton' value='{$i}' />";
}
?>
</form>
In two.php you can then simply pull the value of the submit button that was pressed using $_POST['submitButton']