[英]Error: Undefined Index while calling Ajax
The problem that I'm facing is that I'm getting an Undefined Index variable while calling Ajax. 我面临的问题是我在调用Ajax时得到了一个Undefined Index变量。 I need to post "json" data to the "update.php" page on click of submit button. 单击提交按钮后,我需要将“ json”数据发布到“ update.php”页面。 Basically, I need to capture the values in textbox and send it to the database. 基本上,我需要捕获文本框中的值并将其发送到数据库。
so, I have created a form on the submit button, for which the code is below: 因此,我在“提交”按钮上创建了一个表单,其代码如下:
<form action="update.php" method = "post" class="form-inline">
<input type="submit" class="btn btn-info" id = "saveEdits" disabled = "disabled" onclick = "updateVal()" name="saveEdits" value="Update"/>
/form>
This submit button Calls for an UpdateVal function that captures the value on the text-boxes shown on the page and using AJAX send it to the another php page. 这个提交按钮需要一个UpdateVal函数,该函数捕获页面上显示的文本框中的值,并使用AJAX将其发送到另一个php页面。
updateVal function is as below: updateVal函数如下:
function updateVal() {
var node_list = document.getElementsByTagName('input');
var c = 0;
var fieldName = [];
var fieldText = []
var ID = [];
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text') {
fieldName[c] = node.name;
fieldText[c] = node.value;
ID[c] = node.id;
c++;
}
}
var postData = {
fieldName: fieldName,
fieldText: fieldText,
ID: ID
};
$.ajax({
type: "post",
url: "update.php",
dataType: "json",
data: {'postData' : JSON.stringify(postData)},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
});
The run time data ie value in textboxes is being captured and can be shown at console, however, when I'm posting this data on update.php, where I would be capturing the json and will update the database, I'm getting the error: 运行时数据(即文本框中的值)已被捕获并可以在控制台上显示,但是,当我将这些数据发布到update.php上时(我将在其中捕获json并更新数据库),错误:
Notice: Undefined index: in update.php on line 11 注意:未定义的索引:在第11行的update.php中
Below is my update.php 以下是我的update.php
<?php
$json = $_POST["postData"];
$result = json_decode($json);
var_dump($result);?>
Remove your action
attribute from your form, since you send your data per Javascript/AJAX you don't need it. 从表单中删除action
属性,因为您不需要按Javascript / AJAX发送数据。
What you do now is: you send the data twice, once per updateVal()
and once per default submit handling of the form. 现在要做的是:发送两次数据,每个updateVal()
一次,每个默认提交表单处理一次。
Since the form-submitted data doesn't contain a input named postData
, you are getting your undefined index error. 由于表单提交的数据不包含名为postData
的输入,因此您将得到未定义的索引错误。
Edit: You stated you are not comfortable with the whole AJAX topic. 编辑:您说您不满意整个AJAX主题。 Here is a nice simple answer covering the basics. 这是一个涵盖基础知识的简单答案。
Edit2: 编辑2:
to listen for the response of your server add a callback to your ajax-request: 侦听服务器的响应,向您的ajax请求添加回调:
$.ajax({
type: "post",
url: "update.php",
dataType: "json",
data: {'postData' : postData},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
}).done(function (response, textStatus, jqXHR){
// Show an alert on response
alert(response);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.