Link to home
Start Free TrialLog in
Avatar of jackie777
jackie777

asked on

php form with two buttons

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

Avatar of gamebits
gamebits
Flag of Canada image

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

Avatar of jackie777
jackie777

ASKER

Thanks gamebits.

Could you please show me an example of the code you are talking about.
ASKER CERTIFIED SOLUTION
Avatar of gamebits
gamebits
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks again gamebits