Create a Function from a button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Create a Function from a button

Post by NYColt »

Hello All

Thank you for allowing me to post to your forum.

I have a button that I need to turn into a function. here is the code that works for the button followed by the function code which is not setting the values for the button. Can anyone see what I may be doing wrong here? Thanks

Working Button:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value=<? renderSingleListingItemRaw($listingID, "email") ?>>
<input type="hidden" name="item_name" value=<? renderSingleListingItemRaw($listingID, "item_name") ?>>
<input type="hidden" name="amount" value=<? renderSingleListingItemRaw($listingID, "amount") ?>>
<input type="hidden" name="return" value="http://www.mysite.com">
<input type="hidden" name="notify_url" value="http://www.mysite.com">
<input type="image" src="https://www.paypal.com/images/x-click-but23.gif" name="submit">
</form>

Function code NOT working:
<?php
function renderpaypal() {
echo "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" />\n";
echo "<input type=\"hidden\" name=\"business\" value=\"" . renderSingleListingItemRaw($listingID, "email") . "\" />\n";
echo "<input type=\"hidden\" name=\"item_name\" value=\"" . renderSingleListingItemRaw($listingID, "item_name") . "\" />\n";

echo "<input type=\"hidden\" name=\"amount\" value=\"" . renderSingleListingItemRaw($listingID, "amount") . "\" />\n";
echo "<input type=\"hidden\" name=\"return\" value=\"http://www.mysite.com\" />\n";
echo "<input type=\"hidden\" name=\"notify_url\" value=\"http://www.mysite.com\" />\n";
echo "<input type=\"image\" src=\"https://www.paypal.com/images/x-click-but23.gif\" name=\"submit\" />\n";
echo "</form>\n";
}
?>

Any insight would be GREAT! Thanks.

NYColt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

looks to me that renderSingleListingItemRaw() echo/print's a string instead of returning one..
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

Hey thanks for the quick look,

I don't see it, how should it look?

Thanks
NYColt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you do this:

Code: Select all

<?php
renderSingleListingItemRaw("test","email");
?>
does it print anything? if so, then it's doing an echo/print.
look into it's code, if you have it, and possibly switch it to returning the final string instead of echo/print..
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

You see the first bit of code for the button sets the value. This works:

<input type="hidden" name="business" value=<? renderSingleListingItemRaw($listingID, "email") ?>>

What does NOT work is when I tried to do the same thing with the button but call it as a function. This does not work:

echo "<input type=\"hidden\" name=\"business\" value=\"" . renderSingleListingItemRaw($listingID, "email") . "\" />\n";

You see what I mean?

NYColt
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

are you calling the function aswell ?

Code: Select all

//function 

renderpaypal();
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

Called it like this: <?php renderpaypal() ?> should work right?
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

Tried it like this too <php? renderpaypal(); ?> and still did not set values.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

NYcolt wrote:Called it like this: <?php renderpaypal() ?> should work right?
you should look at youre browser for that, think you have seen the answer already ;-)

however..

Code: Select all

// here is a function

<?php
function foo()
{
  function bar()
  {
   echo "I don't exist until foo() is called.\n";
  }
}
// calling the functions 
foo();
bar();

?>
Straight from http://www.php.net

That would mean youre code should be something like this

Code: Select all

<?php
function renderpaypal() {
echo "<form action="https://www.paypal.com/cgi-bin/webscr" method="post">\n";
echo "<input type="hidden" name="cmd" value="_xclick" />\n";
echo "<input type="hidden" name="business" value="" . renderSingleListingItemRaw($listingID, "email") . "" />\n";
echo "<input type="hidden" name="item_name" value="" . renderSingleListingItemRaw($listingID, "item_name") . "" />\n";

echo "<input type="hidden" name="amount" value="" . renderSingleListingItemRaw($listingID, "amount") . "" />\n";
echo "<input type="hidden" name="return" value="http://www.mysite.com" />\n";
echo "<input type="hidden" name="notify_url" value="http://www.mysite.com" />\n";
echo "<input type="image" src="https://www.paypal.com/images/x-click-but23.gif" name="submit" />\n";
echo "</form>\n";
}
renderpaypal();  // Now we are calling it ..
?>
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

Thanks,

Calling the function is NOT the problem that is working fine.

The problem is in the button itself.
Before I converted the button to a function the button worked fine.

Button values set when I put the button directly on the page with this code:

<input type="hidden" name="business" value=<? renderSingleListingItemRaw($listingID, "email") ?>> <----this works. This sets the email address for pay pal, price Item ID everything.

When I turned it into a function I messed something up here:
echo "<input type=\"hidden\" name=\"business\" value=\"" . renderSingleListingItemRaw($listingID, "email") . "\" />\n"; <---this does NOT work I have the translation wrong.

See what I mean?

Something in the php syntax on the second bit of code is not right. It does not do the same thing the the first bit of code does.

Thanks
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Yep that might be true however without executing the function it wont do anything either ;-)

Just a small hint.. for the future when messing around with functions and all those.


Glad to hear you fixed it all tho
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

Hi,

I still don't have it working.

That 2nd bit of function code is not setting the value the way the first code is. the function code is not oassing the value to Pay pal.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

i wouldnt know how to help you on that part since most of youre code is using
other functions, that code is not availeble here..

Code: Select all

renderSingleListingItemRaw($listingID, "email")
Now that looks to me like a function
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hang on, just noticed this. If I'm not mistaken, unless you have register globals on, $listingID will not be set once you are in the function. Try replacing this:

Code: Select all

function renderpaypal() {
with:

Code: Select all

function renderpaypal() {
$listingID = $GLOBALS['listingID'];
NYColt
Forum Newbie
Posts: 17
Joined: Wed Apr 07, 2004 4:49 pm

Post by NYColt »

I will try that now, I still call the function the same way right?
Post Reply