使用XHR调用将JavaScript值传递给PHP文件(Passing JavaScript values to PHP file using XHR call)

编程入门 行业动态 更新时间:2024-10-25 10:33:54
使用XHR调用将JavaScript值传递给PHP文件(Passing JavaScript values to PHP file using XHR call)

我在服务器上有一个PHP文件,需要从网页上的客户端使用动态值来查询我的数据库。 用户将单击链接(以及将用户定向到新选项卡)获取链接所连接的文档位置字符串。 我目前正在尝试使用jQuery / AJAX和XHR调用将这些值传递给服务器端代码,但是当我运行它时,似乎PHP没有正确执行。 这是JavaScript代码:

// Clicking the link $('a.docs').on('click', function(){ // Getting the values needed for the query from existing table var theData = subtable.row($(this).parents('tr')).data(); // The document string var thedoc = theData[7]; // Pass the document value to the PHP file on the server $.post('https://example.com/TEST/dashboard/change.php', { document: thedoc }); // Make XHR call to execute the PHP file on the server var xhr = new XMLHttpRequest(); xhr.open("GET", "https://example.com/TEST/dashboard/change.php", true); xhr.send(); });

如您所见,我尝试在进行GET XHR调用之前将thedoc的值发送到服务器上的文件。 以下是服务器上的PHP代码:

<?php // The line that grabs the document variable being passed $document = $_POST['document']; // ... MySQL query stuff using $document as parameter... ?>

当我测试它并单击网页上的链接时,JavaScript正确地抓取了文档链接变量,它只是传递没有工作。 关于我做错的任何想法?

I have a PHP file on a server that needs to take dynamic values from client-side use on a web page in order to query my database. The user will click a link which (as well as directing the user to a new tab) takes the document location string that the link is connected to. I am currently trying to pass these values to the server side code using jQuery/AJAX and an XHR call, but when I run it, it seems that the PHP does not execute properly. Here is the JavaScript code:

// Clicking the link $('a.docs').on('click', function(){ // Getting the values needed for the query from existing table var theData = subtable.row($(this).parents('tr')).data(); // The document string var thedoc = theData[7]; // Pass the document value to the PHP file on the server $.post('https://example.com/TEST/dashboard/change.php', { document: thedoc }); // Make XHR call to execute the PHP file on the server var xhr = new XMLHttpRequest(); xhr.open("GET", "https://example.com/TEST/dashboard/change.php", true); xhr.send(); });

As you can see, I try to send the value of thedoc to the file on the server before making the GET XHR call. Here is the what PHP code on the server does:

<?php // The line that grabs the document variable being passed $document = $_POST['document']; // ... MySQL query stuff using $document as parameter... ?>

When I test this and click the link on the webpage, the JavaScript correctly grabs the document link variable, it's just the passing that has not been working. Any ideas on what I am doing incorrectly?

最满意答案

我无法评论:(

我同意jorgonor1。

您正在向服务器执行2个单独的请求。

首先。 您正在使用JQUERY在$ .POST(...)部分中将数据发送到服务器。 从示例代码中,忽略结果。

然后,使用xhr请求执行通用页面的干净请求。

如果您使用$ .POST(),并希望对服务器的结果执行某些操作,请尝试:

$.post('https://example.com/TEST/dashboard/change.php', {document: thedoc}, function(result){ //do something with the data returned form the server, server response stored in "result" variable });

请参阅W3Schools上的示例

I cannot comment :(

I agree with jorgonor1.

You are performing 2 separate requests to your server.

The first. You are sending data to the server with JQUERY in the section $.POST(...). And from the sample code, ignore the results.

Then secondly, you perform a clean request of the generic page with the xhr request.

If you use $.POST(), and want to perform something with the result from the server try:

$.post('https://example.com/TEST/dashboard/change.php', {document: thedoc}, function(result){ //do something with the data returned form the server, server response stored in "result" variable });

See the example on W3Schools

更多推荐

本文发布于:2023-08-07 22:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   PHP   JavaScript   XHR   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!