简体   繁体   English

如何通过$ .ajax()将JavaScript变量发送到PHP文件中

[英]How do I send a JavaScript variable into a PHP file via $.ajax()

How do I send the JavaScript variable exampleId into the https://example.com/widget/widget-updater.php php file from within the $.ajax() function below? 如何从下面的$ .ajax()函数中将JavaScript变量exampleId发送到https://example.com/widget/widget-updater.php php文件中?

jQuery(document).ready(function($) {   

var element = document.getElementById('widget');
var exampleId = element.getAttribute('attributeExample');

alert(exampleId); /* This prints out correctly into a pop up box */



$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php"
});        

})(); 

I know how to get the variable once its in PHP with $_GET['exampleId'], but I don't know how to send it to the PHP file via the $.ajax() function above. 我知道如何使用$ _GET ['exampleId']在PHP中获取变量,但是我不知道如何通过上述$ .ajax()函数将其发送到PHP文件。 Thanks. 谢谢。

Here is the HTML Code: 这是HTML代码:

<script 
src="https://example.com/widget/externalJSfileContainingTheCodeBelow.js" 
async></script><div id="widget" dataVar="1"></div>

Here is the Javascript / jQuery Code 这是Javascript / jQuery代码

(function () {
var jQuery;
if (window.jQuery === undefined) {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
  script_tag.onload = scriptLoadHandler;
}
(document.getElementsByTagName("head")[0] ||
  document.documentElement).appendChild(script_tag);
} else {
jQuery = window.jQuery;
main();
}

function scriptLoadHandler() {
jQuery = window.jQuery.noConflict(true);
main();
}

function main() {
jQuery(document).ready(function ($) {

var element = document.getElementById('widget');
var numberVariable = element.getAttribute('dataVar');
alert(numberVariable);

$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php",
data: { id: numberVariable }
});
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "https://example.com/widget/widget.css"
});
css_link.appendTo('head');
var jsonp_url = "https://example.com/cgi-bin/data.py?callback=?";
$.getJSON(jsonp_url, function (data) {
$('#widget').html(data.html);
});
});
}
})(); 

Just add a data object in the ajax config. 只需在ajax配置中添加一个data对象。

$.ajax({
   type: "GET",
   data:{'exampleId' : exampleId},
   url: "https://example.com/widget/widget-updater.php"
});

Internally jQuery will create the query string in url from that when it is a GET and will become 当它是GET时,内部jQuery将从url中创建查询字符串,并将成为

"https://example.com/widget/widget-updater.php?exampleId=12345"

You can manually create the query string instead but using an object is cleaner and less work and less error prone 您可以改为手动创建查询字符串,但是使用对象会更整洁,工作量更少,更不易出错

If you are using a GET response you can pass data in a query parameter. 如果使用的是GET响应,则可以在查询参数中传递数据。

Your ajax URL is going to look like this: https://example.com/widget/widget-updater.php?exampleId=YOURDATA 您的Ajax URL将如下所示: https : //example.com/widget/widget-updater.php?exampleId=YOURDATA

$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php?exampleId=data"
});        

From PHP you can use $_GET['exampleId'] and is going to give you data 在PHP中,您可以使用$_GET['exampleId']并为您提供data

Since you are using the method of GET you can simply apend your URL IE: 由于您使用的是GET method ,因此您可以简单地在URL IE后面附加:

$.ajax({
type: "GET",
url: "https://example.com/widget/widget-updater.php?id=12345&foo=bar"
});

ALTERNATIVELY you can use the data attribute in .ajax() IE 您也可以在.ajax() IE中使用data属性

$.ajax({
type: "GET",
data:{id : '123456', foo: 'bar'},
url: "https://example.com/widget/widget-updater.php"
});

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

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