简体   繁体   English

从 ajax 调用在后台执行 php 页面

[英]Execute a php page in background from ajax call

I have this tab.php page that is basically the index of the site.我有这个tab.php页面,它基本上是网站的索引。 It contains all JavaScript and HTML code.它包含所有 JavaScript 和 HTML 代码。

I'm trying to send some array from this page to a PHP page and than do an update query, uploading the array on my phpmyadmin.我正在尝试从该页面发送一些数组到 PHP 页面,然后进行更新查询,将数组上传到我的 phpmyadmin 上。

I used this code in tab.php but it doesn't work.我在tab.php中使用了此代码,但它不起作用。

 var send_array = {
"red": redtruppe,
"blue": bluetruppe,
"orange": orangetruppe,
"purple": purpletruppe,
"black": blacktruppe,
"green": greentruppe
};

$.ajax({ 
   type: "POST", 
   url: "send.php", 
   data: { TruppeJson : send_array }, 
   success: function() { 
          alert("Success"); 
    } 
}); 

the send.php page is, for now, something like this: send.php 页面现在是这样的:

<?php
$myTruppe = json_decode($_POST['TruppeJson']);
echo '<script>console.log('.$myTruppe.')</script>';
?>

The success alert seems to work, but the send.php apparently not, any suggestions?成功警报似乎有效,但send.php显然没有,有什么建议吗?

Try to use that for the data:尝试将其用于数据:

 JSON.stringify(send_array)

and define your dataType as JSON并将您的数据类型定义为 JSON

dataType: "json"

Would look like this:看起来像这样:

$.ajax({ 
   type: "POST", 
   url: "send.php", 
   data: JSON.stringify(send_array),
   dataType: "json",
   success: function() { 
          alert("Success"); 
    } 
}); 

As you stated, success is getting called.正如你所说,成功正在被召唤。 So your data is not just been sent to the backend.因此,您的数据不仅仅是被发送到后端。

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

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