Wrong submit buttons get sent in POST with PHP script - php

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...
}
?>

Related

how can I get the value of a input field using PHP if there are two forms on the page both of POST method

I want to get the value of an <input> field using PHP.
There are two forms on my page both of POST method.
<form method="POST">
<input type="text" name="first">
<input type="submit" name="submit" value="submit">
</form>
<form method="POST">
<input type="text" name="second">
<input type="submit" name="submit1" value="Post">
</form>
How do I get the value of the second input field? Even though if I use $_POST['second'] it shows me an error:
Undefined index: 'second'
The W3C specs define that an input can be associated to only one form. It is a sign of bad design when you need to have multiple forms and the backend has to know which data is present in other forms.
The <form> element can contain arbitrary element structures like tables, it can even contain the entire document body content. You should hardly need multiple forms.
A common use-case is to have multiple submit buttons having the same name instead. Only the pressed button will be part of the form data.
<form method="post">
<input type="text" name="text_input">
<button type="submit" name="action" value="add">submit</button>
<button type="submit" name="action" value="update">submit</button>
<button type="submit" name="action" value="delete">submit</button>
</form>
Again, do not do that, however, if you really want to share a field across multiple forms for some reason, this can only be done by javascript intercepting the form submit event. This will not work when scripts are disabled by the user.
document.querySelectorAll('form').forEach(e => {
e.addEventListener('submit', function() {
document.querySelectorAll('.multi-form-input').forEach(e => e.setAttribute('form', this.id));
})
})
<input class="multi-form-input" name="common_input" type="text">
<form id="form-1" method="post">
<button type="submit" name="action" value="1">submit</button>
</form>
<form id="form-2" method="post">
<button type="submit" name="action" value="2">submit</button>
</form>

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>

html - how to give a submit button a variable for GET?

i have the following button:
<form method="GET" action="/cms/deleteBlog/">
<input class="btn btn-danger btn-small" type="submit" name="'.$blogID.'" value="Delete">
</form>
So right now i get the following url:
cms/deleteBlog/?1=Delete
obviously i want something like the following:
cms/deleteBlog/id=1
So i tried the following (which is obviously not working):
name="id='.$blogID.'"
So how do i solve this? i've been looking around on the internet, and i know the solution is quite easy. But i just can't remember the way how it is done!
Add a hidden form field.
<input type="hidden" name="id" value="1" />
Also, you should never use GET to delete or modify data. Read Why should you delete using an HTTP POST or DELETE, rather than GET? for insight.
Why not use a hidden input field, e.g:
<form method="GET" action="/cms/deleteBlog/">
<input type="hidden" name="id" value="'.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>
Or, if you want pretty URLs:
<form method="GET" action="/cms/deleteBlog/id='.$blogID.'">
<input class="btn btn-danger btn-small" type="submit" value="Delete">
</form>

Multiple submit on one form

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>

multiple submit button

i've a form POST with multiple submit buttons. i understand to get this to work i must have them with different name.
however, i wanna keep the name to be the same because i wanna handle the POST using a single script.
im not sure if there is other way but i know javascript can be used. however, how do i get the value of the hidden value associated to the button since now they have only a single ??
my example is as follows:
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
</form>
Your hidden values are not associated with the buttons at all. Furthermore, you cannot use the same value for the ID attribute on multiple elements.
What I usually do in this situation is check the POST vars. Name them something like remove_1, remove_2, etc. Then you can search through your POST vars, find all of them beginning with remove_ (or whatever format you choose... don't use it for other things) and then you can parse out the ID of what you are trying to remove.
You could always just use 3 different forms, all with the same action. No JavaScript needed.
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
It's possible using two different methods:
If you absolutely have to show 3 different buttons, use a separate <form> wrapper for each one. Put each "removeid" element in a different form.
Otherwise, just have a single button, and when submitted, use JavaScript to set the value of a single hidden input element before posting the form. You can find sample code for this easily with a Google query for "javascript+form+post".
You can have one form with more than one submit button sharing the same name, your initial assumption was wrong.
The following code is perfectly valid, and the value of the clicked submit button will be passed along with its name:
<form action="TestZone.html" method="GET">
<input type="submit" name="MySubmit" value="First" /><input type="submit" name="MySubmit" value="Second" /><input type="submit" name="MySubmit" value="Third" />
</form>
You can't have multiple elements with same ID, but same name for form elements is common and valid.
Hi i have resolved my questions by following Brad solution. to get the POST var, i did this:
//Check if Remove btn is clicked
$isClickRemove = false;
$cid = "";
foreach($_POST as $k=>$v){
$pos = strpos($k,"btnremovecart_");
if($pos !== false){
$pos2 = strpos($k,"_"); //2nd pos to get cartID
$cid = substr($k,$pos2+1);
$isClickRemove = true;
break;
}
}
my html looks like this:
<input type="submit" id="btnremovecart_11" name="btnremovecart_11" value="Remove" />
hope this helps =)
You can't because there is no way of distingushing the different fields.

Categories