Multiple submit on one form - php

I have two buttons for a register form.
<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" />
Check to see if either button is pushed
if((isset($_POST['submit1'])) or (isset($_POST['submit2'])))
Then PHP code to sanitize and validate data for either input
Now I want to have the "Pay Now" go to one page, and the "Pay Later" go to a different page, but I can n not figure it out. Thanks

You can use $_SESSION. After set session, redirect page and get session data
if(isset($_POST['submit1']) || isset($_POST['submit2'])) {
$_SESSION['post'] = $_POST;
if($_POST['submit1'])
header("Location: pay_now.php");
elseif($_POST['submit2'])
header("Location: pay_later.php");
}
pay_now.php or pay_later.php
$data = $_SESSION['post'];

Use form action like this:
<form action="" method="post">
<input type="submit" name="submit1" value="Pay Now" class="submit" id="submit1" onclick="this.form.action='page1.php'" />
<input type="submit" name="submit2" value="Pay Later" class="submit" id="submit2" onclick="this.form.action='page2.php'" />
</form>

Try this
if(isset($_POST['submit']))
{
if($_POST['submit'] == 'Pay Now')
{
echo $_POST['submit'];
}
if($_POST['submit'] == 'Pay Later')
{
echo $_POST['submit']
}
}

You can do it in PHP but you can also do it in JS using onlick event.
<form method="POST" action="" name="dynamicForm">
<input type="button" name="submit" value="Pay Now" class="submit" id="submit1" onclick="buttonClicked(1);" />
<input type="button" name="submit" value="Pay Later" class="submit" id="submit2" onclick="buttonClicked(2);" />
</form>
<script type="text/javascript">
function buttonClicked(type) {
if (type === 1) {
document.dynamicForm.action = 'firstUrl';
} else {
document.dynamicForm.action = 'secondUrl';
}
document.dynamicForm.submit();
}
</script>

Related

Wrong submit buttons get sent in POST with PHP script

My PHP form uses multiple submit buttons that I use for A/B processing
Occasionally, the wrong submit buttons get passed on even though they are not the ones clicked. The buttons look like this:
<form method="post" action="url..." autocomplete="off">
... fields...
<input type="submit" name="UpdateExit" value="Exit" />
<input type="submit" name="UpdateSave" value="Save" />
<input type="submit" name="InsertNew" value="Insert new" />
<input type="submit" name="Delete" value="Delete" />
<input type="button" name="Cancel" value="Cancel" />
</form>
Sometimes the var_dump($_POST) shows both UpdateExit and UpdateSave even though i clicked InsertNew! Needless to say it messes up the output. Then "the right button" is sent.
I narrowed down this behavior to webkit browsers and the first time I use the form after clearing my browser's cache, then it seems to "come back to normal". This is empirical and I cannot say it's PHP or HTML related. but I have been struggling with this for the better part of the day and found no spot on info on SO or elsewhere :(
Anyone heard/encountered a similar behavior?
Try:
<form method="post" action="url..." autocomplete="off">
... fields...
<input type="submit" name="UpdateExit" value="Exit" />
<input type="submit" name="UpdateSave" value="Save" />
<input type="submit" name="InsertNew" value="Insert new" />
<input type="submit" name="Delete" value="Delete" />
<input type="submit" name="Cancel" value="Cancel" />
</form>
Handle Cancel logic as you would any of the other submit buttons.
PHP Form Handler Logic:
<?php
if (isset($_POST['UpdateExit'])) {
//Do something based on Update and Exit was used
}
elseif (isset($_POST['UpdateSave'])) {
//Do something based on Update and Save was used
}
elseif (isset($_POST['InsertNew'])) {
//Do something based on Insert New was used
}
elseif (isset($_POST['Delete'])) {
//Do something based on Delete was used
}
else {
//Do something because Cancel was used...
}
?>

HTML form second button wont search my database

I have a database set up called customer. I also have a form that allows me to enter data into that database when submit, which is working fine.
However I also want to be able to search the database on the same page. I added a search field and button, and tried to use an if statement to determine which button was pressed.
The second button for searching the database doesn't seem to be doing anything when pressed, it doesn't seem to even enter the else if statement and I can't see why.
I have the following code:
<?php
require("header.php");
connect('final');//connect to DB
header ("location:/xampp/newCustomer.php");
if (isset($_POST['add'])) {
//do stuff for submit button here which works fine
}
else if (isset($_POST['btnSearch'])){
echo 'searching'; // test if button is working
$query = $_POST['searching'];
$data = mysql_query("SELECT *FROM customer WHERE 'First_Name' LIKE '$query'") ;
if($data === FALSE) {
$error = 'Query error:'.mysql_error();
echo $error;
}
else
{
$test = array();
$colNames = array();
while($results = mysql_fetch_assoc($data))
{
$test[] = $results;
}
$anymatches=mysql_num_rows($data);
if ($anymatches != 0)
{
$colNames = array_keys(reset($test));
}
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
}
}
?>
With my form setup like this:
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
you use type="button", that gives it a button structure but a button dont sumbit a form only a submit does that.
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
Try this:
HTML:
<form action="NewCustomer.php" method="post">
<table>
<tr>
<td> Name: </td>
<td><input type="text" name="name"></td>
<td><input type="submit" name="add" value="add"></td>
</tr>
</form>
<form action="NewCustomer.php" method="post">
<tr>
<td>Search: </td>
<td><input type="text" name="search"></td>
<td><input type="submit" name="search" value="search"></td>
</tr>
</table>
</form>
And PHP:
<?php
if (isset($_POST['add'])) {
echo "add";
}
if (isset($_POST['search'])){
echo "search";
}
?>
I think this is what you need :)

