PHP Form multiple buttons - php

I have a form with 2 buttons, that depending on which is selected will either be deleted or edited from the database. Those are each individual pages using SQL statements (questionedit and questiondelete). However, when i press a button, nothing happens...Any Ideas
Here is my javascript
function SelectedButton(button)
{
if(button == 'edit')
{
document.testedit_questionform.action ="testedit_questionedit.php";
}else if(button == 'delete'){
document.testedit_questionform.action ="testedit_questiondelete.php";
}
document.forms[].testedit_questionform.submit();
}
Here is my form (being echoed from a loop)
<form name="testedit_questionform" action="SelectedButton" method="POST">
<span class="grid_11 prefix_1" id="" >
Question:<input type="text" name="QuestionText" style="width:588px; margin-left:10px;" value="$row[0]"/>
<input type="button" value="Edit" name="Operation" onclick="submitForm('edit')" />
<input type="button" value="Delete" name="Operation" onclick="submitForm('delete')" />
<input type="hidden" name="QId" value="$row[3]" /><br />
</form>

First of all, your function should be named submitForm
function submitForm(button) {
if(button == 'edit') {
document.testedit_questionform.action ="testedit_questionedit.php";
} else if(button == 'delete') {
document.testedit_questionform.action ="testedit_questiondelete.php";
}
document.testedit_questionform.submit();
}
And then, call submit method from your form.
EDIT:
An alternative to calling forms is: document.forms['FORM_NAME'].submit()

Look at how you access the form to call submit(). Now look at how you access the form to change the action. One of them is clearly wrong.

It would be easier to make one PHP file, and buttons with different names like this:
<form method="post" action="actions.php">
<input type="submit" name="action1" value="Action 1" />
<input type="submit" name="action2" value="Action 2" />
[...]
And the file actions.php:
if(isset($_POST["action1"])) {
// action 1
}
elseif(isset($_POST["action2"])) {
// action 2
}

Related

How to know which button was clicked if a form has two buttons

I have two forms, one with login and another with logout and they both use the same controller/form processor,
I am using
$_SERVER['REQUEST_METHOD']
to see if the form is submitted.
But how can I know if login or logout was clicked.
$_POST['action'] == 'login'
and
$_POST['action'] == 'logout'
are not working.
okay here is the complete form :
<?php
$test = 'default';
if( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'login' ){
//do some stuff
$test = 'login';
}elseif( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'logout' ){
//do other stuff
$test = 'logout';
}
?><?php echo $test; ?>
<form method="post" action="">
<p>
<input type="text" name="username" placeholder="Username" value="">
</p>
<p>
<input type="password" name="password" value="" placeholder="Password">
</p>
<p>
<input type="submit" name="login" value="Log In">
</p>
</form>
<form method="post" action="">
<input type="submit" name="logout" value="Log Out">
</form>
But the $test doesn't change.
Give the buttons different names
If you have a input element of type submit, you will get a button that posts a value itself. Give it a name and a value, and that value will be posted along with the post data:
<input type="submit" name="button1" value="Click me">
<input type="submit" name="button2" value="Or me">
The value is often localized and I think you wouldn't want to check for that, but you can just check for the existence of Button1 or Button2 in the post variables to see which one was clicked, regardless what their value is.
Give the buttons (only) different values
Alternatively, if you know the button value is useful (it contains an id or name rather than a localized text from a template), you could give both buttons the same name (like 'action') and check the value of the post variable instead. In that case, the two buttons behave more or less like a group of radio buttons. Not my preference, but certainly possible and acceptable.
<input type="submit" name="action" value="Action 1">
<input type="submit" name="action" value="Action 1">
Or you could use a button tag in that case, which is a similar but slightly different element. It also has a name and value attribute, but you can specify the text as the content of the element, so you can decouple the value from the text you present to the user. This would be better than using the input version above.
<button type="submit" name="action" value="Action 1">Click me for action 1!</button>
<button type="submit" name="action" value="Action 2">Secondary action</button>
Note that type="submit" is the default for buttons, so you can omit it, as long as you don't need to support IE7.
Checking which one was clicked
Whichever solution you pick, don't forget to check thoroughly though. There are other ways of submitting a form, for instance through clicking enter, so make sure to propertly handle the case where neither button was clicked.
if (array_key_exists('button1', $_POST)) {
// Button 1 was clicked
} elseif (array_key_exists('button2', $_POST)) {
// Button 2 was clicked
} else {
// Neither was clicked.
}
or for the alternative
if (array_key_exists('action', $_POST)) {
switch ($_POST['action') {
case 'Action 1':
// Button 1 was clicked
break;
case 'Action 2':
// Button 2 was clicked
break;
default:
// An unknown button was clicked!
break;
}
} else {
// Neither was clicked.
}
Assign names to the buttons:
<input type="submit" name="Add" value="Add" />
<input type="submit" name="Delete" value="Delete" />
and verify which one is set as:
if (isset($_POST['Add'])) {
// add
} elseif (isset($_POST['Delete'])) {
// delete
}
You can identify through isset function:
if(isset($_POST['action']) && $_POST['action']=="login"){
//Login Button Logic
} else if(isset($_POST['action']) && $_POST['action']=="logout"){
//Logout Button Logic
}

How to set two buttons on one form html&php

This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}

2 submit buttons on form - need one to open a new window

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

Disable Button HTML

I have a submit form for a URL and I want it to have specific behavior which I am not able to achieve so far. Initially I want the button to be enabled. After someone enters a URL and hits the "submit" button, I want to call my checkURL() function. If the function returns true, I want the button to become disabled and I want to then open remote_file.php. If it returns false, I want the button to be enabled and make them try another URL.
<form name=URLSubmitForm
action="remote_file.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288000">
<input type="text" name="name" size="50">
<input type="submit"
onchange="this.disabled=false"
onclick="this.disabled=true; checkURL();"
value="submit">
</form>
Edit: It looks like I was just putting the onchange in the wrong place. I ended up doing this to fix reenabling the button
<input type="text" onchange="submit.disabled=false" name="name" size="50">
Thanks!
I would propose that you attach the event handling code to the form's onsubmit event, not the button event(s). What you're trying to control is whether or not the form is posted. The button being disabled while your validation logic runs is a secondary goal.
Try this instead:
<script type="text/javascript">
function checkURL(){
var submitButton = document.getElementById('submitButton');
submitButton.disabled=true;
/* implement your validation logic here
if( url is invalid ){
submitButton.disabled=false;
return false;
}
*/
// everything is valid, allow form to submit
return true;
}
</script>
<form name="URLSubmitForm" action="remote_file.php" onsubmit="return checkURL();" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288000">
<input type="text" name="name" size="50">
<input type="submit" name="submitButton" id="submitButton" value="submit">
</form>
<input type="submit"
onclick="if (checkURL()) { this.disabled='disabled'; return true; } else { return false; }"
value="submit">
How about in the form's onsubmit event:
<form onsubmit="(function(){
if(checkURL()){
this.elements['submit'].disabled = 'disabled';
}
else{
return false;
}
})()">
Since you haven't given any ajax code, the form will still be submitted normally and when the page is reloaded the button will be enabled again.
onclick="checkURL(this);"
function checkURL(arg){
this.disabled=true;
if(<something>) this.disabled=false;
}

$_POST variable

I'm encountering a problem. I'm using Wordpress, but this ain't a Wordpress question.
I'm using two forms, on one form I have all the input fields and one hidden input field which I use for checking which form the user has submitted. I have saved its value as 'save'. There is another form which is just for resetting all the options and its value is 'reset'. In PHP, I check the value of the hidden field and take respective actions. But the problem is that the reset thingy isn't working.
Here is my HTML for the forms:
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
In PHP, I verify them like this:
// if I change the 'save' literal to something else like 'savea', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
if ('save' == $_POST['action']) {
foreach ($this->cp_options as $option) {
if (isset($_POST[$option['id']])) {
update_option($option['id'], $_POST[$option['id']]);
}
else {
delete_option($option['id']);
}
}
header("Location: themes.php?page=functions.php&saved=true");
die;
}
// if I change the 'reset' literal to something else like 'reseta', $_POST variable will not be empty
// but if I dont, then $_POST variable is NULL
elseif ('reset' == $_POST['action']) {
foreach($this->cp_options as $option) {
delete_option($option);
}
header("Location: themes.php?page=functions.php&reset=true");
die;
}
The problem is if I change the 'reset' or 'save' literal to anything else like 'reseta' or 'saveasdfasd', $_POST variable won't be empty, but if I dont, then $_POST variable is NULL.
Any ideas on why this is happening?
[Old Answer Redacted]
EDIT
Try to isolate your testing environment first. This gave me results I expected.
<?php
if ( isset( $_POST['action'] ) )
{
switch( $_POST['action'] )
{
case 'save':
echo 'Save Action Requested';
break;
case 'reset':
echo 'Reset Action Requested';
break;
default:
echo 'Unknown action requested:';
var_dump( $_POST['action'] );
}
} else {
echo 'No action parameter received';
}
?>
<fieldset>
<form method="post">
<!-- Some input fields here-->
<p class="submit">
<input name="save" type="submit" value="Save changes" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</fieldset>
<fieldset>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
</fieldset>
I know you said that $_POST is null but are you assuming that or did you actually check $_POST == null? Have you tried doing a var_dump($_POST) to print out exactly what is getting sent across? Just comment out your re-direction and see what is in $_POST. Maybe that will give you a better clue as to what is happening.
Simple... remove the hidden inputs and change both of your "submit" buttons to have the same name, but different values:
<input type="submit" name="action" value="Reset" />
<input type="submit" name="action" value="Save" />
Then you can test it like this:
if ($_POST['action'] === 'Reset') {
// Do a reset here
} else {
// Do a save here
}
And you probably want to wrap the whole thing in:
if (isset($_POST['action'])) {
// Put your form handling here
}
If you have multiple forms on one page I'd recommend you send each form to a different URL. This is by far the simplest and most reliable way to detect where the form is going, just have two different scripts to deal with processing that form. You can then include or redirect to the final page you want the user to see.
This might because there are duplicate elements with the same name.
Can you try putting an id or name to your form?

Categories