0

Got myself into a bit of a pickle attempting to send over a custom variable to paypal when the button is clicked

I'm currently adamant that this is the problem I'm having and why the transactions aren't executing correctly

So here's my code:

<?php
                $check = $mysqli->query("SELECT is_member FROM users WHERE username = '$username'");
                $row = $check->fetch_assoc();
                $isMember = $row['is_member'];
                if ($isMember == 0){
                echo'
                <p>To gain access to the members section and receive daily horse racing tips from our professionals purchase premium membership</a></p>
                <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
                <input type="hidden" name="custom" value="$id;">
                <input type="hidden" name="cmd" value="_s-xclick">
                <input type="hidden" name="hosted_button_id" value="CVWJZN5AALBVJ">
                <input type="image" src="https://www.sandbox.paypal.com/en_US/GB/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
                <img alt="" border="0" src="https://www.sandbox.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
                </form>
                ';

                }else{
                echo'<p> You are a member, <a href="membership.php">click here</a> to access the membership page </p>';
                }
                ?>

The problem I'm having is this line:

<input type="hidden" name="custom" value="$id;">

Sending the ID of the user to paypal. I know how to do this normally, but because I'm already echoing out the form I don't believe I need to use new php tags to echo out the variable

I have tried many different ways of passing it, just want to get the correct one!

3 Answers 3

3

Just a quick solution:

Write that line like this:

'<input type="hidden" name="custom" value="' . $id . '">'

Entire code:

$check = $mysqli->query("SELECT is_member FROM users WHERE username = '$username'");
                $row = $check->fetch_assoc();
                $isMember = $row['is_member'];
                if ($isMember == 0){
                echo'
                <p>To gain access to the members section and receive daily horse racing tips from our professionals purchase premium membership</a></p>
                <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
                <input type="hidden" name="custom" value="' . $id . '">
                <input type="hidden" name="cmd" value="_s-xclick">
                <input type="hidden" name="hosted_button_id" value="CVWJZN5AALBVJ">
                <input type="image" src="https://www.sandbox.paypal.com/en_US/GB/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
                <img alt="" border="0" src="https://www.sandbox.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
                </form>
                ';

                }else{
                echo'<p> You are a member, <a href="membership.php">click here</a> to access the membership page </p>';
                }

What I would have done if I were you:

<?php
$check = $mysqli->query("SELECT is_member FROM users WHERE username = '$username'");
                    $row = $check->fetch_assoc();
                    $isMember = $row['is_member'];
                    if ($isMember == 0){
                    ?>
                    <p>To gain access to the members section and receive daily horse racing tips from our professionals purchase premium membership</a></p>
                    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
                    <input type="hidden" name="custom" value="<?php echo $id; ?>">
                    <input type="hidden" name="cmd" value="_s-xclick">
                    <input type="hidden" name="hosted_button_id" value="CVWJZN5AALBVJ">
                    <input type="image" src="https://www.sandbox.paypal.com/en_US/GB/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
                    <img alt="" border="0" src="https://www.sandbox.paypal.com/en_GB/i/scr/pixel.gif" width="1" height="1">
                    </form>
                    <?php

                    }else{
                    echo'<p> You are a member, <a href="membership.php">click here</a> to access the membership page </p>';
                    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

To add, this is because variables aren't interpolated when using single quotes for the string, only double quotes.
Thank you, turns out the problem wasn't even the id :)
2

There are several ways to achieve this (among other) :

  • Classic concatenation :

    echo '<input type="hidden" name="custom" value="' . $id . '">';
    
  • Use of double quotes wrapping :

    echo "<input type='hidden' name='custom' value='$id'>";
    
  • Use of Heredoc :

    echo <<<EOF
    <input type="hidden" name="custom" value="$id">
    EOF;
    

If you use Heredoc, don't forget to place the ending label without any preceding space, on its own line.

Comments

1

The Problem is that you're using single quots, which just echo (->value="$id;"), instead of double quots, in which PHP searchs for variables and inserts them: (->value="1").

So you could use double quots: (than you need to escape the HTML double quots)

echo "<input type=\"hidden\" name=\"custom\" value=\"$id;\">";

Or just concat this into your String: (with the point (concatenation) operator)

echo '<input type="hidden" name="custom" value="'.$id.'">';

Hope I could help, -Minding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.