javascript - Jquery Ajax 到 PHP 脚本不读取数据

标签 javascript php jquery arrays ajax

我有以下 $ajax 代码,该代码旨在发送一个带有填充有数据的 'title''name' 键的数组到script.php。 此脚本创建一个 2D 数组的新实例,并将其添加到以“name”为键的现有多维数组中。

但是script.php没有读取从ajax发送的$newTitle$newName

我确信这很简单 - 我对 PHP 和 jQuery 非常陌生。

javascript:

$.ajax({
    data: {title: 'this', name:'testthis'},
    url: 'script.php',
    method: 'POST',
    success: function(msg) {
        alert(msg);
    }
});

script.php:

//load the existing array into memory
$newLogins =json_decode(unserialize(file_get_contents("config.data")),true);
// get the data from ajax
$newTitle = $_POST["title"];
$newName = $_POST["name"];
//create a new instance of the example array
$addNew = array(
     'password' => 'pw',
     'title' => $newTitle,
     'emails' => array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15786c78747c796d556d6d3b767a78" rel="noreferrer noopener nofollow">[email protected]</a>'));
//add the new array to the existing array with 'name' as key
$newLogins[$newName] =$addNew;
//save it
file_put_contents("config.data", serialize(json_encode($newLogins)));
//send it back to ajax
echo json_encode($newLogins);

编辑

在下面添加了 HTML 文件:

<?php
$thisLogins = json_decode(unserialize(file_get_contents("config.data")),true);
?>
<head>
<script src="../libs/jquery.min.js"></script>
<script src="../libs/jquery-ui.min.js"></script>
</head>
<table id="tableId" border=1>
<thead>
    <tr>
    <th> Project Login</th>
    <th> Project Name</th>
    </tr>
    </thead>
<tbody>
<?php foreach( $thisLogins as $name => $infos ): ?>
    <tr>
        <td><?= $name ?></td>
        <td><?= $infos["title"]?></td>
        <td><button>Delete</button></td>
    </tr>

<?php endforeach; ?>
</tbody>
</table>

<script type="text/javascript"> 

$.ajax({
    data: {title: 'this', name:'testthis'},
    url: 'script.php',
    method: 'POST',
    success: function(msg) {
        alert(msg);
    }
});
</script>

我得到的是输出:

"":{"password":"caps","title":null,"emails":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f098959c9d958299b092839399de939f9d" rel="noreferrer noopener nofollow">[email protected]</a>"]}}

最佳答案

为了从 JSON 输出中读取数据,最好在 script.php 中将内容 header 设置为 JSON。它将使 $.ajax 函数的输出完美地易于阅读,例如...... header('Content-Type:application/json;');

同时删除 JavaScript 中 $.ajax 函数中不必要的逗号,只是为了谨慎起见,需要在 jquery 链接之后运行 JavaScript 代码。

您可以在 PHP 中使用条件来检查 JavaScript 中是否存在空输入。例如...

<?php
if(!empty($_POST["title"]) && !empty($_POST["name"])){
$newTitle = $_POST["title"];
$newName = $_POST["name"];
/****** process your code here ******/
//create a new instance of the example array
 $addNew = array(
  'password' => 'pw',
  'title' => $newTitle,
  'emails' => array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd1c5d1ddd5d0c4fcc4c492dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>'));
  //add the new array to the existing array with 'name' as key
  $newLogins[$newName] =$addNew;
 //save it
 file_put_contents("config.data", serialize(json_encode($newLogins)));
 //send it back to ajax
 $output['status'] = 'true';
 $output['error'] = 'none';
 $output['newLogins'] = $newLogins;
 }
else{
$output['status'] = 'false';
$output['error'] = 'Invalid Input';
}

//Setting up proper output
header('Content-Type:application/json;');
echo json_encode($output);
?>

关于javascript - Jquery Ajax 到 PHP 脚本不读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31951446/

相关文章:

带有 docker 卷的 PHP 命令不起作用

javascript - 机器人/垃圾邮件机器人是否具有选择单选按钮的能力?

javascript - 如何将两个 json 对象连接在一起?

jquery - ie7 中 jQuery append 问题

JavaScript 添加错误?

javascript - 连接API的WebApp与后端渲染的区别

Javascript float div 向下滚动时始终位于顶部

PHP session save_handler用户(mysql)不会保存

javascript - ajax 加载后 setInterval javascript 调用不会消失

javascript - XMLHttpRequest - 当 REST API 返回 JSON 对象而不是对象数组时,我遇到问题