How to deal with two submit buttons in a form?

I don't understand this,
if I do this and I click the check out button, the page won't go to the check out page,
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
It only does if I change the name="checkout" to name="cart-checkout",
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="cart-checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
It works that way but does not make any sense to me, do you know why it does it that way?
So I tried to use <a> tag inside the <button> tag and it goes to the check out page,
<form action="cart.php" method="post" id="form-cart">
<button name="update" id="update" type="submit" value="Update cart">Update cart</button>
<button name="checkout" id="checkout" type="submit" value="Check out">Check out</button>
</form>
But does it a valid html to put <a> tag inside the <button> tag?
Why not change the buttons to:
<input name="update" id="update" type="submit" value="Update cart">
<input name="cart-checkout" id="checkout" type="submit" value="Check out">
Then in PHP (on cart.php) you can do:
<?php
if($_POST){
if(isset($_POST['update']){
// process update
} else if(isset($_POST['cart-checkout']){
// process cart checkout
// or uncomment the below line to forward to checkout.php
// header("Location: checkout.php");
}
}
?>
What about making one <button> that simply acts like a <a href=""> ?
-edit-
Whoops, sorry, misread. <button onclick="window.location='/nextpage';"> ?
I would actually use JavaScript here with an onclick event, which will take you to the checkout page.

Multiple submit buttons

I have a registration form with two submit buttons. One submit button is for a free member account, and the other is for a premium member account.
My form code is
<form action="post.php" method="post">
<input type="text" name="name" />
<input type="text" name="mail" />
//submit buttons
<input type="submit" value="signup for free member" />
<input type="submit" value="signup for premium member" />
</form>
if($_POST['name'] and $_POST['mail']){
$user_name = $_POST['name'];
$mail = $_POST['mail']
//How i can know he is preimum or free ?
}
Now how can I tell whether the user clicked on the free button or the premium button?
example:
if($_POST['free_member']){
$member = 'free';
}else{
$member = 'premium';
}
HTML
<input type="submit" value="signup for free member" name="signup_free"/>
<input type="submit" value="signup for premium member" name="signup_premium"/>
PHP
if($_POST['signup_free'])
{
$member = 'free';
}
elseif($_POST['signup_premium'])
{
$member = 'premium';
}
You could easily give the submit buttons a name:
<input name="submit" type="submit" value="signup for free member" />
<input name="submit" type="submit" value="signup for premium member" />
Then your post will show like this:
var_dump($_POST['submit']); // 'signup for premium member'
This isn't very scalable. I'd recommend using javascript to populate a hidden field on submit. Example:
HTML:
<!-- Include JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<input type="hidden" id="program_type" name="program_type" value="" />
<input class="submit" type="submit" value="signup for free member" data-program-type="free" />
<input class="submit" type="submit" value="signup for premium member" data-program-type="premium" />
JQuery:
$('.submit').click(function(e){
e.preventDefault(); // Stall form submit
$('#program_type').val($(this).data('program-type'));
$(this).parents('form:first').submit(); // Submit form
});
PHP:
var_dump($_POST['program_type']); // 'free' or 'premium'

How do i select buttons using php and enter into database?

Here is my current code..
<form action="js.php" method="post">
<input type="button" id='approve' value="Yes" onclick="a()" class="approve" />
<input type="button" id='reject' value="No" onclick="r()" class="reject"/>
<input type="submit" name="submitted" value="TRUE" />
</form>
This is my form and the page title is js.php..
I want that if the user clicks on any of the button the name of the button should bge entered into the database into a table called response under the heading response.
How Do i do that?Thanks..
I have done some PHP part...
<?php
require_once('connect.php');
if(isset($_POST['submitted']))
{
//if on button click
mysqli_select_db($connect,"check");
//entering query
}
?>
here is what Im using to enter the data
<?php
require_once('connect.php');
//if on button click
mysqli_select_db($connect,"pubd");
//entering query
if ($_POST['action'] == 'approve') {
// user clicked Yes
$yes = "insert into response (button) values ('approve')";
$yesr = mysqli_query($connect,$yes) or die(mysqli_error($connect));
} else if ($_POST['action'] == 'reject') {
$no = "insert into response (button) values ('reject')";
$nor = mysqli_query($connect,$no) or die(mysqli_error($connect));
}
?>
<form action="js.php" method="post">
<input type="button" id='approve' name="action" value="Approve" onclick="a()" class="approve" />
<input type="button" id='reject' value="Reject" name="action" onclick="r()" class="reject"/>
</form>
If you're just looking for an answer how to check which button was clicked, here it is:
HTML:
<input type="submit" id='approve' value="Yes" name="action" class="approve" />
<input type="submit" id='reject' value="No" name="action" class="reject"/>
PHP:
if ($_POST['action'] == 'Yes') {
// user clicked Yes
} else if ($_POST['action'] == 'No') {
// user clicked No
}

Categories