在echo&#d; d形式中发送php变量

时间:2015-10-24 15:36:25

标签: php mysql

单击按钮后,我自己进入位的pickle 尝试通过自定义变量发送到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标签来回应变量

我尝试了许多不同的传递方式,只想获得正确的方法!

3 个答案:

答案 0 :(得分:3)

快速解决方案:

写下这样的一行:

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

答案 1 :(得分:2)

有几种方法可以实现这一目标(其中包括):

  • 经典连接:

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

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

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

如果您使用Heredoc,请不要忘记在没有任何先前空格的情况下放置结尾标签。

答案 2 :(得分:1)

问题是你正在使用单个 quots,它只是回显( - &gt; value =&#34; $ id;&#34;),而不是 double quots,其中PHP搜索变量并插入变量:( - &gt; value =&#34; 1&#34;)。

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

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

或者只是将它连接到你的字符串:(使用点(连接)运算符)

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

希望我能提供帮助, -Minding