简体   繁体   English

您如何将php函数放在php echo内的html表单中?

[英]How can you put a php function inside of an html form inside of a php echo?

I am trying to make a paypal checkout button, the button works fine when it is not inside of an echo, but when I put the button inside of an echo because I need it to dynamically be produced, it has a problem referencing the "paypal_items();" 我正在尝试制作一个Paypal结帐按钮,当它不在回显中时,该按钮可以正常工作,但是当我将按钮放入回显中时,因为我需要动态生成该按钮,因此它在引用“ paypal_items”时存在问题();” function, which is what adds all of the items that a user has in their cart. 功能,这就是添加用户购物车中所有物品的功能。 The button works, it redirects to the paypal website but it says there are no items (which makes me believe that the paypal_items() isn't getting reached. I've tried a bunch of different syntaxes but I can't figure anything out and I'm wondering if this is even possible because it's inside a form which is furthermore inside of an echo. 该按钮有效,它重定向到贝宝网站,但是它说没有任何内容(这使我相信未达到paypal_items()。我尝试了多种语法,但是我无法弄清楚我想知道这是否可能,因为它位于回声内的表单中。

There is a lot more that happens in cart(), I just erased most of it because it seemed unnecessary, it basically gathers the session data for what is in the cart and then echo's it for the buyer to see. 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>'.
    }
}

Try it this way: 尝试这种方式:

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

Euhm the <?php tags are on the wrong place. Euhm <?php tags are on the wrong place.

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM