dongtuo3530 2016-12-23 19:47
浏览 43
已采纳

Checkbox Array,Jquery,PHP

I have a main.php page with some checkboxes and a textbox, what I am trying to achieve is on selecting of "Select All or any of the checkboxes the values should be passed".The checkbox change function should trigger and post the data to ajaxData.php. The Ajaxdata.php should return the respective team members name back to the main.php page and display the fname values in a textbox.

The problem right now is nothing happens on click on any of the checkboxes

My main.php page is below

Select All <input type="checkbox" name="select_all" id="select_all" value="1">

Pythons<input type="checkbox" name="select[]" id="team" value="Pythons">
Wipers <input type="checkbox" name="select[]" id="team" value="Wipers">
<input type="textbox" name="first_name" value="">
<script>

$("#select_all").change(function() {
   var select_all = $(this).val();
 alert('1');
    if(select_all){
        $.ajax({
            type:'POST',
            url:'ajaxData.php',
            data:'select_all='+select_all,
            success:function(html){
                $('#first_name').html(html);

            }
        }); 
    }else{
        //$('#carrier').html('<option value="">Select State First</option>');

    }

</script>

ajaxData.php code looks like the following

if(isset($_POST["select_all"]) && !empty($_POST["select_all"])){

$select_sql="SELECT * FROM member WHERE team_id = ".$_POST['select_all']." ORDER BY name ASC";
 $result=mysqli_query($con,$select_sql);
 $cnt=mysqli_num_rows($result);
 if($cnt > 0)
 {
        while($results=mysqli_fetch_assoc($result))
            {

            if($results['id'] !=0 )
                {
                 echo $results['fname']                               
            }
}
  • 写回答

1条回答 默认 最新

  • doujiong2533 2016-12-23 20:04
    关注

    You are missing }); at the end, that's why you will get an error like Uncaught SyntaxError: Unexpected end of input:

    $("#select_all").change(function() {
       var select_all = $(this).val();
     alert('1');
        if(select_all){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'select_all='+select_all,
                success:function(html){
                    $('#first_name').html(html);
    
                }
            }); 
        }else{
            //$('#carrier').html('<option value="">Select State First</option>');
    
        }
    }); //this is missing in your code
    

    And in your PHP script you are missing some }(two more exactly), and also missing ; after echo $results['fname'], so change echo $results['fname'] to echo $results['fname']; try this:

    <?php
    if(isset($_POST["select_all"]) && !empty($_POST["select_all"])){
    
    $select_sql="SELECT * FROM member WHERE team_id = ".$_POST['select_all']." ORDER BY name ASC";
     $result=mysqli_query($con,$select_sql);
     $cnt=mysqli_num_rows($result);
     if($cnt > 0){
            while($results=mysqli_fetch_assoc($result)){
    
                if($results['id'] !=0 ){
                     echo $results['fname'];                               
                }
           }
     }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