简体   繁体   English

复选框值发布和jQuery Ajax

[英]check box value posting and jquery ajax

i have this scenario in my program, i'm printing a list of check boxes to my main page using below codes 我的程序中有这种情况,我正在使用以下代码在我的主页上打印复选框列表

<form action="two.php" method="post">
    <div id="option_box" style="width: 250px; height: 400px; overflow: scroll;">
            <?php
            $sql = "SELECT * FROM gene_names ORDER BY name";
            $result = $db->query($sql);
            $counter = 0;
            echo "<table border=\"0\">";
            while ($row = $result->fetch()) {
                $output[] = "<tr>"; 
                $output[] = "<td>"; 
                $output[] = "<input type=\"checkbox\" value=\"".$row['name']."\" name=\"things[]\" id=\"ran\"/>";   
                $output[] = " ";     
                $output[] = $row['name'];
                                $output[] = "</td>";  
                $output[] = "</tr>";    
                $counter++;
            }
            echo join('',$output);
            echo "</table>";
        ?>
    </div>
    <input type="submit" value="See Interactions" name="see" id="see"/>
 </form>

after that i'm using the below code to get the process done using jquery ajax 之后,我使用下面的代码使用jquery ajax完成该过程

        $('#see').click(function(eve){
                eve.preventDefault();
                $.post('two.php', function(data) {
                        $('#results_of_interactions').html(data);
                });
    });

but i need to get the selected check box values inside my two.php(the files handling the processes of the above form) file in order to execute a sql query, but when i use this jquery approach the check box variables are not available at my two.php file, so my query is not properly executed and i can't get the results, how can i correct this issue?please suggest some thing to do, a modification to this or another approach? 但是我需要在我的two.php(处理上述表单过程的文件)文件中获取选定的复选框值,以便执行sql查询,但是当我使用此jquery方法时,复选框变量不可用我的two.php文件,因此我的查询未正确执行,我无法获得结果,我该如何解决此问题?请提出一些建议,对此方法或另一种方法进行修改?

regards, Rangana 问候,Rangana

That's because you have to manually prepare the data and send it along with your post request, it does not submit the form, it submits whatever you manually tell it to submit to two.php 那是因为您必须手动准备数据并将其与您的发帖请求一起发送,它不提交表单,而是提交您手动告诉它提交给two.php

$('#see').click(function(eve){
    eve.preventDefault();

    //serialize form data to be sent with request
    var data = $('form').serialize();

    //send request with 'data'
    $.post('two.php', data, function(data) {
        $('#results_of_interactions').html(data);
    },'json');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM