Solved

php form with two buttons

Posted on 2009-03-29
6
307 Views
Last Modified: 2013-12-12
Hi,

I am trying to make a form that has two submit buttons that will send the form input one of  two different location depending on which submit button the user hits. My code (that doesn't work) is below.


<?php
 
if (isset($_POST['submit']))
{   
   $submit_value="products2.php";
}
else if (isset($_POST['creditcard']))
{
   $submit_value="products2.php";
}
?> 
        
<form  action="<?php $submit_value ?>" method="post" class="smalltxt1">
              <input type="hidden" name="cmd" value="_s-xclick" />
              <input type="hidden" name="hosted_button_id" value="2060293" />
              <table>
                <tr>
                  <td>
                    Gift Card Message:</td>
                </tr>
                <tr>
                  <td><input type="text" name="on0" maxlength="200" value="" /></td>
                </tr>
                <tr>
                  <td>Quantity:&nbsp;
                      <input type="text" name="quantity" value="" size="3" /></td>
                </tr>
              </table>
              <input type="image" src="images/button1.gif" border="0" name="submit" alt="" />
              <br />
              <input type="image" src="images/addtocart.gif" border="0" name="creditcard" alt="" />
            </form>

Open in new window

0
Comment
Question by:jackie777
  • 3
  • 2
6 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24013217
What is not working? What I usually do is I set my form action to one page and do the split (one script or another) on that page.
0
 
LVL 28

Expert Comment

by:gamebits
ID: 24013255
I think there is a flaw in your logic (code logic)

1- the first time you open the page there is nothing in the form action, so the page will submit to herself.

2- when you click on one of the buttons to submit the form your code check which one has been clicked and set the form action appropriately but at this point it is to late the form has already been submited.
0
 

Expert Comment

by:toodfa
ID: 24013605
Aslo, there is one litle mistake in that code.
You actually dont print $submit_value variable, and because of that the action parameter is always empty.

Use something like this and try to initialize varaibles before using/printing.

<form  action="<?php echo $submit_value; ?>" method="post" class="smalltxt1">

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jackie777
ID: 24013611
Thanks gamebits.

Could you please show me an example of the code you are talking about.
0
 
LVL 28

Accepted Solution

by:
gamebits earned 250 total points
ID: 24013656
Roughly it look like this

<form action="process.php" method="POST">
   Your form input fields here as usual
  <input type="image" src="images/button1.gif" border="0" name="submit" alt="" />
              <br />
              <input type="image" src="images/addtocart.gif" border="0" name="creditcard" alt="" />
            </form>

your process.php page will look like this

<?php

   if(isset($_POST['submit'])){

                           here you put your code to handle the form when the button submit has been clicked

                                            }else{

                           here you put your code to handle the form when the button creditcard has been clicked

                                                     }
?>

0
 

Author Closing Comment

by:jackie777
ID: 31564053
thanks again gamebits
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question