如何在php中获取参数

时间:2018-05-14 21:49:25

标签: javascript php html xmlhttprequest

我有一个问题,我提供两个参数,但php文件没有设法获取它们

var xmlhttp=new XMLHttpRequest();
    var url="http://localhost:8090/rest/rest.php?utente="+document.getElementById("utente").value+"&pass="+document.getElementById("pass").value;



    function myFunction(response)
    {
        xmlhttp.onreadystatechange=function()
        {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET",url,true);
        xmlhttp.send();
    }  

这是php

<?php Header('Access-Control-Allow-Origin:*');$utente=$_GET['utente'];$pass=$_GET['pass'];?>

在这里输入代码

1 个答案:

答案 0 :(得分:0)

如何使用javascript发送数据而不刷新?!

https://developer.mozilla.org/pl/docs/XMLHttpRequest

类似的问题:AJAX HTML PHP question

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <button type="button" onclick="get_data()">Get Data!</button>
    <p id="result"></p>
    <script>
    function get_data()
    {
      var http = new XMLHttpRequest();
      http.onreadystatechange = function()
      {
        if(this.readyState == 4 && this.status == 200)
        {
          document.getElementById("result").innerHTML = this.responseText;
        }
      };
      //http.open("GET","http://test.com/go/to/file.php",true);
      http.open("GET","http://test.com/go/to/file.php?name=test&year=2018",true);
      http.send();
    }
    </script>
</body>
</html>

如何使用PHP获取数据参数?

您使用GET向php文件发送请求。

因此参数位于URL。

示例:http://localhost:8090/rest/rest.php?name=test&year=2018

rest.php FIle:

<?php
//print all of the parameters
print_r($_GET);//you can delete this line
//check name parameter exists or no!
//if(isset($_GET['name']))
if( isset($_GET['name']) && isset($_GET['year']))
{
   echo("Hello ".$_GET['name']);
   echo("(");
   echo($_GET['year']);
   echo(")");
}
else
{
   exit("Error!");
}