【问题标题】:how to echo hidden form fields based on conditional check boxes in php?如何根据php中的条件复选框回显隐藏的表单字段?
【发布时间】:2012-05-13 06:14:41
【问题描述】:

我希望表单根据选中的复选框来回显隐藏的表单字段,注意我需要在按下提交按钮之前回显隐藏的表单字段,因为它将发送到贝宝。这是我到目前为止所拥有的 -

<?php       
echo ('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
echo ('<input type="hidden" name="cmd" value="_cart">');
echo ('<input type="hidden" name="upload" value="1">');
echo ('<input type="hidden" name="business" value="youremail@mail.com">');
echo ('<input type="hidden" name="currency_code" value="US">'); 
echo ('<input type="checkbox" name="camp1" value="a">');
echo ('<input type="checkbox" name="camp2" value="b">');    

if (isset($_POST['camp1'])) {

echo ("<input type='hidden' name='item_name_1' value='beach ball'>");
("<input type='hidden' name='amount_1' value=50'>");
}
if (isset($_POST['camp2'])) {

echo ("<input type='hidden' name='item_name_2' value='towel'>");
("<input type='hidden' name='amount_2' value='20'>");
}
echo ("<input type='submit' value='PayPal'>");
("</form>");
?>

我也试过替换

if (isset($_POST['camp1'])) {

类似

if(IsChecked('camp1','a')) {

没有运气。

任何帮助将不胜感激!

【问题讨论】:

    标签: php forms field hidden checkbox


    【解决方案1】:

    如果您不提交表单,则不会执行 POSTGET 方法,并且您在 if 语句中调用来自 POST $_POST['camp1'] 的变量,您将永远无法获得该值。解决此问题的另一种方法是使用 JS 或 jQuery,例如:

    您修改后的 PHP:

    <html>
    <head>
    <title>Paying process</title>
    </head>
    <script scr="jquery-1.7.2.js" type="text/javascript" />
    <script scr="functions.js" type="text/javascript" />
    <body>
    <?php       
    echo ('<form id="payForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
    echo ('<input type="hidden" name="cmd" value="_cart">');
    echo ('<input type="hidden" name="upload" value="1">');
    echo ('<input type="hidden" name="business" value="youremail@mail.com">');
    echo ('<input type="hidden" name="currency_code" value="US">'); 
    echo ('<input type="checkbox" name="camp1" value="a">');
    echo ('<input type="checkbox" name="camp2" value="b">');    
    echo ('<div id="productInfo" style="display:none;"></div>');
    echo ("<input type='submit' value='PayPal'>");
    echo ("</form>");
    ?>
    </body>
    </html>
    

    functions.js 文件:

    $(document).ready(function(){
    
        $("#payForm").submit(function(e){
           if($('#productInfo').html() == ''){
               //if there is no product in the hidden div, prevent the submit and show msg.
               alert('Please select at least one product');
               e.preventDefault();
           }
        });
    
        // event when user clicks a checkbox
        $("input[type='checkbox']").on('click', function(){
           var checked = $(this).attr('checked');
           if(checked){
              var value = $(this).val();
              $.post('product.php', { value:value }, function(data){
                  // data = 0 - means that there was an error or no product
                  if(data != 0){
                     // At this point you will have your "a" or "b" products in hidden
                     // Also submit button will appear
                     // This means that everything was Ok and the user selected a product
                     $('#productInfo').append(data);
                  } else {
                     // data = 0
                     alert('Please try again.');
                  }
              });
           }
        });
    
    });
    

    product.php 文件(在 jQuery 帖子中使用):

      <?php
        if ($_POST && isset($_POST['value'])) {
    
            // db connection
            $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
            if (!$link) {
               // error happened
               print(0);
            }
            mysql_select_db('mydb');
    
            // sanitize the value
            $value = mysql_real_escape_string($_POST['value']);
    
            // start the query - I am assuming one :-)
            $sql = "SELECT * FROM products WHERE Id = '$value'";
            $result = mysql_query($sql, $link);
    
            // check if the product exists
            if(mysql_num_rows($result) > 0){                                                 
              while($row = mysql_fetch_array($result)){
                if($value == 'a'){
                   echo ("<input type='hidden' name='item_name_1' value='".$row['product_name']."' />");
                   echo ("<input type='hidden' name='amount_1' value='".$row['product_price']."'>");    
                } else {
                   echo ("<input type='hidden' name='item_name_2' value='".$row['product_name']."' />");
                   echo ("<input type='hidden' name='amount_2' value='".$row['product_price']."'>");    
                }                               
              }
            } else {
              // no product found
              print(0);                                       
            }
      ?>
    

    现在,只有当用户至少选择一种产品时,您才能提交隐藏值、项目 1 或 2 或两者。

    希望这会有所帮助:-)

    【讨论】:

    • 对,那么有没有办法在不使用 post 的情况下查看复选框是否被选中?更像是 IsCheked
    • 我正在编辑我的帖子并给你一个例子,尝试使用 jQuery 和 PHP,我现在正在编码,等待 2 分钟并再次检查我的帖子 :-)
    • 完美的奥斯卡,非常感谢!!
    【解决方案2】:

    听起来您正试图让 PHP 执行客户端行为,但这是不可能的。当用户第一次请求页面时,PHP 会呈现页面一次,之后不会执行任何操作,直到他们提交表单或加载另一个页面(或者直到发生其他一些通信,如果您使用的是 ajax 或 jquery)。如果您希望页面内容响应单击复选框而更改,则需要合并一些 javascript。

    【讨论】:

    • 好的,这是一个好的开始,谢谢,我开始寻找 js 的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多