10 Replies - 3420 Views - Last Post: 09 March 2012 - 04:09 AM Rate Topic: -----

#1 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Dynamically creating paypal button using php

Posted 08 March 2012 - 02:40 AM

Hello, I want to create a paypal add to cart button dynamically. I took the code from paypal website then changed accordingly to add item name, price and description from database. However, I am getting the following error


Parse error: syntax error, unexpected '<' in /hermes/bosweb/web101/b1014/ipg.shopbyclickorg/buttonx.php on line 22

Here is the code

<html>
<head>
<title> product</title>
</head>
<body>
<?php
$hostname = "shopbyclickorg.ipagemysql.com";
	$username = "shopbyclickorg";
	$password = "*********";
	if(!($link = mysql_connect($hostname,	$username,$password)))
	      die("Could not connect to database.");
	$databasename = "e_commercedb";
	if(!(mysql_select_db($databasename,$link)))
	      die("Could not open table.");
$result =mysql_query( 'SELECT * FROM product');
$output[] = '<ul>';
while ($row = mysql_fetch_array( $result ))  {
	$output[] = '<li>"'.$row['PName'].'" : '.$row['Pdescription'].': &pound;'.$row['Pprice'].'<br /></li>';
}
$output[] = '</ul>';
echo join('',$output);
[color="#FF0000"]<form action="https://www.paypal.com/cgi-bin/webscr" method="post">[/color]
<input type="hidden" name="cmd" value="_s-xclick">
<input type=\"hidden\" name=\"item_name\" value=\"" . $row['PName'] . "\" />
<input type=\"hidden\" name=\"item_number\" value=\"" . $row['Pdescription'] . "\" />
<input type=\"hidden\" name=\"amount\" value=\"" . $row['Pprice'] . "\" />
<input type="hidden" name="hosted_button_id" value="XF6W8F8SUAYKW">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
?>
</body>
</html>
 


The line in red is the 22nd line.. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically creating paypal button using php

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 02:47 AM

You have placed HTML code within a PHP tag, these are two distinct languages so you must break out of PHP to output HTML like this, or use echo/print etc. Your code actually looks like it's being copied from something where it is being echo'd, and then changed slightly (and incorrectly).

You'll be looking for something like this (untested):
<html>
<head>
<title> product</title>
</head>
<body>
<?php
$hostname = "shopbyclickorg.ipagemysql.com";
	$username = "shopbyclickorg";
	$password = "*********";
	if(!($link = mysql_connect($hostname,	$username,$password)))
	      die("Could not connect to database.");
	$databasename = "e_commercedb";
	if(!(mysql_select_db($databasename,$link)))
	      die("Could not open table.");
$result =mysql_query( 'SELECT * FROM product');
$output[] = '<ul>';
while ($row = mysql_fetch_array( $result ))  {
	$output[] = '<li>"'.$row['PName'].'" : '.$row['Pdescription'].': &pound;'.$row['Pprice'].'<br /></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="item_name" value="<?php echo $row['PName'] ?>" />
<input type="hidden" name="item_number" value="<?php echo $row['Pdescription'] ?>" />
<input type="hidden" name="amount" value="<?php echo $row['Pprice'] ?>" />
<input type="hidden" name="hosted_button_id" value="XF6W8F8SUAYKW">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</body>
</html>


Was This Post Helpful? 2
  • +
  • -

#3 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 05:38 AM

Thanks. It worked. However, there is one more problem. I want a button generated for each item in the database but with this code only one button is generated. I tried to include the form within the while loop but it doesn't work. Any idea how could I create a loop to have one button for each item?
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 06:48 AM

You can simply move that code into the while loop and it will output many of the forms.
Was This Post Helpful? 0
  • +
  • -

#5 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 08:16 AM

I did place the form in while loop but I am getting this error:

Parse error: syntax error, unexpected $end in /hermes/bosweb/web101/b1014/ipg.shopbyclickorg/buttonx.php on line 39

I know this error is generated when there is a missing curl bracket but there is no missing bracket in the code. Here is the code.
 <html>
<head>
<title> product</title>
</head>
<body>
<?php
$hostname = "shopbyclickorg.ipagemysql.com";
	$username = "shopbyclickorg";
	$password = "********";
	if(!($link = mysql_connect($hostname,	$username,$password)))
	      die("Could not connect to database.");
	$databasename = "e_commercedb";
	if(!(mysql_select_db($databasename,$link)))
	      die("Could not open table.");
$result =mysql_query( 'SELECT * FROM product');
$output[] = '<ul>';
while ($row = mysql_fetch_array( $result ))  {
	$output[] = '<li>"'.$row['PName'].'" : '.$row['description'].'  $'.$row['Pprice'].'<br /></li>';
?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="item_name" value="<?php echo $row['PName'] ?>" />
<input type="hidden" name="item_number" value="<?php echo $row['description'] ?>" />
<input type="hidden" name="amount" value="<?php echo $row['Pprice'] ?>" />
<input type="hidden" name="hosted_button_id" value="XF6W8F8SUAYKW">
<input type="image" src="images/addcart.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>
}
$output[] = '</ul>';
<?php
echo join('',$output);
?>
</body>
</html>

This post has been edited by Coolest_gal: 08 March 2012 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 08:21 AM

There is no line 39 in the code. :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 08:43 AM

Hi,

while ($row = mysql_fetch_array( $result ))  {



You have the closing bracket for that while loop, as well as another statement, outside <?php tags:
}
$output[] = '</ul>';



These things can be avoided by using code separation. Your code indenting is a bit inconstant as well, that makes things harder to read.

Hope this helps :)
Was This Post Helpful? 1
  • +
  • -

#8 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 03:04 PM

I will try to use the code separator. However, as of now is there any way to insert the form in while loop within php?
Was This Post Helpful? 0
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 03:15 PM

Yes, we've been over this in my initial reply to this post:
<input type="image" src="images/addcart.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>
}
$output[] = '</ul>';
<?php
echo join('',$output);
?>
</body>
</html>



PHP and HTML are separate, this is what <?php is for. You need to go back into PHP where your HTML ends.
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: Dynamically creating paypal button using php

Posted 08 March 2012 - 03:48 PM

Imagine if you used PHP to generate the HTML, put it in a file that you saved to a drive that you then mailed to the user who would open that HTML file with their browser. Imagine also that the user has no way to process PHP files. Do you think that you'd want to mix PHP and HTML in the same file then? I think you'd want to be sure that you only used PHP on your computer because it would cause problems if you didn't, right?

The only difference between the above paragraph and reality is that instead of mailing the HTML to the user it is sent electronically. Read the link, separate your code.
Was This Post Helpful? 0
  • +
  • -

#11 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Dynamically creating paypal button using php

Posted 09 March 2012 - 04:09 AM

View PostRudiVisser, on 08 March 2012 - 03:15 PM, said:

Yes, we've been over this in my initial reply to this post:
<input type="image" src="images/addcart.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>
<?php
}
?>
$output[] = '</ul>';
<?php
echo join('',$output);
?>
</body>
</html>



PHP and HTML are separate, this is what <?php is for. You need to go back into PHP where your HTML ends.


So with a little change in the code I now have a button for each item. However, each button takes the value of first item only!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1