I've multiple forms with a single action, a single php page that gets called by all the forms.
How can I differentiate which form was sent to the php page?
Using a different unique input type="hidden" for each form.
HTML:
<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">
PHP:
$myform = $_POST["form_id"];
You can also use the submit button but note that the "value" parameter is what gets displayed to the user so you won't be able to modify it (assuming you want the same text to be displayed on every button).
<input type="submit" name="action" value="the user saw this">
PHP:
$_POST["action"] // -> "the user saw this";
Add a hidden field (action or the like) to each field, then check for it.
<form id="num1">
<input type="hidden" name="action" value="first_action" />
</form>
...and the check:
<?php
if(!empty($_REQUEST['action']) {
switch($_REQUEST['action']) {
case 'first_action':
// first action code
break;
}
}
?>
Give each submitbutton an other name or put a with different values in each form.
You can detect this from the submit button itself too, if submit has different values like:
update name, update profile, delete users...
On the submit button for each form, use different names. Something like:
<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...
Then on your PHP, you'll have:
$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]
Related
Well, it's the first time I'm using stackoverflow, so I'd looking foward to be clear.
How can I set a value for a PHP variable from a button press?
For example, if the user pressed form button 1, $variable = 'false'; if it presses form button 2, $variable = 'true'... is that possible?
Thx, in advance
Yes, set different values to same name submit buttons
like:
<input name="submitBtn" type="submit" value="Choice1">
<input name="submitBtn" type="submit" value="Choice2">
Then depending on which button is pressed in php $_POST['submitBtn'] will have value Choice1 or Choice2
Yes, this is possible.
You can put 2 (or more) submit buttons with the same name and different values into a form and then chack the value of the submit button via php:
html:
<form method="POST" action="somefile.php">
<input type="submit" name="sb" value="button1" />
<input type="submit" name="sb" value="button2" />
</form>
php:
<?php
$submitbutton = $_POST['sb'];
?>
In the HTML side just create two submit button:
<input name="click" type="submit" value="clickOne">
<input name="click" type="submit" value="clickTwo">
Then on the PHP side (assuming you are submitting with POST method):
switch ($_POST['click']) {
case 'clickedOne':
// first pressed
break;
case 'clickedTwo':
// second pressed
break;
default:
// something wrong
break;
}
I have several sections of code that insert values into different tables in my DB.
I am wondering if there is a way to capture which button on my form has been selected so when the page reloads it only executes the one insert statement?
this is how I submit to the same page
<form name="input" action=myawesomeform.php" method="POST">
This is what my submit button looks like
<input type="submit" value="Submit" id="5050gdmyButton" />
so pretty much I want to execute one of the insert statements depending on which button was selected..
any help would be greatly appreciated.
1) You need to give name attribute to your submit button
2) when you have multiple inputs in your form and when you press one of them, then you only get that one in your php code with name attribute.
So for example:
<input type="submit" name="submit1" value="add" id="5050gdmyButton1" />
<input type="submit" name="submit2" value="update" id="5050gdmyButton2" />
in your php
if(isset($_POST['submit1']) && $_POST['submit1'] == 'add'){
// do insert for first one
}
if(isset($_POST['submit2']) && $_POST['submit2']== 'update'){
// do update for second one
}
you can give two different names for submit button like this..
<input type="submit" name="button1" value="Button1" id="5050gdmyButton" />
<input type="submit" name="button2" value="Button2" id="5051gdmyButton" />
In the Receiving page use the condition like this...
if ($_POST['button1'])
{
//do your first button process
}
elseif($_POST['button2'])
{
//do you second button process
}
If you are having more than one submit button than you should make a different name for each button
<input type="submit" value="addSubmitButton" id="5050gdmyButton1" />
//to add value in db
<input type="submit" value="removeSubmitButton" id="5050gdmyButton2" />
//to remove value in db
in your serverside
<?php
if(isset($_POST["addSubmitButton"])){
//add value in db
}
if(isset($_POST["removeSubmitButton"])){
//remove data from database
}
?>
just but a name tag inside input
<input type="submit" value="Submit" id="5050gdmyButton" name="5050gdmyButton" />
and in php check if name isset()
if(isset($_REQUEST['5050gdmyButton'])){ //do somthing }
I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.
Using a form with a blank action - action="".
I have 2 buttons on the form that do different things. one to submit/save the info, the other to open an output sheet:
<input type="submit" name="SubmitSave" id="SubmitSave" value="Submit / Save" onClick="this.form.action='PA_Monitorcall.php'; this.form.submit()" />
<input type="submit" name="EmailDetails" id="EmailDetails" value="Email" onClick="this.form.action='OutputSheetPA.php'; this.form.submit()" />
I need the output sheet to open in a new window, but can't have this in the form header details, it will need to go in the code for the button above. Any ideas?
Cheers!
onClick event of both submit buttons, call a javascript function, which would toggle the 'target' attribute of the form tag to '_blank' or '_parent'/''.
with this new value for 'target' attribute your post would be submitted in a new window/tab
<form target="" action="" method="post">
<input type="submit" value="Same Window" onClick="ChangeTarget('same')" />
<input type="submit" value="New Window" onClick="ChangeTarget('new')" />
</form>
function ChangeTarget(loc) {
if(loc=="new") {
document.getElementById('form_id').target="_blank";
} else {
document.getElementById('form_id').target="";
}
}
Use type="button" instead. Your onClick already calls submit, so you don't need them to be submit inputs.
You can name the two inputs with the same name and then check the value of that inputs.
<form action="">
<input type="submit" name="submit" id="SubmitSave" value="Submit / Save" />
<input type="submit" name="submit" id="EmailDetails" value="Email" /></form>
</form>
And in the php file:
<?php
if ($_POST['submit'] == 'Submit / Save')
// save the form input
elseif ($_POST['submit'] == 'Email')
// do other stuff
...
I have this:
<form method="post" id="kl" action="step2.php">
<input type="radio" name="rubrik" value="bussines"></input>
<input type="radio" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
What i bassicaly want is: When the second radio button is checked, to submit the form to step2a.php, a different file. How can i do this? Jquery, Javascript, php?
You could do this with JavaScript (bind a submit listener that checks the value of the radio button and then sets the action property of the form), but it would be simpler and more reliable to do something (server side) along the lines of:
<form ... action="step-selector.php">
and
<?php
if (isset($_POST['rubrik']) && $_POST['rubrik'] == 'bussines') {
include('step2.php');
} elseif (isset($_POST['rubrik']) && $_POST['rubrik'] == 'private') {
include('step2a.php');
} else {
include('error-state.php');
}
?>
you can do this by modifying the Form into:
<form method="post" id="kl" action="step2.php">
<input type="radio" class="radio" rel="step2.php" name="rubrik" value="bussines"></input>
<input type="radio" class="radio" rel="step2a.php" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>
I added rel attribute to radio buttons. each has a value of the url. I also added a class to get the element with jQuery.
Now, you will need some Javascript, i will use jQuery code:
$('.radio').click(function (){
rad = $(this);
radRel = rad.attr('rel');
$('form#kl').attr('action', radRel);
});
There are multiple ways of doing it, depending on what you want exactly.
Check this one out, it might help you get there; Radio Button to open pages
You can use form.submit() as onclick-handler (not onchange) and change the action, too.
<input type="radio" name"rubrik" value="private" onclick="this.parentNode.action='yourOtherFile.php'; this.parentNode.submit()"></input>