Forum Moderators: coopster

Message Too Old, No Replies

How to make image change in PHP when user clicks through

How to make image change in database PHP when user clicks through to Paypal

         

galahad2

7:07 pm on Dec 10, 2008 (gmt 0)

10+ Year Member



I'm trying to set up a page with some standard Paypal code behind a "Buy Now" button, where the button gets changed for another one, i.e buynow.gif gets swapped for sold.gif. So the user not only gets sent to the Paypal site, but at the same time the Buy Now button for that particular product changes to a Sold button (and this is permanent unless manually restored).

The Buy Now button is a field in a table in the mySQL db and is output as the $title7 variable in the code below:

<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\" target=\"_top\">
<div align=\"justify\">
<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" />
<input type=\"hidden\" name=\"business\" value=\"sales@company.co.uk\" />
<input type=\"hidden\" name=\"no_note\" value=\"1\" />
<input type=\"hidden\" name=\"currency_code\" value=\"GBP\" />
<a href=\"#\" onClick=\"javascript:document.img01.src='images/sold.gif'\"><input type=\"image\" src=\"images/$title7.gif\" name=\"submit\" alt=\"Buy this item\" /></a></div></form>

The Javascript in the header is:

<script language = "JavaScript">
function preloader()
{
heavyImage = new Image();
heavyImage.src = "sold.gif";
}
</script>

and then in the body tag: onLoad="javascriptreloader()"
It shows without any problem, but the onClick obviously doesn't work as I guess the image needs to be changed with a mySQL UPDATE statement of some kind- but I'm not sure how that can be integrated with the form above...

I mentioned that the image change won't necessarily mean a sale, but they have a little script I made which uploads and overwrites the "sold" image manually if the sale doesn't go through.

I've tried using a PHP script called changeimage.php as follows, in place of the straight link to Paypal:

<?php
include ('inc/dbconnect.php');
if($_POST['submit'])
{
$title5 = $_POST['_Reference'];

// Build SQL Query
$query = "UPDATE table SET BuyNow = 'sold' WHERE Reference = '$title5'"; // specify the table and field names for the SQL query
}
if($result = mysql_query($query))
{
//go to the new member confirmation page
header('location: "https://www.paypal.com/cgi-bin/webscr\"');
exit;
}
else
{
echo "ERROR: ".mysql_error();
}

?>

But I get an ERROR: Query was empty.

The form code on the button page is now:

<form action=\"changeimage.php\" method=\"post\" target=\"_top\" name=\"submit\" >
<div align=\"justify\">
<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" />
<input type=\"hidden\" name=\"business\" value=\"sales@example.com\" />
<input type=\"hidden\" name=\"no_note\" value=\"1\" />
<input type=\"hidden\" name=\"currency_code\" value=\"GBP\" />
<input type=\"hidden\" name=\"_Reference\" value=\"$title5\">
<input type=\"image\" src=\"http://www.example.com/vb/images/$title7.gif\" name=\"submit\" alt=\"Buy this item\" /></div></form>

The <input type=\"hidden\" name=\"_Reference\" value=\"$title5\"> line definitely works as I changed the input type to input and it displayed the product reference correctly...

I wondered if there was a way of making Paypal somehow change the image although they would need access to the db.

The problem is that there is no link back from Paypal, the button is really just a link to the client's Paypal payment page- Paypal have no access to the server so I can't see any way how they could "cause" the required image change. Paypal don't get any of the form details- the customer simply wanted just a straightforward link to their payment page, to go with each product.

The other thing is that the products are all "one of a kind", therefore as soon as someone buys one, that's it- there are no more. Which is why there has to be a way of making the "Buy Now" link button change to "Sold" but just for that individual product. They've had an occasion recently where someone bought a product and then someone else went and paid for the same product belieiving it was still available. It wasn't, and they had to be refunded.

You can see the problem here!

So what they want is for the Buy Now button to change to a Sold button whenever it's clicked on and the user goes through to the Paypal page. I pointed out that this doesn't necessarily mean a sale, but apparently they're happy to set the button back manually at a later point if that's the case.

Don't see a way round it at this stage...

[edited by: eelixduppy at 7:08 pm (utc) on Dec. 10, 2008]
[edit reason] exemplified [/edit]

Alcoholico

2:20 am on Dec 11, 2008 (gmt 0)

10+ Year Member



Not surprising. Paypal needs the information submitted with the form in order to process your sale, I don't really know if paypal takes GET requests, but I reckon they do, try this:

