I am searching this and other sites for hours now, so I'm getting pretty desperate. No code from many questions with the same topic here works.
我现在正在搜索这个和其他网站几个小时,所以我非常绝望。这里没有来自同一主题的许多问题的代码。
I need to insert data into the database and display a message after it is done. Also, I am using AJAX with jQuery so it would be asynchronous. It works just fine, the data gets inserted, but no response message shows.
我需要将数据插入数据库并在完成后显示消息。此外,我正在使用AJAX与jQuery,所以它将是异步的。它工作正常,数据插入,但没有响应消息显示。
I am a beginner at PHP and can't understend why this won't work. Relevant code below.
我是PHP的初学者,不能忽视为什么这不起作用。相关代码如下。
PHP function call:
PHP函数调用:
if(isset($_POST["function"]) && !empty($_POST["function"]) && $_POST["function"] == "cl-add") {
$dbb->addMember("MyUsername", $_POST["name"]);
//$dbb is a DataBaseBroker instance
}
PHP function from the Broker:
来自Broker的PHP函数:
function addMember($username, $ime) {
$query = "INSERT INTO clan";
$query.=" (username, ime) ";
$query.="VALUES ('".$username."','".$ime."');";
$result = $this->mysqli->query($query);
if ($result) {
echo("You added a member: ".$ime);
} else {
$response = "An error occured. Please try again.";
$response .= "<br>";
$response .= "Error: ".mysqli_error($connection);
echo $response;
}
}
JQuery function declarations:
JQuery函数声明:
var addMember = function(name, responseFn) {
if (name === "") {
alert("Please enter a name");
return;
}
$.ajax({
type : 'POST',
url: '../includes/layout/cl.php',
dataType : 'json',
data : {
'name' : name,
'function' : 'cl-add'
},
success : function(data) {
responseFn(data); //not working, should alert
}
});
}
var responseCallback = function(data) {
alert(data);
}
And inside $(document).ready():
在$(document).ready()里面:
$(document).on('click', '#cl-add', function(evt) {
var name = $("#cl_frm input").val();
addMember(name, responseCallback);
});
2 个解决方案
#1
2
On your code:
在你的代码上:
dataType : 'json',
The Ajax request is waiting for a JSON response but you are giving a text response.
Ajax请求正在等待JSON响应,但您正在给出文本响应。
You should change the dataType: to text or html depending on your needs.
您应该根据需要将dataType:更改为text或html。
dataType : 'html',
or
dataType : 'text',
PHP changes:
<?php
function addMember($username, $ime)
{
$query = "INSERT INTO clan";
$query .= " (username, ime) ";
$query .= "VALUES ('" . $username . "','" . $ime . "');";
$result = $this->mysqli->query($query);
$response = null;
if ($result) {
$response = "You added a member: " . $ime;
} else {
$response = "An error occured. Please try again.";
$response .= "<br>";
$response .= "Error: " . mysqli_error($connection);
}
echo $response;
exit;
}
#2
0
Change dataType parameter to 'text'. Also you can alert an object in JavaScript, actually you are not trying to alert an object but i just wanted to mention it.
将dataType参数更改为“text”。你也可以用JavaScript警告一个对象,实际上你并没有试图提醒一个对象,但我只是想提一下它。
#1
2
On your code:
在你的代码上:
dataType : 'json',
The Ajax request is waiting for a JSON response but you are giving a text response.
Ajax请求正在等待JSON响应,但您正在给出文本响应。
You should change the dataType: to text or html depending on your needs.
您应该根据需要将dataType:更改为text或html。
dataType : 'html',
or
dataType : 'text',
PHP changes:
<?php
function addMember($username, $ime)
{
$query = "INSERT INTO clan";
$query .= " (username, ime) ";
$query .= "VALUES ('" . $username . "','" . $ime . "');";
$result = $this->mysqli->query($query);
$response = null;
if ($result) {
$response = "You added a member: " . $ime;
} else {
$response = "An error occured. Please try again.";
$response .= "<br>";
$response .= "Error: " . mysqli_error($connection);
}
echo $response;
exit;
}
#2
0
Change dataType parameter to 'text'. Also you can alert an object in JavaScript, actually you are not trying to alert an object but i just wanted to mention it.
将dataType参数更改为“text”。你也可以用JavaScript警告一个对象,实际上你并没有试图提醒一个对象,但我只是想提一下它。