javascript - php无需单击按钮或提交即可获取输入值

标签 javascript php jquery ajax html

我有一个 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>

    $(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>

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

最佳答案

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

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

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

关于javascript - php无需单击按钮或提交即可获取输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380935/

相关文章:

javascript - jquery selectbox css - 尝试更改字体颜色无法按预期工作

javascript - 判断url是true还是false

javascript - 简化不同选择器的重复 onclick 事件

javascript - 使用 toolbox-loader 配置 react-toolbox sass 变量

php - imagewebp (php) 创建损坏的 webp 文件

javascript - 如何检测浏览器高度,减去一定数量的像素,然后输出数字?

jquery - 需要帮助优化下面的 jquery 代码

javascript - `Class<Component>`在JS中的含义

javascript - 如何设置新属性并在输入类型复选框中对该属性的值求和?

php - xt:Commerce 嵌套集 categories_left 和 categories_right