eh.. didnt read above post carefully enough.
blushes
nothing to add to above post.
ducks and hide
have a nice daycycle, citizen.
jakob
[RESOLVED] multiple submit buttons
Some times it's a good idea to ask "Why use two or more scripts?" before attempting to solve it on the HTML-level.
When I first saw the question I thought : "Make two or more buttons, make them call their respective JavaScript-funtions which in turn sets a hidden value and the have the form submitted to ONE PHP-script."
Then, in the PHP-script, branch out on the value from the hidden field. But that has already been suggested in manners close to what I suggested (somewhat like Steps)
Is the reason for using different scripts, because you really CAN'T join the different codebits into one page or is it because of newbie difficulties?
Let us all hear what you found and how you solved the problem in the end, ok?
"Make two or more buttons, make them call their respective JavaScript-funtions which in turn sets a hidden value and the have the form submitted to ONE PHP-script"
that would be extra work for no advantage, since the name of the buttons would be equivalent to a hidden value.
Personally, I think that it is best to avoid depending on clientside scripting here.
Generally, as long as there is a method to determine which script block to use, one script can branch into either block.
I want to double what laserlight said, we shouldn't write our PHP code in way that depends on a client side script. What I usually do to have multiple submit buttons is that I name them like this: submit[insert], submit[delete], so, when I wana read the code, I get the real name in this way:
list($name) = each($_POST["submit"]);
And using multiple submit buttons isn't something for newbies I think! Suppose we have a form that shows several items of our db, so, for each item we need to provide a button for edit/delete, so, we can use checkboxes/radio buttons and then use 2 submit buttons, but in the PHP script we should know which button was pressed. Just my 2 cents :rolleyes:
Here's something you can try. Here's the submission form...
<FORM METHOD=POST ACTION='/handler.php'>
<input type=text name="input">
<INPUT TYPE=submit name=action[form1] value="Cancel">
<INPUT TYPE=submit name=action[form2] value="Save">
<!-- form stuff goes here -->
</FORM>
And then, handler.php:
<?
// what script do we want?
$keys=array_keys($action);
$action=$action[0];
// this is for security...
$match="/[a-zA-Z0-9]+/";
if (preg_replace($match, '', $action))
die ('Invalid action attempt!');
// now include the right script.
if (!file_exists("./$action.php"))
die ('Script requested was not found");
else include "./$action.php";
?>
Simple - now you can have any number of form submit buttons, and have a consistent way to handle them...
I was a little surprised to notice that some people were suggesting using Javascript.
Looking at the Javascript code posted so far, it doesn't look as though it's doing anything more than a code such as (off the top of my head) ...
if ($submit1)
$executestring = "Location: submit1.php";
if ($submit2)
$executestring = "Location: submit2.php";
header($executestring);
Using multiple buttons in this way is far easier for newbies (such as myself) to do than some people think. Just create a form that reloads itself (with $PHP_SELF). Rename each submit button to whatever name you like and include 'if' statements at the top of your script to read and act upon the input from the user.
It's that easy and the method I always use. However, watch out for some interesting bugs in PHP. I came across one a couple of weeks ago where a button check like the one above always entered the code in the first 'if' no matter what button was pressed and then executed the code for the right button afterwards. This was fixed with a workaround but it was a pain in the butt.
I had to do this in one of my first php pages, as i had a news editing page with buttons for both delete and edit.
I just used:
<input type="submit" name="type" value="Delete News">
<input type="submit" name="type" value="Edit News">
and had the page i was posting to check the value of $_POST['type']. This worked fine, and at the time was the only way i could think of getting it working properly.
I assumed that if an input element was in a form, it would be sent regardless of whether you clicked it or another button, ie. having 2 submit buttons, submit1 and submit2, clicking submit1 would still post submit2 and therefore have big problems on the if ($submit1). I don't think thats the case anymore, but i like to stay on the safe side
<edit> Can't get it right when i try, no wonder my scripts never work first go </edit>
I know minimum one thing, that requires JS - automatic form renew.
There are several SELECT's, and content of next of them depends on OPTION, selected in the previous.
I can't invent nothing better than set "onChange=submit();" in each SELECT.
Although, IMHO use of client side scripting is not a good idea.
When I want to call a page back (as long as I am not using large form data) I use regular HTML links with "get" data:
<a href="productDisplay.php?productID=1">product 1</a>
<a href="productDisplay.php?productID=2">product 2</a>
<a href="productDisplay.php?productID=3">product 3</a>
The URLs can also contain other variables, obviously.
Hope this Helps:
Chris Adams
@: there's a problem with that solution (and all other posted solutions that check for the value of the submit-element): multi-language pages.
There you cannot rely on the value of a button. Furthermore, it s not really a good idea to check for an element that serves only the purpose of giving information to a user. It has nothing to do with the logic of your application...
Give each button a different name and then check if the name is set in the POST array:
<?php
if (isset($_POST['btn1']))
echo 'pressed button 1';
else
echo 'pressed button 2';
?>
...
<input type="submit" name="btn1" value="Just some text">
<input type="submit" name="btn2" value="Just some text">
There is an answer.
<form>
<input type="submit" name="submit" value="NEW" />
<input type="submit" name="submit" value="SAVE" />
<input type="submit" name="submit" value="EDIT" />
</form>
<?php
switch ($_GET['submit']) {
case 'NEW':
print 'This is what happens when new button is clicked.';
break;
case 'SAVE':
print 'This is what happens when save button is clicked.';
break;
case 'EDIT':
print 'This is what happens when edit button is clicked.';
break;
default:
print 'You can put the html form here.';
break;
} // End: switch ($_GET['submit'])
?>
Yes, you can have more than one, no you don't need js to do anything. Its very simple...when the user presses the submit button, it has a value, like all other form elements. Just check for the value of the button...
Wee have multiple submits and use the first letter of the value in our control code to make the page 'do stuff'
Originally posted by mcrbids
Here's something you can try. Here's the submission form...
<FORM METHOD=POST ACTION='/handler.php'>
<input type=text name="input">
<INPUT TYPE=submit name=action[form1] value="Cancel">
<INPUT TYPE=submit name=action[form2] value="Save">
<!-- form stuff goes here -->
</FORM>
And then, handler.php:
<?
// what script do we want?
$keys=array_keys($action);
$action=$action[0];
// this is for security...
$match="/[a-zA-Z0-9]+/";
if (preg_replace($match, '', $action))
die ('Invalid action attempt!');
// now include the right script.
if (!file_exists("./$action.php"))
die ('Script requested was not found");
else include "./$action.php";
?>
Simple - now you can have any number of form submit buttons, and have a consistent way to handle them...
I'm a little confused, where exactly do you put/type in the specific pages (cancel.php, save.php)? Thanks
I'll explain the code below.
When you start a form with just <form> the method="GET" is assumed. And action="$PHP_SELF" is also assumed.
This means the form submits to itself and uses $_GET
i.e. (We are using a file called myform.php)
<form>
is the same as
<form method="GET" action="myform.php">
When the submit button labled NEW is clicked.
<form>
<input type="submit" name="submit" value="NEW" />
<input type="submit" name="submit" value="SAVE" />
<input type="submit" name="submit" value="EDIT" />
</form>
http://www.example.com/myform.php
becomes,
http://www.example.com/myform.php?submit=NEW
To read values passed via the url (GET Method) PHP uses the $GET Array. Notice above our submit buttons have a name="submit" thats the name of the index you need to check in $GET.
i.e. In the above example $_GET['submit'] is equal to "NEW"
<?php
switch ($_GET['submit']) {
case 'NEW':
print 'This is what happens when new button is clicked.';
break;
case 'SAVE':
print 'This is what happens when save button is clicked.';
break;
case 'EDIT':
print 'This is what happens when edit button is clicked.';
break;
default:
print 'You can put the html form here.';
break;
} // End: switch ($_GET['submit'])
?>
The switch statement above is checking what $_GET['submit'] is equal to and then doing something.
Here is an example you can copy and paste to test. Make a file called myform.php. Paste the following code into it.
<?php
switch ($_GET['submit']) {
case 'NEW':
print 'The New button was clicked.';
break;
case 'SAVE':
print 'The Save button was clicked.';
break;
case 'EDIT':
print 'The Edit button was clicked.';
break;
default:
print '
<form>
<input type="submit" name="submit" value="NEW" />
<input type="submit" name="submit" value="SAVE" />
<input type="submit" name="submit" value="EDIT" />
</form>';
break;
} // End: switch ($_GET['submit'])
?>
do the majority of people not have javascript enabled? I get the impression that it should eb avoided at all costs! I quite like javascript
some do, some don't...you can't count on it and should never use it to do heavy validation for user input.
What you should do, if you prefer to use it, it to test to see if it works and then alert the user that your site requires js to be turned on...if they have it turned off
This has nothing to do w/ java. This is a PHP action based on input from a user. NEW, SAVE, EDIT.
On a side note I can copy your form locally and remove all your java validation. Then I can submit malformed data all day to your site.
Java is a nice touch but you had better code your PHP to handle input as if you were not using Java at all.
Do a search on google for PHP XSS. (Cross Site Scripting)
i see, but if im using javascript to say run a slideshow its not a secuity risk, but its just a pain if someone doesn't have it turned on.
How do you go about testing for javascript? would having javascript document.write a meta refresh tage do the job?
<form action="noJavaScript.php" name="theForm" method="POST">
ID : <input type="text" name="emp_id" size="40" length="40" value="<?=$_GET["emp_id"];?>"><br>
First Name to update : <input type="text" name="name" size="40" length="40" value="<?=$_GET["name"];?>"><br>
Branch to update : <input type="text" name="branch" size="40" length="40" value="<?=$_GET["branch"];?>"><br>
city to update : <input type="text" name="city" size="40" length="40" value="<?=$_GET["city"];?>"><br>
<input type="submit" name="submit" value="Update" onclick="document.theForm.action='update2form.php'">
<input type="reset" name="reset" value="Clear">
<input type="submit" name="submit" value="insert" onclick="document.theForm.action='insert2form.php'">
<input type="submit" name="submit" value="delete" onclick="document.theForm.action='delete2form.php'">
</form>
you guys are all crazy, this is something that should be written in assembly!
all jokes aside - submitted information is something you want handled server side always, never on the client side. However I don't see there being a problem using javascript for the icing on the cake - so to speak.
If you want to save yourself processing time from bad form submission, have the server side scripting in place and use the javascipt as a secondary measure to prevent users from submitting bad form data. At least you can prevent a fraction of the server load / querying or whatever you have going on behind your forms.