Execute query on button click If checkbox is checked - php

I want to execute a script if checkbox is checked when I click on the submit button.
I got the following code snippet in my overview.php:
<table style="width: 100%; font-size: 11;">
<tr>
<td colspan="6">
<form method="post" action="update.php" target="_blank">
<script language="JavaScript">
function toggle(source) {
checkboxes = document.querySelectorAll("input[name^='box[']");
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<input type="submit" name="update" value="Update - LoL / BoL">
<input type="checkbox" onClick="toggle(this)" name="All" value="All"> Select all
</form>
</td>
</tr>
<tr>
<th>ve067</th>
<th>ve378</th>
<th>ve724</th>
<th>ve725</th>
<th>ve726</th>
<th>ve727</th>
</tr>
<tr>
<td>
<form method="post" action="update.php">
<input type="checkbox" name="box[1]" value="ve067_LB1"> LeagueBot 1<br>
<input type="checkbox" name="box[2]" value="ve067_LB2"> LeagueBot 2<br>
<input type="checkbox" name="box[3]" value="ve067_LB3"> LeagueBot 3<br>
<input type="checkbox" name="box[4]" value="ve067_LB4"> LeagueBot 4<br>
</form>
</td>
</tr>
</table>
And this is a code snippet from my update.php:
<?php
if(isset($_POST['update'] && $_POST['box[4]']) {
// code here
}
?>
But it does not work for me.

You can do it as I have mentioned in a post on pastebin
Hope it helps you dude.

Solution: Consider the following JSFiddle
Explanation: This will validate to see that at least checkbox 'box[4]' has been ticked.
The "key" code here being the return function in the following line.
<input type="submit" name="update" value="Update - LoL / BoL" onclick="return validate();">
Update: Made to accommodate extra information given in comment.
Remember: The validation here is done on the client side so that it doesn't need to query the server (PHP) first

Related

$_POST empty when using IntelliJ

I am writing a simple web page to demonstrate how to use forms in PHP. The page works fine when run on the university's web server; but, when I try to run it locally through IntelliJ 16, $_POST is always empty (but php://input does contain the correct post string). The other aspects of the demo work fine.
I've read many other posts about $_POST being empty; and, from what I can tell, those situations solutions don't apply here.
I'm assuming that there is a configuration problem with my local copy of php; but, I don't know what could be mis-configured that would cause $_GET to work, but not $_POST.
This is the relevant html form:
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
The complete (working) example is here: http://www.cis.gvsu.edu/~kurmasz/StackExchange/formDemo.php
Adding <?php error_reporting(E_ALL); ?> produces no errors or
warnings.
$_SERVER["HTTP_CONTENT_TYPE"] = "application/x-www-form-urlencoded"
What should I check next?
Complete PHP source:
<!-- This file demonstrates
(1) How to set up a simple HTML form
(2) How to use PHP to access the data
-->
<?php error_reporting(E_ALL); ?>
<html>
<head>
<title>Form Demo</title>
<style type="text/css">
#get, #post {
vertical-align: top;
}
</style>
</head>
<body>
<h1>Form Demo</h1>
<h2>Questions:</h2>
<ul>
<li>Can you get data in both <code>$_GET</code> and <code>$_POST</code> at the same time? If so, how. If not, why
not?
</li>
<li>What happens if you leave a text box empty?</li>
<li>Can you "unselect" all the radio buttons? If so, how. If not, why not?</li>
<li>Can you select "Fred" and "Barney" at the same time? Can you select "Barney" and "Trumpet" at the same time?
What's the difference?
</li>
<li>What happens if you unselect all the check boxes?</li>
<li>How does PHP treat checkboxes differently when doing a POST than when doing a GET?</li>
<li>How can you tell which submit button was pushed?</li>
</ul>
<table>
<tr>
<td id="get">
<fieldset>
<legend>GET form</legend>
<form action="formDemo.php" method="get">
"text" type input named <code>theTopOne</code>
<input type="text" name="theTopOne" value="Value for Top input"/><br/>
"text" type input named <code>theSecondOne</code>
<input type="text" name="theSecondOne" value="Value for Input #2"/><br/>
Radio buttons named "flintstones":
Fred <input type="radio" name="flintstones" value="iChooseFred" checked="checked"/>
Barney <input type="radio" name="flintstones" value="iChooseBarney">
Wilma <input type="radio" name="flintstones" value="iChooseWilma"></br>
Radio buttons named "instruments":
Saxophone <input type="radio" name="instrument" value="sax"/>
Trumpet <input type="radio" name="instrument" value="tpt">
Piano <input type="radio" name="instrument" value="piano" checked="checked"></br>
Checkboxes named "courses":
451 <input type="checkbox" name="course451" value="451"/>
452 <input type="checkbox" name="course452" value="452"/>
457 <input type="checkbox" name="course457" value="457"/><br/>
<input type="submit" name="getSubmit1" value="Submit Button 1"/>
<input type="submit" name="getSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_GET</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_GET as $key => $value) {
echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
?>
</table>
</td>
<td id="post">
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
"text" type input named <code>theSecondPost</code>
<input type="text" name="theSecondPost" value="Value for Post #2"/><br/>
Radio buttons named "interest":
Cool <input type="radio" name="interest" value="itsCool" checked="checked"/>
OK <input type="radio" name="interest" value="itsOK">
Boring <input type="radio" name="interest" value="itsBoring"></br>
Radio buttons named "bestState":
Georgia <input type="radio" name="bestState" value="GA"/>
Michigan <input type="radio" name="bestState" value="MI">
California <input type="radio" name="bestState" value="CA" checked="checked"></br>
Checkboxes named "IScourses":
260 <input type="checkbox" name="IScourses[]" value="cis260" checked="checked"/>
333 <input type="checkbox" name="IScourses[]" value="cis333"/>
463 <input type="checkbox" name="IScourses[]" value="cis463"/><br/>
<input type="submit" name="postSubmit1" value="Submit Button 1"/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_POST</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_POST as $key => $value) {
$printMe = $value;
if (is_array($value)) {
$printMe = "[" . implode($value, ", ") . "]";
}
echo "<tr><td>$key</td><td>$printMe</td></tr>\n";
}
?>
</table>
</td>
</table>
<?php
$fp = fopen("php://input", 'r+');
echo stream_get_contents($fp);
?>
<hr>
<?php var_dump($_SERVER); ?>
</body>
</html>

