简体   繁体   English

如何通过 ajax 或任何 http 请求获取 $_GET url 中的参数?

[英]How to get the parameters in $_GET url via ajax or any http request?

For example i have this form that i send to queries.php.例如,我有这个表格,我发送给 query.php。

echo "<form method='GET' action='queries.php'>
       <label>name1</label>
       <input type='checkbox' name='name1'/>
       <label>name2</label>
       <input type='checkbox' name='name2'/>
       <label>name3</label>
       <input type='checkbox' name='name3'/>
       <input type='submit' name='sendData' value='Send'/>
      </form>";

In order to perform an ajax call the url can be queries.php?name1=on&name2=on&name3=on&SendData=Send or with less and variable parameters.为了执行 ajax 调用 url 可以查询。php queries.php?name1=on&name2=on&name3=on&SendData=Send或更少参数

If i have main.js how can i access the url if the parameters i send are variables?如果我有 main.js,如果我发送的参数是变量,我如何访问 url? For variables i mean that they are not always the same.对于变量,我的意思是它们并不总是相同的。

$.ajax({
type: "GET",
url: "queries/queries.php?",
dataType: "json",
contentType: "application/json",
}).done(function (data) {
  console.log(data);

}).fail(function (response) {
  console.log(response);

});

} }

Hope i've been clear, thank you.希望我已经清楚了,谢谢。 And sorry if the question can be newbie.对不起,如果问题可能是新手。

I think you're asking how to get the data from the form fields and put it into the URL?我想您是在问如何从表单字段中获取数据并将其放入 URL?

If so, the simplest way is to handle the form's submit event, and then have jQuery serialise the form's data for you, and send it to the server in your AJAX request.如果是这样,最简单的方法是处理表单的提交事件,然后让 jQuery 为您序列化表单的数据,并在您的 AJAX 请求中将其发送到服务器。

For example:例如:

HTML (note the ID on the form): HTML(注意表格上的 ID):

<form method='GET' id="someForm" action='queries.php'>
   <label>name1</label>
   <input type='checkbox' name='name1'/>
   <label>name2</label>
   <input type='checkbox' name='name2'/>
   <label>name3</label>
   <input type='checkbox' name='name3'/>
   <input type='submit' name='sendData' value='Send'/>
</form>

JavaScript/jQuery: JavaScript/jQuery:

$(function() { //wait till page has loaded
  $("#someForm").submit(function(event) { //add "submit" event handler to the form
    event.preventDefault(); //stop normal postback behaviour
    $.ajax({
      type: "GET",
      url: "queries/queries.php?",
      data: $(this).serialize() //grab the form data
      dataType: "json",
    }).done(function (data) {
      console.log(data);
    }).fail(function (response) {
      console.log(response);
    });
  });
});

serialize() will output a querystring (like you showed in your question) automatically and, for a GET request, append it to the URL - although for a form like this, it's usually preferable to use POST to submit, but that's up to you. serialize() will output a querystring (like you showed in your question) automatically and, for a GET request, append it to the URL - although for a form like this, it's usually preferable to use POST to submit, but that's up to you . Documentation: https://api.jquery.com/serialize/文档: https://api.jquery.com/serialize/

Use data property of JQuery AJAX.使用 JQuery AJAX 的数据属性。

$.ajax({
type: "GET",
url: "queries/queries.php?",
data: {name1: variable1, name2: variable2, name3: variable3},
dataType: "json",
contentType: "application/json",
}).done(function (data) {
  console.log(data);
}).fail(function (response) {
  console.log(response);
});

Where variable1, variable2, variable3 are variables and name1, name2, name3 are GET variables.其中 variable1、variable2、variable3 是变量,name1、name2、name3 是 GET 变量。 So in PHP you'll receive them $name1 = $_GET["name1"];所以在 PHP 你会收到他们 $name1 = $_GET["name1"];

If you have html form and want to submit entire form using AJAX, go for @ADyson's approach as it'll submit entire form and easier to write and cleaner too.如果您有 html 表单并想使用 AJAX、go 提交整个表单,因为它会提交整个表单并且更容易编写和清理。

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

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