为什么我在这段代码中得到空白屏幕

时间:2015-06-03 08:02:06

标签: php jquery

allmob.js:

$(document).ready(function() {
    $(':checkbox').change(function() { 
        var result1 = []; 

        $(':checkbox:checked').each(function() { 
            result1.push($(this).val()); 
        });

        var url = "mob1.php"
        if (result1.length > 0) { 
            //var fin = result1 ;
            $.post(url, { contval: result1 }, function(data) {
                $('#result').html(data);
            });
        }
        else {
            $('#result').html("nothing is checked");
        }
    });
});

如果不是传递result1而是传递1 $.post(url, { contval: 1 }, function(data),我会收到输出,如果我通过result1,则会出现空白屏幕。

mob.php:

 <?php 
     $contval = $_POST['contval'] ;

     if ($contval == "1")
     { 
         echo "1 is checked" ;
     }
   if($contval == "2")

     {
      echo "2 is selected" ;
     }
?>   

allmob.php

   <?php 

  session_start();
    ?>
     <html>
     <head>

      <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
     <link rel="stylesheet" type="text/css" href="css/allmob.css">
     <script type = "text/javascript="js/jquery1.11.3.js">                     </script>
           <script type = "text/javascript" src ="js/allmob.js"></script>
      </head>
     <body>
     <div class = "well">Add2Kart</div>
     <div class = "container">
     <div class = "row">
    <div class = "col-lg-4">
  <h5>Price :</h5>
    <input type = "checkbox" name = "price" value = "1">&#8377;1,000-&#8377;5,000</input><br>
     <input type = "checkbox" name = "price" value = "2">&#8377;5,000-&#8377;10,000</input><br>
         <input type = "checkbox" name = "price" value = "3">&#8377;10,000-&#8377;15,000</input><br>
         <input type = "checkbox" name = "price" value = "BETWEEN 15000 AND 25000">&#8377;15,000-&#8377;25,000</input><br>
         </div>


          <div class = "col-lg-8">
            <div id = "result" >

          </div>
       </div>
      </div>
     </body>
      </html>

选中的复选框由allmob.js处理,所选复选框的值发送到mob.php所以我想要做的是如果选择值1复选框然后在mob.php条件1打印如果机器人1和2复选框被选中两个条件都应打印

1 个答案:

答案 0 :(得分:1)

试试这个......

$_POST['contval']是一个数组,因此您无法使用“==”直接检查它的值

<?php 
     $contval = $_POST['contval'] ;

     if (!empty($contval) &&  in_array("1", $contval)) 
     { 

         echo "1 is checked" ;
     }
?>