Multiple form actions with one submit button

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 30795 Views - Last Post: 09 July 2011 - 08:55 AM Rate Topic: -----

#1 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Multiple form actions with one submit button

Posted 05 July 2011 - 09:59 PM

I'm trying to use an insert record query in Dreamweaver to insert transaction details such as item type, name, address, price, etc into my MySQL database. I'm also making the user initiate a transaction via paypal. So I want the "Pay with PayPal button" to insert the data-set into the database and redirect the user to paypal to pay the amount they entered in the price box. Any way to initiate two form actions at once with one button? I've heard you can use Javascript, but I'm not sure how. Any help would be greatly appreciated. FYI I'm not using any PayPal API. I'm just using the HTML based form.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple form actions with one submit button

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Multiple form actions with one submit button

Posted 05 July 2011 - 10:22 PM

Something like this:

if(issset($_POST['btnSubmit']) && $_POST['btnSubmit'] == "PayPal"){
// redirect to paypal page
}else if(issset($_POST['btnSubmit']) && $_POST['btnSubmit'] == "DataOnly"){
// redirect to normal page
}


Was This Post Helpful? 0
  • +
  • -

#3 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 05 July 2011 - 10:35 PM

So this checks to see if both data arrays have populated forms and performs both actions? I'm sorry, it's late and I'm trying to figure things out. Do you mind breaking it down for me?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Multiple form actions with one submit button

Posted 05 July 2011 - 10:42 PM

make a server script that triggers those two actions.
Was This Post Helpful? 0
  • +
  • -

#5 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Multiple form actions with one submit button

Posted 05 July 2011 - 11:04 PM

View Postthatgoogleguy, on 05 July 2011 - 10:35 PM, said:

So this checks to see if both data arrays have populated forms and performs both actions? I'm sorry, it's late and I'm trying to figure things out. Do you mind breaking it down for me?


let me make it more clear for you


Form below has 2 buttons (Submit & Pay Pal) if you submit form with any of these buttons it will go to action.php file and action.php file will take care of these buttons
HTML Form
<form name="form1" method="post" action="action.php">
  <label>
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Pay Pal">
  </label>
  <label>
    <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit">
  </label>
</form>




action.php this page is checking coming value of btnSubmit and executing code...
<?PHP
if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'] == "Pay Pal"){
	echo "Code for paypal button";	
}else if(isset($_POST['btnSubmit']) && $_POST['btnSubmit'] == "Submit"){
	echo "Code for submit button";	
}
?>



Hope it helps you...
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 07:11 AM

action.php is a bit redundant :P
This code
if(.... && $_POST['btnSubmit'] == ...)
will cause PHP to spit a E_WARNING if $_POST['btnSubmit'] doesn't exist. Then again since we're using a && some compilers will terminate the IF statement if the first condition is false. Not sure about PHP's interpreter on that one.

However never use IF statements to check if a value exists because it has to retrieve the value before doing the comparison. isset would suffice.
if(isset($_POST['btnSubmit']))
{
	switch ($_POST['btnSubmit'])
	{
		case "Pay Pay":
			///... code
			break;

		case "Submit":
			///... code
			break;
	}
}

This post has been edited by codeprada: 06 July 2011 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

#7 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 07:36 AM

Thanks Codprada. I'll give it a try. It definitely makes more sense now.
Was This Post Helpful? 0
  • +
  • -

