Small Legend
Small Legend

Reputation: 688

Sending php variables within an echo'd form

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!

Upvotes: 0

Views: 74

Answers (3)

TipuZaynSultan
TipuZaynSultan

Reputation: 783

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>';
                    }
?>

Upvotes: 3

Minding
Minding

Reputation: 1414

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

Upvotes: 1

Maen
Maen

Reputation: 10698

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.

Upvotes: 2

Related Questions