如何在php中基于条件复选框回显隐藏的表单字段?

如何在php中基于条件复选框回显隐藏的表单字段?,php,forms,field,hidden,checkbox,Php,Forms,Field,Hidden,Checkbox,我希望表单回显基于复选框的隐藏表单字段,注意我需要在按下提交按钮之前回显隐藏表单字段,因为它将转到paypal。这是我到目前为止所拥有的- <?php echo ('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'); echo ('<input type="hidden" name="cmd" value="_cart">'); echo ('<input

我希望表单回显基于复选框的隐藏表单字段,注意我需要在按下提交按钮之前回显隐藏表单字段,因为它将转到paypal。这是我到目前为止所拥有的-

<?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(IsChecked('camp1','a')) {
没有运气


任何帮助都将不胜感激

如果您没有提交表单,则不会执行
POST
GET
方法,并且您在If语句中调用POST
$\u 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>
$(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帖子):


如果您没有提交表单,则不会执行
POST
GET
方法,并且您正在调用If语句中POST
$\u 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>
$(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执行客户端行为,但这是不可能的。当用户第一次请求页面时,PHP只呈现一次页面,在用户提交表单或加载另一个页面之前(或者在使用ajax或jquery时,在发生其他通信之前),PHP不会执行任何操作。如果您希望在单击复选框时更改页面内容,则需要合并一些javascript

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

好的,那么有没有办法不使用post检查复选框?我正在编辑我的文章,给你一个例子,尝试使用jQuery和PHP,我现在正在编码,等待2分钟,再次查看我的文章:-)完美的奥斯卡,非常感谢!!好的,那么有没有办法不使用post检查复选框?我正在编辑我的文章,给你一个例子,尝试使用jQuery和PHP,我现在正在编码,等待2分钟,再次查看我的文章:-)完美的奥斯卡,非常感谢!!好的,这是一个好的开始谢谢,我会开始用jsok寻找解决方案这是一个好的开始谢谢,我会开始用js寻找解决方案