#8 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 08:29 AM

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
//insert services, pay for services
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) 
{
	switch ($_POST["MM_insert"])
	{
		case "PayPal":
		//code

		break;
		
		case "Submit":
		$insertSQL = sprintf("INSERT INTO my_table (type, fname, lname, address, `state`, price, city, `description`, username,) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['type'], "text"),
                       GetSQLValueString($_POST['fname'], "text"),
                       GetSQLValueString($_POST['lname'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['state'], "text"),
                       GetSQLValueString($_POST['price'], "int"),
                       GetSQLValueString($_POST['city'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['username'], "text"),


  mysql_select_db($database, $-------);
  $Result1 = mysql_query($insertSQL, $------) or die(mysql_error());
		break;
	}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My Company | Order Form</title>
</head>

<body>
<?php 


?>

<!---------------FORM-------------->
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Type:</td>
      <td><input type="text" name="type" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">First Name:</td>
      <td><input type="text" name="fname" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Last Name:</td>
      <td><input type="text" name="lname" value="" size="32"></td>
    </tr>

So this is the form shortened up and after I place the original Dreamweaver code inside the algorithm that codeprada has supplied.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">


The above code is the form action that PayPal recognizes. I'm fully aware that I'm letting the user select the price of the goods! I still don't see how these two form actions fit into the switch statement.
Was This Post Helpful? 0
  • +
  • -

#9 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 08:41 AM

Exactly what are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#10 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 08:59 AM

I'm trying to create a custom form that submits the values entered into the database for future references as well and send the total value to PayPal to be processed. So I run into the issue of having to perform two tasks with one button. I Googled up on this issue, but didn't find too many helpful articles.
Was This Post Helpful? 0
  • +
  • -

#11 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 10:16 AM

So you want to run two task with the same button as in one after the other or either one of the two like above?

I suggest making these tasks functions so you can call them in any order you want.
Was This Post Helpful? 0
  • +
  • -

#12 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 11:06 AM

Yea pretty much. I'd rather save the form field contents to the database and then send the order to PayPal. I'm not sure how to put a form action in a function. I'm completely clueless how to do this. PayPal HTML only supports custom fields so this a HUGE draw back.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick"/>
<input type="hidden" name="business" value="seller@apress.com"/>
<input type="hidden" name="item_name" value="Red Jacket"/>
<input type="hidden" name="item_number" value="7601"/>
<input type="hidden" name="amount" value="125.00"/>
<input type="hidden" name="no_shipping" value="2"/>
<input type="hidden" name="no_note" value="1"/>
<input type="hidden" name="currency_code" value="USD"/>
<input type="hidden" name="bn" value="PP-BuyNowBF"/>
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif"
border="0" name="submit"
alt="Make payments with PayPal - it's fast, free and secure!"/>
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif"
width="1" height="1"/>
</form>



The code above will generate a PayPal button passing the hidden fields onto the PayPal payment processor. If I want to add anymore fields I'm limited to only two. This really sucks because I want to collect a lot more information from clients without upgrading and using PayPal API and being charged a $20 monthly charge. I know there's a work around it, I'm just not sure how I would go about doing it. :helpsmilie:

noorahmad I just saw your post. I think it solved things. Using jQuery or simple CSS I'm going to make the first button hidden.
Was This Post Helpful? 0
  • +
  • -

#13 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 11:33 AM

Spoiler



I think I know what you're trying to do. You're trying to identify which form is submitting the data. Instead of having two buttons and hiding one make another hidden input element and assign it a value of Paypal.
<input type="hidden" name="form_type" value="paypal" />

<?php
if(isset($_POST['submit']))
{
	if($_POST['form_type'] == 'paypal')
	{
		//the submit came from the paypal form
		//handle data
	}
}
?>


Your form's action doesn't point to a file in your domain so there's no way you can capture the values to enter them into your database.
Was This Post Helpful? 0
  • +
  • -

#14 thatgoogleguy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 76
  • Joined: 26-May 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 11:41 AM

I'm completely lost now. Sorry for the difficulty guys. I'll explain what I'm trying to do in the simplest way I can.

I want 1 submit button to perform the action of taking the user to PayPal to Pay as well as take the information they filled out and save it into my own database since PayPal doesn't offer access to their database. I already know where values are coming from. I guess I'm trying to combine a PayPal form with my own custom form.

I think Ajax can get the job done pretty easy.
Was This Post Helpful? 0
  • +
  • -

#15 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Multiple form actions with one submit button

Posted 06 July 2011 - 12:11 PM

Uhhh well point your form to your PHP script get the values from $_POST place them in your database then use cURL to send your form variables via post to paypal.
Rundown
<!DOCTYPE html>
<html>
<body>
<form action="script.php" method="post">
	<input type="hidden" name="element_name" value="50" />
	<input type="submit" name="submit" value="Submit" />
</form>
</html>


<?php
//... enter the values into your database
//... INSERT INTO ....
$curl = curl_init("https://www.paypal.com/cgi-bin/webscr");
///////////////////
//READ THIS FIRST//
//http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($curl, CURLOPT_POST, true);

//set last parameter which is your post string to suit your form
curl_setopt($curl, CURLOPT_POSTFIELDS, "business=whatever&item_name=....etc"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  //return any results
$result = curl_exec($curl); //execute 
?>


Please make sure you read http://unitstep.net/...rotected-sites/ or else your script won't work.

This post has been edited by codeprada: 06 July 2011 - 12:13 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2