php获取输入值而不单击按钮或提交它

时间:2016-02-13 14:10:48

标签: javascript php jquery ajax html5

我有一个html代码,其值存储在输入中,隐藏为其类型。我想使用该值并将其放在php变量中,而无需单击按钮或任何东西。只是明白地获取值(因为当我要从sql获取值时我需要它)。我的AJAX代码不起作用...我想要获得的值是45,可以在下面的代码中找到。

这是我的html代码(它在一个模态中,php代码也在那里):

 <div id="CalenderModalEdit" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                   <h4 class="modal-title" id="myModalLabel2">Case Details</h4>
             </div>
             <div class="modal-body">
                <form id="antoform2" class="form-horizontal calender" role="form">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Hearing #</label>
                         <div class="col-sm-9">
                            <p style="margin-top: 8px; text-align: justify" id="title_disp"></p>
                          </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Case #</label>
                            <div class="col-sm-9">
                                <p style="margin-top: 8px; text-align: justify" id="case_id"></p>
                                <input type="text" class="form-control" id="id_case" value="45">
                            </div>
                     </div>


                    <div class="form-group">
                        <label class="col-sm-3 control-label">Complainant (s)</label>
                        <div class="col-sm-9">

    <?php 
    include ("config.php");
    if(isset($_POST['userID'])) {
        $uid = $_POST['userID'];
        echo $uid;

        // Do whatever you want with the $uid

    $view = mysqli_query ($conn, "SELECT idCase, lastName, firstName, Case_idcase, Person_idPerson
                                    FROM complainant, person, bar_case
                                    WHERE Person_idPerson = idPerson AND Case_idCase = idCase AND idCase = '$uid';");

    //mysqli_close($conn);
    while ($line = mysqli_fetch_array($view)) {

?>  

?>                
                            <p style="margin-top: 8px; text-align: justify" id="comp"><?php echo $line['firstName'].' '.$line['lastName']?></p>
<?php }} mysqli_close($conn); ?>
                         </div>
                       </div>

             </form>
         </div>
        <div class="modal-footer">
      <button type="button" class="btn btn-default antoclose2" data-dismiss="modal">Close</button>

      </div>
      </div>
      </div>
      </div>

AJAX SCRIPT:

        <script>

    $(document).ready(function() {
            var userID = $("id_case").attr('id');
            //alert($(this).attr('id'));
            $.ajax({
                        type: "POST",
                        url: "../1-secretary/calendar-hearing.php",
                        data: { userID : userID },
                        success: function(data)
                        {
                            alert("success!");
                        }
                    });
    });



    </script>

我想在同一页面中使用它。我该怎么做?请帮我。非常感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

您(和任何其他)php脚本只是在运行脚本的服务器上生成HTML,然后将此HTML发送到在不同计算机上运行的浏览器。要从HTML页面(您记得在另一台计算机上的浏览器中)读取内容,您需要将一些数据发送回服务器,您的脚本可以在其中拦截它。

HTML,php和SQL代码在服务器上的同一文件中并不意味着生成的HTML页面和php / SQL代码在同一台机器上运行。

因此,您无法在不将数据发送回脚本所在的服务器的情况下获取该值。人们是对的 - 你需要AJAX或表单提交才能获得价值。

相关问题