简体   繁体   English

Ajax 不会向 PHP 发送数据

[英]Ajax won't send data to PHP

SOLVED: The code posted here was correct, but some of the PHP code connecting to the database needed fixing, I have it working now.已解决:此处发布的代码是正确的,但是连接到数据库的某些 PHP 代码需要修复,我现在可以使用了。 I used Quentin 's suggestions in the comments to fix my problem.我在评论中使用了Quentin的建议来解决我的问题。

I'm trying to send a JSON of an array through ajax to a PHP file, then have the php file run.我正在尝试通过 ajax 将数组的 JSON 发送到 PHP 文件,然后运行 ​​php 文件。 This is my ajax:这是我的ajax:

    $('#submit').click(function() {
  $.ajax({
    method: "POST",
    url: "submit.php",
    data: {selectedImageArray: selectedImageArray}
  }).done(function( msg ) {
    alert( "Data Saved: " + selectedImageArray );

  });
});

In my PHP file I connect to a database then have this:在我的 PHP 文件中,我连接到一个数据库然后有这个:

$array = $_POST['selectedImageArray'];
    $sql = $dbh->prepare("INSERT INTO pending_trades (steam_id, trade_items, is_giving, is_receiving, has_started) VALUES (:steamid, :itemlist, '1', '0', '0')");
    $sql->bindParam(':steamid', $steamprofile['steamid']);
    $sql->bindParam(':itemlist', $array);

    $sql->execute();

I want this to send the "selectedImageArray" json to the php and run the msql query using it on the press of this button:我希望这将“selectedImageArray”json 发送到 php 并在按下此按钮时使用它运行 msql 查询:

<button id="submit" class="button1" >Submit<span></span></button>

When the button is clicked, I get the "data saved: array data " message, but the php code isn't run.单击按钮时,我收到“数据已保存: array data ”消息,但未运行 php 代码。 Why isn't this working?为什么这不起作用?

EDIT: I set data: {selectedImageArray: selectedImageArray} instead of data: selectedImageArray because I was told it makes it into JSON format, is that correct?编辑:我设置了data: {selectedImageArray: selectedImageArray}而不是data: selectedImageArray因为我被告知它变成了 JSON 格式,对吗?

you are adding in database an array not an string try to use:您正在数据库中添加一个数组而不是字符串尝试使用:

$sql->bindParam(':itemlist', json_encode($array));

and when you get the data just run:当您获取数据时,只需运行:

$array = json_decode($itemlist);

You can add content type JSON.您可以添加内容类型 JSON。 other wise it will consider as text.否则,它将被视为文本。

var data = JSON.stringify({selectedImageArray: selectedImageArray});
 $.ajax({
method: "POST",
dataType: 'json', // If your response is JSON
contentType: "application/json",
url: "submit.php",
data: data
}).done(function( msg ) {
alert( "Data Saved: " + selectedImageArray );
include ('submit.php');
});

First of all look at your browser debugger of what is really sent to the php file.首先看看你的浏览器调试器真正发送到 php 文件的内容。 Second thing: You will send JSON to it.第二件事:您将向它发送 JSON。 That means, that you have an POST form Array like this:这意味着,您有一个像这样的 POST 表单数组:

[{
   "key1": "value1",
   "key2": "value2"
}]

So, in conclusion this does not work:所以,总而言之,这不起作用:

$array = $_POST['selectedImageArray'];

You need to access eg $_POST['key1'] directly.您需要直接访问例如$_POST['key1']

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

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