在echo'd表单中发送php变量


Sending php variables within an echo'd form

让自己陷入了一个小麻烦试图发送一个自定义变量到paypal当按钮被点击

我目前坚持这就是我遇到的问题,以及为什么事务不能正确执行

这是我的代码:

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

我遇到的问题是这一行:

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

发送用户ID给paypal。我知道怎么做,但因为我已经回显了表单,我不认为我需要使用新的php标签来回显变量

我已经尝试了很多不同的方法通过它,只是想得到正确的一个!

只是一个快速的解决方案:

像这样写这行:

'<input type="hidden" name="custom" value="' . $id . '">'
整个代码:

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

如果我是你,我会怎么做:

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

有几种方法可以实现这一点:

  • 经典拼接:

    echo '<input type="hidden" name="custom" value="' . $id . '">';
    
  • 双引号的使用:

    echo "<input type='hidden' name='custom' value='$id'>";
    
  • Heredoc的使用:

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

如果使用Heredoc,不要忘记将结束标签放在单独的行上,前面没有空格。

问题是您正在使用单引号,它只是回显(->value="$id;"),而不是双引号, PHP在其中搜索变量并插入它们:(->value="1")

所以你可以使用双引号:(然后你需要转义HTML双引号)

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

或者直接将其连接到String中:(使用point (concatation)操作符)

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

希望我能帮上忙,-Minding