I have got a while loop that runs through all the records of the database printing them on a table. Now i also have some checkboxes within that very loop that I want to use to submit a form when clicked. Now when I click the checkbox it will indeed submit the form thanks to a Jquery script I found, BUT whenever i submit it it submits with the ID of the first record of the table.This Image
shows the table, as you see the first record has ID 34. Now every checkbox I click will send the $id 34.
This does not happen with normal submit buttons.
Is there a way I can submit with the individual userID's
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
I'm sorry if i'm not very clear with the explanation it is quite hard to explain this situation. Thank you guys!
Probably your problem is that you are submiting the same form always and its because you create a form for each row but it has the same id
For you the easy way is to put each form with the id cointaining the unique value of the row and doing submit with that.
Something like this
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete_<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete_<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
It looks like the <form> your creating has a static id, so ALL forms will have id='resComplete'. The jQuery submit function will grab the first element with id='resComplete' and submit it. You need to make it unique for every form and make the onchange='$("#resComplete").submit();' code match it.
Eg.
<?php
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete-<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete-<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
Better yet, use jQuery to find out what form it's in by chaning the onchange to something like:
<input id='complete' type='checkbox' name='complete' value='1' onchange='$(this).closest('form').submit();' <?php if($complete == 1){echo "checked";}?>>
Related
echo "<form method=\"post\" action=\"settings.php\" onchange=\"this.form.submit()\">";
echo "Announce New Files: <input type=\"radio\" name=\"announcefiles\" value=\"On\" $checkon1> On";
echo "<input type=\"radio\" name=\"announcefiles\" value=\"Off\" $checkoff1> Off<br>";
echo "</form>";
I am trying to get this form to submit when one of the radio buttons is pressed but I'm not sure how to catch the submission.
for example, normally with a submit button you would use something along the lines of if(isset($_POST['submit'])) but I'm not sure how to do it if the form auto submits.
Add hidden input field like:
<input type="hidden" name="action" value="submit" />
Then in PHP check:
if(isset($_POST["action"]) && $_POST["action"] == "submit") { ... }
You should be checking the request method. If you've set things up cleanly, a POST request at that URL will mean a form submit. As you've noticed, you can have attempted submits where a value just isn't there.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
See $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' for more discussion.
Give your form a name and check for isset($_POST['form_name']) or check for the name of the radio isset($_POST['announcefiles'])
Also, you don't need all the quote escaping that you have, you can use single quotes as well as use a multiline string - see example below.
echo "
<form method='post' name='form_name' action='settings.php' onchange='this.form.submit()'>
Announce New Files: <input type='radio' name='announcefiles' value='On' $checkon1> On
<input type='radio' name='announcefiles' value='Off' $checkoff1> Off<br>
</form>";
<?php
// Check if form was submitted
if (isset($_POST['form_name']) {
// Form submitted
}
?>
<?php
// Check if radio was selected
if (isset($_POST['announcefiles']) {
// Form submitted
echo 'You chose' . $_POST['announcefiles'];
}
?>
Try this:
You may have an easier time if you separate the php and html a little more.
<form method="post" action="settings.php" onchange="this.form.submit()">
<fieldset>
<legend>Announce New Files:</legend>
<label for="on"><input type="radio" id="on" name="announcefiles" value="On" <?php echo $checkon1 ?> /> On</label>
<label for="off"><input type="radio" id="off" name="announcefiles" value="Off" <?php echo $checkoff1 ?> /> Off</label>
</fieldset>
</form>
Then in your php logic in settings.php ( or above the form if you are posting back to the same page ) you can check for the value of announcefiles:
<?php
if(isset($_POST['announcefiles'])){
// DO SOMETHING
}
?>
Let me know if this helps. Or if I'm missing the question.
I have several forms with radio buttons on a page. My goal is to have the database updated immediately on the click of a radio button. Here is what I have put together so far, but I'm not sure where to go from here.
<? include 'dbconnect.inc.php';?>
<script type="text/javascript">function do_submit(){document.forms['decision'].submit();}</script>
<? $result = $mysqli->query("SELECT * FROM items");
while( $row = $result->fetch_assoc() ){ ?>
<form name='decision' method='post' action='action.php'>
<label>Keep:</label><input type='radio' name='dec' value='keep' onchange='do_submit()' <? if($row['Dec1']=='keep'){echo "checked='checked'";} ?> >
<label>Donate:</label><input type='radio' name='dec' value='donate' onchange='do_submit()' <? if($row['Dec1']=='donate'){echo "checked='checked'";} ?> >
<label>Sell:</label><input type='radio' name='dec' value='sell' onchange='do_submit()' <? if($row['Dec1']=='sell'){echo "checked='checked'";} ?> >
<label>Trash:</label><input type='radio' name='dec' value='trash' onchange='do_submit()' <? if($row['Dec1']=='trash'){echo "checked='checked'";} ?> >
<label>Give To:</label><input type='text' name='dec' size='15' onchange='do_submit()' <? if($row['Dec1']!='keep' || $row['Dec1']!='donate' || $row['Dec1']!='sell' || $row['Dec1']!='trash'){echo $row['Dec1'];} ?> >
<input type='hidden' name='Id' value='<? echo $row['Id']; ?>' />
</form>
<? } ?>
action.php
<? $mysqli->query("UPDATE items SET Dec1 = '{$_POST['dec']}' WHERE Id = '{$_POST['$Id']}'") or die(mysqli_error($mysqli)); ?>
Am I anywhere close? Can someone help me through this?
This is untested, but you'd probably want to go with something like this (AJAX + jQuery). Would need quite a bit added to it to pass all your variables through properly.
$(":radio").click(function(){
$("#result").html(ajax_load);
$.post(
loadUrl,
{myvariable: "myvalue", myothervariable: "myothervalue"},
function(responseText){
$("#result").html(responseText);
},
"html"
);
});
Here's a good, supplementary read for you as well: 5 Ways to Make Ajax Calls with jQuery
Bind an ajax onclick event to send the command to your action script.
Inline php is not the way to go.
I have this form which allows the input of any product quantity from 1-10:
<form method='post' action='cart.php'>
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
<input type='submit' value='Update'>
</form>
And another form (button) to display a selection of payment modes:
<form action='cart.php' method='post'>
<input type='hidden' name='next'>
<input type='submit' value='Select Payment Mode'>
</form>
What I want to happen is that when a user did not input anything (1st form), ex. null or 0, I want to display an alert box that says 'Product quantity can't be null or 0'.
Here's my code for that:
if (isset($_POST['next'])) {
if ($_POST['quantitychange']==null || $_POST['quantitychange']==0) {
?>
<script type='text/javascript'>
alert('Product quantity can't be null or 0.');
</script>
<?php
}
else {
echo "
//Payment modes here
";
}
}
The error is that even when a user inputs a quantity bet. 1 to 10, it still displays the alert message. Any help? Thank you.
By the way, the input type "number" only works in Google Chrome browser.
Use a small javascript (or jQuery) function to validate the form before posting it. Have this function throw up the alert if your condition isn't met and then return false. If the condition is met, return true, and it gets submitted.
Edited to add since this might get googled, I'll help a bit with code snippet I have used. The below example is jQuery and was used in production for a web application I made for my employees. document.form.doit.submit(); should be the pure javascript way of submitting the form.
<script type="text/javascript">
function subForm() {
// document.form.doit.submit();
if( test condition passes ) {
$('#save_order').submit();
}
}
</script>
<form id="save_order" action="oms_db.php" method="POST">
<input id="doit" type="button"
value="i am a button" onClick="subForm();">
</form>
I think you have some error in your forms. Instead of the below:
<input type='number' name='quantitychange' size='2' min='1' max='10' value=".$_SESSION["itemsSelected"][$i][1].">
<input type='hidden' name='ProductID' value=".$_SESSION["itemsSelected"][$i][0].">
you should be using something like this:
<input type='number' name='quantitychange' size='2' min='1' max='10' value="<?php echo $_SESSION["itemsSelected"][$i][1]; ?>">
<input type='hidden' name='ProductID' value="<?php echo $_SESSION["itemsSelected"][$i][0]; ?>">
The value parameters in the hidden input fields needs to be echoed from PHP. What you have now is like the value is simple strings ".$_SESSION["itemsSelected"][$i][0].".
I suggest you use
if(empty($_POST['quantitychange'])) { echo 'yourerror'; }
As it is far cleaner then your script. (http://php.net/manual/en/function.empty.php)
Update:
Also, you can't use two seperate forms like you do, your browser only posts whats between
<form>
</form>
Using only one will fix your problem.
I want to pass the same boolean value "isProvena" several times in two PHP files. The first time I pass the value from utilitizer.php to hraprint.php by using the codes as follows:
if ($_POST['type'] == 'printphys' || $_POST['type'] == 'printprovenahpa')
{
echo "<form action='/content/822' method=post>";
echo "<input type=hidden name=filename value='$filename'>";
if($_POST['type'] == 'printprovenahpa') {echo "<input type=hidden name=isProvena value='1'>";}
echo "<input type=hidden name=content value='";
if ($_POST['type'] == 'printphys') echo 751;
else if ($_POST['type'] == 'printprovenahpa') echo 520;
echo "'>";
echo "<input type=submit value='Start Job'></form>";
}
And then I get the value "isProvena" from hraprint.php and post(get) again:
$isProvena = false;
extract($_REQUEST, EXTR_IF_EXISTS);
$isProvena = (boolean)$isProvena;
<form action="/content/822" method="GET">
<input type="hidden" name="isProvena" value="<?php echo ($isProvena) ? '1' : '0' ?>" />
<tr>
<td><label for="showOnlyScreening">Print Only Screenings:</label></td>
<td><input id="showOnlyScreening" type="checkbox" name="showOnlyScreening" value="1" <?php echo ($isProvena) ? 'checked="checked"' : ''?>/></td>
</tr>
<tr>
</table>
</form>
And post again:
<form action="/content/822" method="POST">
<input type="hidden" name="isProvena" value="<?php echo ($isProvena) ? '1' : '0' ?>" />
</table>
And I do the judgement here:
if($isProvena){
.........
}
The reason I need to post(get) several times is that there are several page redirect action happens in the same PHP file(hraprint.php). When I was trying to get the value which is supposed to be 'true' from if($isProvena){} and execute the function, I failed.
Anyone can help me to have a look and tell me what is wrong?
It would be easier if you simply use sessions for that. Sessions are made specifically for that purpose - passing variables easily from one page to another.
And it is not yet established in the last code block of your answer that $isProvena already exists because I do not see any extract() there.
P.S. Use the $_POST and $_GET variables instead of extracting the $_REQUEST. The code is vulnerable to the problems caused by register_globals
When I pass the html form having checkboxes to a php page, when I retrieve them I get value true for all the boxes that I have checked, and false for unchecked boxes. I need to get the value only.
<?php
$contact=$_POST['contact'];
foreach ($contact as $conid){
echo $conid
}
?>
One possible approach is to set names like that:
<input type='checkbox' name='box[1]'>
<input type='checkbox' name='box[2]'>
<input type='checkbox' name='box[3]'>
This way you could access them in PHP with
foreach ($_POST['box'] as $id=>$checked){
if ($checked =='on')
//process your id
}
The second approach is more clean, I think. Set value's attributes on checkboxes:
<input type='checkbox' name='box[]' value='1'>
<input type='checkbox' name='box[]' value='2'>
<input type='checkbox' name='box[]' value='3'>
With this you'll receive only checked values:
foreach ($_POST['box'] as $id){
//process your id
}