2 forms on same page php

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values

How to update the data in particular table row in HTML?

Since the <form> can not inside <tr>, I can not assign the form for each row. And at the end I have create a from like this
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
So, it seems when I submit data it submit every input, how can I send only a specific row? Thanks
Since forms send all the values, (since your using <button>), you could assign each row a key that corresponds to that row. Then use that in the textbox. Note: Textbox values must be an array structure, so that particular key from the submit update can be used (kinda like filtering). Consider this example:
<?php
if(isset($_POST['update'])) {
$product_key = $_POST['update'];
$product_name = $_POST['my_product_name'][$product_key];
echo $product_name;
// this should correspond to that same textbox row that you selected
}
?>
<form method="POST" action="">
<table>
<tr>
<td><input type="text" name="my_product_name[1]"></td>
<td><button name="update" type="submit" value="1">Update</button></td>
</tr>
<tr>
<td><input type="text" name="my_product_name[2]"></td>
<td><button name="update" type="submit" value="2">Update</button></td>
</tr>
</table>
</form>
This is not possible. A form sends everything in it. You have to use multiple forms in order to do that. Can you explain why you want to send each row separately. Let us know the complete problem so we can provide you much better solution :)
You must use separate forms. To avoid this issue, you could try using div's and style them with css.
<div class="big-wrapper">
<div class="form-wrapper">
<form id="first-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 1</button>
</form>
</div>
<div class="form-wrapper">
<form id="second-input">
<input name="my_product_name">
<button type="submit">Click Here To Submit Form 2</button>
</form>
</div>
Example of above code (with no additional css): http://postimg.org/image/p0olzl933/
One method would be to have form tags that wrap tables and each table effectively becomes a row.
Honestly, there are a lot of ways to do this... you could handle it using Javascript with something like I have below or generating a special table with a server-side script and writing that to the page. You could use ajax calls to send the values that change to a web service, etc.
Hope that thelps.
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
<form>
<table>
<tr><td><input name="my_product_name"></td><td><button type="submit"></td></tr>
</table>
</form>
...
Collect the value then push it into the form on submit.
<script>
function dosubmit(e){
var value = $(this).parents('tr').find('input.some').val();
$(this).parents('tr').find('input.my_product_name').val(value);
}
</script>
<table>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><input name="some"/></td>
<td>
<form onsubmit="dosubmit(event)">
<input type="hidden" name="my_product_name"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
</table>
I don't know how much good a jsbin will be, but here it is
You can submit the value in form by setting in hidden parameter as follows:
HTML:
<form action="" id="formId">
<input type="hidden" name="product_name" id="hiddenValue" />
</form>
<table>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
<tr>
<td><input name="my_product_name"></td>
<td><button type="submit" onclick="submitForm(this)"></td>
</tr>
</table>
Script:
<script>
function submitForm(id){
var input = id.parentElement.parentElement.getElementsByTagName("input")[0].value;
//Retrieving value of specific rows
document.getElementById("hiddenValue").value = input;
//Submits the form with given id.
document.getElementById("formId").submit();
}
</script>

How to submit two php forms to one location

I have two php forms, both for registration means (enter name, username, password etc). My first registration form (reg1.php) contains the basic inputs as I mentioned above and then I have a second registration form (reg2.php) that includes checkboxes where the form would ask the user other questions before finalizing their sign up.
What I am having trouble with is that when I enter data into my inputs in the first registration form, then proceed to the next registration page and fill that out and click Sign Up, my output on my server only obtains the information I inputted in the second registration page, not the first.
How can I get both inputted data from the two registration pages to appear together? I have been told about POSTBACK to help with this but I am not all that familiar with how to use it.
First php page:
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table style="width:300px">
<form name="Rego" method="post" action="rego2.php">
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pass" /></td>
</tr>
<tr>
<td><label>Confirm Password:</label></td>
<td><input type="password" name="conpassword" id="conpass"/></td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Next Page">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
Second php page
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
When the first form is submitted either store the data in a $_SESSION variable and retrieve it when you process the second form, or write it to hidden <input> elements on your second form and retrieve it from the $_GET or $_POST array
In 2nd input form you take first inout from value and keep those one as hidden.
and 2nd input form you should
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<input type="hidden" name="fname" value="<?php echo $_POST['fname'];?> " />
<input type="hidden" name="username" value="<?php echo $_POST['username'];?> " />
<input type="hidden" name="passowrd" value="<?php echo $_POST['password'];?> " />
<input type="hidden" name="conpassowrd" value="<?php echo $_POST['conpassword'];?> " />
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
I hope it will work for you.
In the second page, you can get the post data from page 1 and set it in a hidden field like this, say the fname:
<input type="hidden" name="fname" id="fname" value="<?php echo $_POST['fname'] ?>">
When you submit from the second page, you can catch this data in the destination page, say
$_POST['fname']

1st form within table not submitting

I have a while loop generated table that looks like the following:
<table>
<tr>
<th>Date</th>
<th>Pushups</th>
<th>Delete</th>
</tr>
<tr>
<td>11-01-2012</td>
<td>28</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete1">
<input type="hidden" value="253" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete1" />
</form>
</td>
</tr>
<tr>
<td>11-02-2012</td>
<td>33</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete2">
<input type="hidden" value="246" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete2" />
</form>
</td>
</tr>
<tr>
<td>11-03-2012</td>
<td>43</td>
<td>
<form action="../workout_tracker/delete_pushups.php" method="get" id="Delete3">
<input type="hidden" value="39" name="pushups_id" />
<input type="hidden" value="/workout_tracker/pushups-tracker/" name="current_page" />
<input type="submit" value="Delete" form="Delete3" />
</form>
</td>
</tr>
</table>
The problem is that the first "Delete" form in the first row of the table isn't submitting. When I click, it doesn't do anything.
There is a form higher up on the page, but it closes and has validated. I get the same response in Chrome, Firefox, and IE.
UPDATE:
In a different form higher in the markup (not shown), the form tag had not been closed. I had </for>...
Do any of the other delete forms work or is it just the first one?
Open up your browser console/debugger and look for any errors on page load or when you click the button.
The only thing that looks as a possible problem is the form attribute of the submit input element. The link below shows that it's new in HTML5 and the only browser that doesn't support it yet is IE. It could be that you need to update to the latest version of your browser.
http://www.w3schools.com/tags/att_input_form.asp

Categories