简体   繁体   English

简单的PHP AJAX:如何调用php脚本

[英]simple PHP AJAX : How to call a php script

I'm trying to get my javascript function to use ajax to call a php script. 我正在尝试让我的JavaScript函数使用Ajax调用php脚本。 The php script will update a MYSQL table but I've tested the PHP script and it works fine. php脚本将更新MYSQL表,但我已经测试了PHP脚本,并且工作正常。

This is my function: 这是我的功能:

    function rate()
{

 $.ajax({
    data: '' ,       
    url: 'update.php',
    method: 'GET', 
    success: function(msg) {
        alert(msg);
    }
});

}

and the function is called later with: 然后使用以下函数调用该函数:

rate();

The php script doesn't need any information given to it, it just needs to be called, can anyone point out where I'm going wrong. php脚本不需要任何信息,只需要调用它,任何人都可以指出我要去哪里了。

Just use like in this example: 只需在此示例中使用:

<script>
  function rate() {
    $.get("update.php");
  }
</script>

Here is an example of how I use the ajax call: 这是我如何使用ajax调用的示例:

$.ajax({
                               type: "POST",
                               url: "page_url",
                               dataType: 'json',
                               data: {
                                   'date1' : date1,
                                   'call': 'function_name'
                               },
                               beforeSend: function(){
                                   $("#loading").show();
                               },
                               success : function(response){    
                                  },
                                complete: function(){ 
                                   $("#loading").hide();
                               }
                      })

and on php part I add: 在PHP部分,我添加:

function function_name($request){
  your code here
}

if (!empty($_POST['call'])) {
    die($_POST['call']($_POST));
}

If you dont need to pass the data to the PHP page, you can omit 'Data' from the ajax parameters. 如果您不需要将数据传递到PHP页面,则可以从ajax参数中省略“数据”。

<script>
    function rate(){
        $.ajax({      
            url: 'update.php',
            method: 'POST', 
            success: function(msg) {
               alert(msg);
            }
        });
    }

    rate();
</script>

Have you included jquery library Try this 您是否包含了jquery库,请尝试一下

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<script>
      function rate()
{

 $.ajax({
    data: '' ,       
    url: 'update.php',
    method: 'GET', 
    success: function(msg) {
        alert(msg);
    }
});

}


    rate();


</script>

i show my all working code, try it: 我显示我所有的工作代码,尝试一下:

<html>
  <head>
    <script type="text/javascript" src="/js/jquery-1.8.2.js"></script>
    <script>
      function rate() {
        $.get("update.php");
      }
    </script>
  </head>

  <body>
    <button onclick="rate()">Click me</button>
  </body>
</html>

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

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