你怎么能在PHP回声中把一个PHP函数放在一个html表单里面?

时间:2013-06-27 08:10:53

标签: php forms function paypal echo

我正在尝试制作一个paypal结帐按钮,当按钮不在回声中时,按钮工作正常,但是当我将按钮放在回声内部因为我需要它动态生成时,它有一个问题引用“paypal_items();”功能,即添加用户在购物车中的所有商品的功能。按钮工作,它重定向到paypal网站,但它说没有项目(这让我相信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>'.
    }
}

2 个答案:

答案 0 :(得分:0)

以这种方式尝试:

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

答案 1 :(得分:0)

Euhm <?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>'.
   }
}
?>