【问题标题】:How can you put a php function inside of an html form inside of a php echo?如何将 php 函数放入 php echo 中的 html 表单中?
【发布时间】:2013-06-24 15:31:27
【问题描述】:

我正在尝试制作一个贝宝结帐按钮,当它不在回声内时,该按钮工作正常,但是当我将按钮放在回声内时,因为我需要动态生成它,它在引用时出现问题“贝宝项目();”功能,这是添加用户在其购物车中的所有商品的功能。该按钮有效,它重定向到贝宝网站,但它说没有项目(这让我相信 paypal_items() 没有到达。我尝试了一堆不同的语法,但我想不通我想知道这是否可能,因为它在一个表单内,而且在一个回声内。

cart() 中发生了更多事情,我只是删除了大部分内容,因为它似乎没有必要,它基本上收集了购物车中内容的会话数据,然后将其回显给买家查看。

function paypal_items() { //this function takes the cart and organizes all the variables in a way that paypal can read it (this function is called on in the paypal send form)
$num = 0;
foreach($_SESSION as $name => $value) {
    if ($value!=0) {
        if(substr($name, 0, 5)=='cart_') {
            $id = substr ($name, 5, strlen($name)-5);
            $get = mysql_query ('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
            while ($get_row = mysql_fetch_assoc ($get)) {
                $num++;
                echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">'; //This num and number from database listing is flip flopped.
                echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
                echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
                echo '<input type="hidden" name="shipping_'.$num.'" value="'.$get_row['shipping'].'">'; //Cost of Shipping first item.
                echo '<input type="hidden" name="shipping2_'.$num.'" value="'.$get_row['shipping'].'">'; //Cost of shipping two or more items is applied (shipping is multiplied depending on quantity)
                echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
            }
        }
    }
} 

}

function cart() {
    if ($total = 0) {
           // Empty Cart Alert..
    }

    else {
        echo '<div class="checkoutBtn">'.'<p>
              <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                 <input type="hidden" name="cmd" value="_cart">
                 <input type="hidden" name="upload" value="1">
                 <input type="hidden" name="business" value="blah@blahblah.com">
                 <?php paypal_items(); ?>
                 <input type="hidden" name="currency_code" value="USD">
                 <input type="hidden" name="amount" value="echo number_format($total, 2);">
                 <input type="image" src="images/cartPage/checkoutBtn.gif" width="247" height="54" name="submit" alt="Secure Checkout With PayPal!">
              </form>
            </p>;'.'</div>'.
    }
}

【问题讨论】:

  • 您在回声中使用 标签。删除这些标签,它应该可以工作

标签: php forms function paypal echo


【解决方案1】:

试试这个方法:

function cart() {
    if ($total = 0) {
           // Empty Cart Alert..
    }

    else {
        echo '<div class="checkoutBtn"><p>
              <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                 <input type="hidden" name="cmd" value="_cart">
                 <input type="hidden" name="upload" value="1">
                 <input type="hidden" name="business" value="blah@blahblah.com">';
        paypal_items();
        echo '<input type="hidden" name="currency_code" value="USD">
                 <input type="hidden" name="amount" value="'.number_format($total, 2).'">
                 <input type="image" src="images/cartPage/checkoutBtn.gif" width="247" height="54" name="submit" alt="Secure Checkout With PayPal!">
              </form>
            </p></div>';
    }
}

【讨论】:

    【解决方案2】:

    嗯&lt;?php tags are on the wrong place.

    试试这个:

         <?php
        function cart() {
    if ($total = 0) {
              // Empty Cart Alert..
      }
    else {
        echo '<div class="checkoutBtn">'.'<p>
              <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
                 <input type="hidden" name="cmd" value="_cart">
                 <input type="hidden" name="upload" value="1">
                 <input type="hidden" name="business" value="blah@blahblah.com">
                  paypal_items();
                 <input type="hidden" name="currency_code" value="USD">
                 <input type="hidden" name="amount" value="echo number_format($total, 2);">
                 <input type="image" src="images/cartPage/checkoutBtn.gif" width="247" height="54" name="submit" alt="Secure Checkout With PayPal!">
              </form>
            </p>;'.'</div>'.
       }
    }
    ?>
    

    【讨论】:

    • 感谢您的评论,我已经尝试过了,但没有运气。我认为的问题是 paypal_items 在 ECHO 内 ECHOING ......所以它输出两次,这不起作用?我需要按照你下面的人说的做,把回声分成两个回声,然后把函数放在中间。
    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    相关资源
    最近更新 更多