<?php
$cmd = '_xclick';
$business = 'sales@example.com';
$no_note = '1';
$currency_code = 'GBP';
include ('inc/dbconnect.php');
if(!empty($_POST)) {
$_Reference = !empty($_POST['_Reference']) ? mysql_real_escape_string(trim(strip_tags($_POST['_Reference']))) : '';
if (!empty($_Reference)) {
// Build SQL Query
$query = "UPDATE table SET BuyNow = 'sold' WHERE Reference = '$_Reference'"; // specify the table and field names for the SQL query
}
else {
@mysql_close();
die('Error: _Reference MUST NOT be empty'); #A real, helpful message to your customer would be nice.
}
if($result = mysql_query($query)) {
//go to the new member confirmation page
$qry_str = "https://www.paypal.com/cgi-bin/webscr?cmd=$cmd&business=$business&no_note=$no_note&currency_code=$currency_code";
header("Location: $qry_str");
exit;
}
else {
die( 'ERROR: '.mysql_error());#A real, helpful message to your customer would be nice.
}
}
else {
die('Form was not posted');
}
?>

Notice I have harcoded in the script some of the values posted with the form so you should be able to get rid of them. This is just a a skeleton and I don't know if paypal support GET requests, otherwise you should use a diferent approach when posting to paypal.
Good Luck.

[edited by: Alcoholico at 2:28 am (utc) on Dec. 11, 2008]

galahad2

1:50 pm on Dec 13, 2008 (gmt 0)

10+ Year Member



Thanks, I'll give this a go and see how I get on...

galahad2

12:43 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



Well, that's *almost* working!

I've set it up so that it goes to the Paypal page AND updates the db, BUT only if I put in the actual literal name of the product in the form, e.g

<input type="hidden" name="_Reference" value="CH1">

The above won't work though in reality as it needs to use the variable for Reference, which is grabbed further up in the PHP on the product display page. The full PHP for this almost-working page is:

<?php
include ('inc/dbconnect.php');

// Get the search variable from URL
$refnumber =@$_GET['Reference'];

// Build SQL Query
$query = "select * from table WHERE Reference like '$refnumber'"; // specify the table and field names for the SQL query
$result = mysql_query($query) or die("The System is undergoing maintenance at the moment and will be available shortly");
if($result = mysql_query($query))
{
$row = mysql_fetch_assoc($result);
$title = $row["LargeImage"];
$title2 = $row["Title"];
$title3 = $row["Detail1"];
$title4 = $row["Detail2"];
$title5 = $row["Reference"];
// (this $title5 is the variable I need to use when sending the form below)
$title6 = $row["Price"];


echo "<table width=\"260\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td><img src=\"images/products/$title.jpg\"></td>
</tr>
<tr>
<td class=\"productpadding\"><span class=\"producttextbold\">$title2</span></td>
</tr>
<tr>
<td class=\"productpadding\"><span class=\"producttext\">$title3</span></td>
</tr>
<tr>
<td class=\"productpadding\"><span class=\"producttext\">$title4</span></td>
</tr>
<tr>
<td class=\"productpadding\"><span class=\"producttext\">Ref: $title5</span></td>
</tr>
<tr>
<td class=\"productpadding\"><span class=\"pricetext\">&pound;$title6</span></td>
</tr>
</table>";
}

else
{
echo "ERROR: ".mysql_error();
}

echo '<br />
<div class=\"lefttext_productdisplay\"><p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Click on small images to see details and prices.</strong></p></div>
<div class=\"buynow\">
<form action="paypalsend.php" method="post" target="_top">
<div align="justify">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="business" value="sales@company.co.uk" />
<input type="hidden" name="no_note" value="1" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="_Reference" value="CH1">
//I tried substituting this for the $title5 variable but it treats it literally, can't find a product with name $title5 and therefore doesn't update the db

<input type="image" src="images/buynow2.gif" name="submit" alt="Buy this item" /></div></form>
</div>
</div>';
?>

Any ideas how I can make it treat $title5 as the variable for Reference grabbed earlier?

galahad2

3:38 pm on Dec 14, 2008 (gmt 0)

10+ Year Member



Sorted it- I just used <input type="hidden" name="_Reference" value="<?php echo $title5; ?>" />
...and that did the trick.
Now I just need to get the new value to output from the db, but that's easily done.
Thanks for all your help. :)