通过POST请求ajax和jQuery从多数组运行forloop

时间:2017-09-19 11:44:30

标签: javascript php jquery mysql ajax

现在我想要做的就是我收到了学生的id,现在我需要将它传递到下面的php页面,但是因为我正在接收数组,然后是脚本

<script type="text/javascript">
    $(document).ready(function(){
    $('#emailIds').on('change',function(){
        var stuemID = $(this).val();
        // alert(stuemID);
         console.log(stuemID);
        if(stuemID){
            $.ajax({
                type:'POST',
                url:'getDetails.php',
                data:'semid='+stuemID,
                success:function(html){
                        //alert('Done');
                    //$('#alerttrig').hide();
                }
            });
        }else{
            alert('Please choose atleast one student to view data.');
            //$('#alerttrig').show();
        }
    });
});
</script>

<?php
    require_once 'connectivity.php';
    //print_r ($_POST['semid']);
    //exit;
    $semid = explode(',',$_POST['semid']);
        //print_r ($semid);
        //exit;
    foreach ($semid as $emid){
        //echo $emid;
        print_r ($emid);
        exit;
        if (isset($emid) & !empty($emid)){ 
            $query="SELECT *, COUNT(*) AS count FROM test_schedule ts
                                                JOIN students s ON s.id=ts.student_id
                                                JOIN students_validity sv ON sv.student_id = s.id
                                                WHERE s.id='$emid' AND ts.exam_id != 76 AND ts.exam_id != 75 AND ts.exam_id != 83 AND ts.exam_id != 84 
                                                GROUP BY ts.student_id
                                                HAVING COUNT(*) > 1
                                                ORDER BY s.id ASC";
                                                print_r ($query);
                                                exit;
        $qryexec=mysqli_query($connect,$query);
        //Count total number of rows
        if (mysqli_num_rows($qryexec) > 0){
            while($row = mysqli_fetch_assoc($qryexec)){
                echo '<scrtip type="text/javascript">';
                echo 'alert('.$row['name'].')';
                echo '</script>';
            }
        }
        else{
             echo 'Something went wrong.!';
        }   
    }
    }
    ?>

所以现在我想转换数组并使其在forloop中可访问并显示通过查询执行收到的数据的警报

1 个答案:

答案 0 :(得分:0)

新的更新答案将此文件单独运行并获取流程

查看文件

<html>
<head>
  <title></title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
</head>
<body>
<select multiple="multiple" name="emailIds[]" id="emailIds" tabindex="2" class="select-multiple">

                                        <option value="">Select Student</option>

                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>

                                    </select>
</body>
</html>





<script type="text/javascript">
    $(document).ready(function(){
    $('#emailIds').on('change',function(){
        var stuemID = $(this).val();
        if(stuemID){
            $.ajax({
                type:'POST',
                url:'getDetails.php',
                data: { semid : stuemID },
                success:function(data){
                        alert(data);
                }
            });
        }else{
            alert('Please choose atleast one student to view data.');
            //$('#alerttrig').show();
        }
    });
});
</script>

getDetails.php

<?php 

    foreach($_POST['semid']  as $semval){

        // Do the code you need
            echo $semval.' ';

    }
    exit();

 ?> 

在select和multiple select上输出get alert:)