How to deal with two submit buttons in a form? - php

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.

Related

Only one button working despite formaction

I have a file.php that is echoing the contents of a html file. The solution to my problem seems to be to use formaction according to stackoverflow but for some reason I can't get it to work. 'Createuser' works but not 'shop'. I have tried with not having formaction on the 'shop' button as well since I already have action = shop but it doesn't work. What am I doing wrong?
<form method="post" action="shop.php">
<table>
<p>
<input type="submit" value="Sign in" name="sign_in" form method="post" formaction="shop.php"/>
<input type="submit" value="Create user" name="create_user" formaction="createuser.php" />
</table>
</p>
</form>
The first button shouldn't need formaction as it is defined in the form properties.
Could you try changing your inputs to buttons? something like.
<button type="submit" formaction="createuser.php">Create User</button>
It is important to use type="submit" here or formaction will be ignored.
Try removing the form action, form and method attributes from the sign in submit input. I know you said you removed formaction, but I think the below should work.
<input type="submit" value="Sign in" name="sign_in" />
I don't know if this will solve your problem but where you have your closing table tag isn't where it should be this:
<form method="post" action="shop.php">
<table>
<p>
<input type="button" value="Sign in" name="sign_in" form method="post" formaction="shop.php"/>
<input type="button" value="Create user" name="create_user" formaction="createuser.php" />
</p>
</table>
</form>

why cant i get value of $accept_request_id

<form method="post>
<button input type="submit" id="click" class="btn btn-info " name="<?php echo$accept_request_id; ?>"></button>
</form>
Why can't I get value of $accept_request_id? I use following code
if(isset($_POST[$accept_request_id])) {
$accept_request_id=$_POST['accept_request_id'];
}
You can use below code to get value of form. This will work
<form method="post>
<input type="hidden" name="accept_request_id" value="<?php echo $accept_request_id ?>" />
<button input type="submit" id="click" class="btn btn-info">Submit
</button>
</form>
if(isset($_POST['accept_request_id'])) {
$accept_request_id = $_POST['accept_request_id'];
}

woocommerce login form submit with button instead on input

I'm wanting to use a button element rather than input on the woocommerce login form, purely for styling reasons. So rather than
<input type="submit" class="btn btn-outline-primary" name="login" value="Login" />
I want to use
<button type="submit" class="btn btn-outline-primary">Login</button>
This doesn't work though and I've no idea why.
Just found it out the button element need the value setting the same as the input i.e.
<button type="submit" class="btn btn-outline-primary" name="login" value="Login">Login</button>

I am not able to post form on same page

Here are the codes.
Form
<form method="POST" action="" id="search">
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="publish" id="publish" alt="Publish"> Publish</a></div>
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="edit" id="edit" alt="Edit"> Edit </a></div>
<div class="col-lg-4 center"><a class="btn btn-success" type="submit" name="draft" id="draft" alt="Save as Draft"> Save as Draft</a></div>
</form>
Post
<?php
if ($_POST['publish'] == 'publish'){
echo '<script>alert("Publish"); </script>';
}
if ($_POST['edit'] == 'publish'){
echo '<script>alert("edit"); </script>';
}
if ($_POST['draft'] == 'publish'){
echo 'draft';
}
?>
If I click on any button nothing happens. I am not getting anything nor any error. Nothing appears in firebug as well.
You wanted to submit to the same page. Try giving the page name in your action
<form method="POST" action="yourpage.php" id="search">
Try with this:
<?php
if (isset($_POST['publish'])) {
echo '<script>alert("Publish"); </script>';
}
if (isset($_POST['edit'])) {
echo '<script>alert("edit"); </script>';
}
if (isset($_POST['draft'])) {
echo 'draft';
}
?>
use button instead of links
like
<input type="submit" ..... />
like this
<input type="submit" class="btn btn-success" name="publish" id="publish" alt="Publish" value="Publish" />
You will get data then
and try to on error reporting in php.ini file
You need to add a submit n
Button. <input type=submit name="submit"> also, specify action attribute in form tag to your Php script.
<?php
if (isset($_POST['submit'])){
echo '<script>alert("Publish"); </script>';
}
?>
<form method="POST" action="" id="search">
<input name="submit" type="submit" />
</form>
First thing you can not use type="submit" as attribute in your anchor tag.
Second if you wants to submit button using input type as submit then use the following code:
<?php
if(isset($_POST))
{
if (isset($_POST['publish'])){
echo '<script>alert("Publish"); </script>';
}
if (isset($_POST['edit'])){
echo '<script>alert("edit"); </script>';
}
if (isset($_POST['draft'])){
echo '<script>alert("Draft"); </script>';
}
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="search">
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="publish" id="publish" value="Publish"> </input></div>
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="edit" id="edit" value="Edit"> </input></div>
<div class="col-lg-4 center"><input class="btn btn-success" type="submit" name="draft" id="draft" value="Draft"> </input></div>
</form>
Third, In case if you really wants to use only anchor for submit then you have to use help of javascript function submit() and use the
onclick="document.getElementById('search').submit();
in your anchor tag.

How do I tell the action on a form not to execute when cancel button clicked?

I have page "A" with a form:
<form id="formA" method='post' action="somePage.php" name="formA">
... some input here ...
<div id="save-button"><input type="submit" value="Save" onclick=""/></div>
<div id="cancel-button"><input type="submit" value="Cancel" onclick="self.close()"/></div>
</form>
When I click "Save" it should and does execute "somePage.php" which among other things writes to a file.
When I click "Cancel" I want page "A" to close (which it does) but it still executes "somePage.php".
How can I keep that from happening?
<div id="cancel-button"><input type="submit" value="Cancel" onclick="self.close()"/></div>
to
<div id="cancel-button"><input type="button" value="Cancel" onclick="self.close()"/></div>
type cannot be submit
<form id = 'formA' method = 'post' action = 'nextPage.php'>
...textboxes etc...
<div id="save-button"><input type="submit" value="Save" onclick=""/></div>
</form>
<form id = 'formB' method = 'post' action = 'prevPage.php'>
<div id="cancel-button"><input type="submit" value="Cancel" onclick=""/></div>
</form>
try it

Categories