简体   繁体   English

如何在不重新加载页面的情况下将数据javascript传递到php页面

[英]how to pass data javascript into php page without reloading the page

hi guys need help regarding how to pass a value to php using javacript without reloading the page.嗨,伙计们需要关于如何使用 javacript 将值传递给 php 而无需重新加载页面的帮助。 What i want to happen is that when i put a value in textbox name num1 that value will go to available.php.. advance thank u.我想要发生的是,当我在文本框名称 num1 中放置一个值时,该值将转到 available.php .. 提前谢谢你。 ... ...

here is my code..这是我的代码..

html code html代码

    <!-- Modal Staff-->
     <div id="add" class="modal fade" role="dialog" tabindex="-1" >
      <div class="modal-dialog">
       <form action="dashboard.php" method="post">
        <!-- Modal content-->
         <div class="modal-content">
         <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times; 
        </button>
        <h4 class="modal-title">Set Available Student</h4>
        </div>
         <div class="modal-body">
         <input type="text" name="num1" id="num1"  class="form-control" />
      </div>
      <div class="modal-footer">
        <input type="submit" name="submit" class="btn btn-success" value="Set">
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
    </form>
  </div>
</div>

javascript code javascript代码

    <script type="text/javascript">

    function availble_chair()
    {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","available.php",false);
    xmlhttp.send(null);
    document.getElementById("count").innerHTML=xmlhttp.responseText;
    }
    availble_chair();
    setInterval(function(){ 
    availble_chair();
    },1000);
    </script>

and here is my php code available.php这是我的 php 代码 available.php

<?php

    $set = '';

    $connection = mysqli_connect("localhost","root","","dbattendancelibrary");
    $query=mysqli_query($connection,"SELECT COUNT(Time_in)-COUNT(Time_out) as Status FROM `tbl_student_form`");
    $result = mysqli_fetch_array($query, MYSQLI_NUM);
    if($result) {
        if($result[0] <= $set) {
            $availble = $set-$result[0];
            echo "<p style='font-size:50px;margin-left:240px;'> " .$availble. " 
            </p>";
         } else {
            echo "<p class='alert alert-danger'>Library Already Full</p>";
         }         
    }
?>

Javascript: Javascript:

First, you need an on submit event listener so we can detect when the form was submitted.首先,您需要一个on submit事件侦听器,以便我们可以检测表单何时提交。

// get a reference to your form

var form = document.getElementsByTagName('form');

// i suggest adding a classname or ID to your form if you have more then one form on the page
// if that is the case, then select the form by ID or class instead

// add event listener to the form

form.addEventListener('submit', function(event) {

    // stop form submitting by default

    event.preventDefault();

    // get the value from the textbox, num1

    var num1 = this.querySelector('#num1').value;

    // send value of num1 to available.php via ajax

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'available.php');
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onload = function() {
        if (ajax.readyState == 4 && ajax.status == 200) {
            console.log("I worked!");
        } else {
            console.log("I didn't work!");
        }
    };
    xhr.send(encodeURI('num1=' + num1));

}

PHP PHP

To get the num1 value from AJAX request:从 AJAX 请求中获取num1值:

<?php

$num1 = isset($_GET['num1']) ? (int) trim($_GET['num1']) : 0;

if ($num1 > 0) {
    // success
}

?>

暂无
暂无

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

相关问题 如何在不重新加载页面的情况下将数据从 HTML 页面传递到 PHP 脚本? - How to pass data from HTML page to PHP script without reloading the page? 如何在不使用XMLHttpRequest()重新加载页面的情况下将datepicker值传递给php; - How to pass datepicker value to php without reloading the page using XMLHttpRequest(); 如何在不重新加载页面的情况下将信息传递给并运行PHP脚本 - How to pass information to and run a PHP script without reloading the page 提交表单后如何清除表单并将数据传递到php文件而不重新加载页面 - how to clear form after submit the form and pass the data to php file without reloading page 如何在不重新加载页面的情况下使用javascript按钮单击更改php的变量 - how to change the variable of php with javascript button click without reloading the page 如何将变量从 javascript 函数传递到 php 文件而不重新加载编写 javascript 函数的当前 html 页面? - how to pass a variable from javascript function to php file without reloading the current html page in which javascript function is written? 如何在不重新加载页面的情况下运行php函数 - how to run php function without reloading the page 将当前HTML文本框值从JavaScript传递到PHP,而无需重新加载页面或写入URL - Pass current HTML textbox value from JavaScript to PHP without reloading the page or writing to the URL 如何在不重新加载页面的情况下提交表单数据 - How to submit a form data without reloading the page 如何在不重新加载页面的情况下提交数据? - How to submit data without reloading page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM