0

and thanks for the great help your community provides.

Here is a question regarding Paypal. The code below creates a buy-now non-hosted button. I would like to be able to do run my geoTestArray.php code, currently simply included, as the user presses the Buy Now button, and not when the button is displayed on the page.

Is this possible at all?

Thanks in advance,

Joe

<?php
include ('geoTestArray.php');
echo "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
<input type=\"hidden\" name=\"business\" value=\"shop@mysite.com\">
<input type=\"hidden\" name=\"cmd\" value=\"$cmd\">
<input type=\"hidden\" name=\"amount\" value=\"$price\">
<input type=\"hidden\" name=\"shipping\" value=\"$shipping\">
<input type=\"hidden\" name=\"currency_code\" value=\"$currency\">
<input type=\"hidden\" name=\"shipto\" value=\"$shipto\">
<input type=\"hidden\" name=\"item_name\" value=\"$title\">";
if ($cmd == 'cart') {
echo "<input type=\"hidden\" name=\"add\" value=\"1\">";
}
include('./quantities2.php');
echo "<input type=\"hidden\" name=\"lc\" value=\"US\">
<input class=\"submit\" type=\"image\" src=\"$button\" border=\"0\" name=\"submit\" alt=\"$altaction\">
<img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">
</form>";
?>
user1030520
  • 154
  • 1
  • 13
  • 1
    Possibly, but there may be a better way. What does the geoTestArray do? – Pekka Oct 12 '13 at 14:02
  • It basically finds out the country location of the visiting browser. As I am using a paid service, I only want to use a token when somebody actually presses the buy button, rather than when the page loads. Actually I already have a script that is executed onSumbit, but that is a javascript, and my geoTestArray code is php, so either there is a simple way for doing what I am trying to do, or I will attempt to rewrite my php into javascript. – user1030520 Oct 13 '13 at 07:46

2 Answers2

0

You can display only a form with one input, for example item_id.
After submitting this form you search for item details, execute your geoTestArray.php in backend and after that you can redirect your users to paypal using header("location: $query");

here's how I would do:

<?php
if( isset($_POST['do_action']) && $_POST['do_action'] != "" ) {
    //check for non-empty item_id field
    //run your geoTestArray.php script
    //get item details from database and build your paypal query
    $query = "https://www.paypal.com/cgi-bin/webscr&cmd=_xclick";  // add rest of paypal request fields separated by &
    header( "Location: $query" );
}
?>
<form action='' method='post'>
    <input type='hidden' name='item_id' value'your_item_id'>
    <input type='submit' name='do_action' value='Buy'>
<form>

Hope I helped you! :d

theodorhanu
  • 67
  • 10
  • Hi teoflavian. Thank you for your solution. It looks simple and easy to implement. I will try to see if I can make it work. – user1030520 Oct 13 '13 at 10:03
0

Thanks everyone for comments and solution options. I have eventually chosen to modify an onSubmit javascript I had on my page, by adding:

function InterceptForm(formObj) {
  var newshipto = httpGet('geoTestArray.php');
  formObj.shipto.value =  newshipto;
}

The above code makes use of the httpGet() function posted in this other stackoverflow post:

HTTP GET request in JavaScript?

To complete the puzzle:

The code below is in the html in my page

<script language="javascript">
// During onLoad, loop through all forms and for each form object do something
function InterceptForm(formobj) {
    formobj.onsubmit = function ff() {
        interceptform(formobj);
    };
}
</script>

The code below gathers all forms on the page and runs the above javascript on each (NOTE: you may want to name your forms so that you only run the script on the relevant forms and not on all forms):

<script language="javascript">
function GetForms() {
var formsCollection = document.getElementsByTagName("form");
for(var i=0;i<formsCollection.length;i++)
{
   if (formsCollection[i].name != "seecart")
   {
   InterceptForm(formsCollection[i]);
   }
}
        return true;
}
</script>

The code below is the onLoad function:

<script language="javascript">
function onLoadFunction () {
  GetForms();
  return true;
}
</script>

Cheers,

Joe

Community
  • 1
  • 1
user1030520
  • 154
  • 1
  • 13