AJAX调用PHP页面,需要帮助

发布于 2024-11-15 23:05:23 字数 1424 浏览 0 评论 0原文

当我使用 AJAX 将一些信息发送到 PHP 页面并使用一些连接到数据库并存储一段文本的代码时,我不确定我是否做得正确。 AJAX 调用的 PHP 页面,与普通 PHP 页面相比是否必须有所不同?它不起作用,我收到 404 Not Found 消息?

这是 PHP 页面:

<?php
session_start();
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

$replyArticleId = isset($_POST['replyArticleId']) ? $_POST['replyArticleId'] : '';
$replyText = isset($_POST['replyText']) ? $_POST['replyText'] : '';

$replySign = $_SESSION['accountId'];

date_default_timezone_set("Europe/Stockholm");
$date = new DateTime();
$replyDate = $date->format('Y-m-d H:i:s');

$tableUser = DB_PREFIX . WS_DB_USER;
$tablePost  = DB_PREFIX . WS_DB_POST;
$tableComment = DB_PREFIX . WS_DB_COMMENT;
$tableArticle = DB_PREFIX . WS_DB_ARTICLE;
$tableReply = DB_PREFIX . WS_DB_REPLY;

// Add new comment

$query1 = "INSERT INTO {$tableReply} (replyArticleId, replyText,replyUserId,        
replyDate) VALUES    ('{$replyArticleId}','{$replyText}','{$replySign}',  '{$replyDate}');";
$query2 = "UPDATE {$tableArticle} SET articleDateUpdated = NOW() WHERE articleId =     {$replyArticleId};";
$res = $mysqli->query($query1) or die($mysqli->error);
$res = $mysqli->query($query2) or die($mysqli->error);

$mysqli->close();

这是我用来调用和发送一些测试内容的 AJAX 代码:

$.ajax({
url: "PAddReplyProcessAJAX.php?replyArticleId=1", 
type: "POST",
dataType: "text",
data: "replyText=" + "test"
}); 



?>

I'm not sure if I have done right when I use AJAX to send some info to a PHP-page with some code that connects to a database and stores a piece of text. A PHP-page that AJAX call, must that PHP-page be different compared to a common PHP-page? It's not working, I get a 404 Not Found message?

Here is the PHP-page:

<?php
session_start();
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

$replyArticleId = isset($_POST['replyArticleId']) ? $_POST['replyArticleId'] : '';
$replyText = isset($_POST['replyText']) ? $_POST['replyText'] : '';

$replySign = $_SESSION['accountId'];

date_default_timezone_set("Europe/Stockholm");
$date = new DateTime();
$replyDate = $date->format('Y-m-d H:i:s');

$tableUser = DB_PREFIX . WS_DB_USER;
$tablePost  = DB_PREFIX . WS_DB_POST;
$tableComment = DB_PREFIX . WS_DB_COMMENT;
$tableArticle = DB_PREFIX . WS_DB_ARTICLE;
$tableReply = DB_PREFIX . WS_DB_REPLY;

// Add new comment

$query1 = "INSERT INTO {$tableReply} (replyArticleId, replyText,replyUserId,        
replyDate) VALUES    ('{$replyArticleId}','{$replyText}','{$replySign}',  '{$replyDate}');";
$query2 = "UPDATE {$tableArticle} SET articleDateUpdated = NOW() WHERE articleId =     {$replyArticleId};";
$res = $mysqli->query($query1) or die($mysqli->error);
$res = $mysqli->query($query2) or die($mysqli->error);

$mysqli->close();

This is the AJAX code that I use to call and send some test content:

$.ajax({
url: "PAddReplyProcessAJAX.php?replyArticleId=1", 
type: "POST",
dataType: "text",
data: "replyText=" + "test"
}); 



?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-11-22 23:05:23

看来 JavaScript 没有在您认为的位置调用 PHP 脚本。

打开 FireBug 控制台(或 Chrome 开发人员工具)并查看所查询的确切 URL。

尝试为脚本的 URL 设置绝对路径,看看是否有帮助。根据您的 URL 结构,相对路径可能会有所不同。

作为旁注,您的 PHP 脚本包含 SQL 注入漏洞。确保正确转义值或使用绑定参数。无论出于何种原因,您可能不关心应用程序的安全性,但只要有人输入错误的字符(例如单引号),应用程序仍然会崩溃。

It looks like JavaScript is not calling the PHP script where you think it is.

Open up the FireBug console (or Chrome developer tools) and see the exact URL that gets queried.

Try setting an absolute path for your script's URL to see if that helps. Depending on your URL structure, the relative paths may vary.

As side notes, your PHP script contains SQL injection vulnerabilities. Make sure you properly escape values or use bound parameters. You might not care about security in your application for whatever reason, but it will still crash whenever someone types in a bad character, like a single quote.

浅浅 2024-11-22 23:05:23

你应该在这一行(php页面)上使用$_GET,

$replyArticleId = isset($_POST['replyArticleId']) ? $_POST['replyArticleId'] : '';

$replyArticleId = isset($_GET['replyArticleId']) ? $_GET['replyArticleId'] : '';

你的ajax类型也应该是“GET”

如果你想传递POST数据,你需要传递一个有效的变量数据,

。检查此链接 http://api.jquery.com/jQuery.post/

您还可以必须确保目标文件 php 存在。检查文件名和文件位置。

You supposed to use $_GET on this line (php page)

$replyArticleId = isset($_POST['replyArticleId']) ? $_POST['replyArticleId'] : '';

$replyArticleId = isset($_GET['replyArticleId']) ? $_GET['replyArticleId'] : '';

you're ajax type also should be 'GET'

if you want to pass a POST data you need to pass a valid variable data.

check on this link http://api.jquery.com/jQuery.post/

you also have to make sure that the target file php is exists. Check the file name and file location.

魔法唧唧 2024-11-22 23:05:23

这真的是您想要使用 PAddReplyProcessAJAX.php 的 URL 吗?它与您调用 ajax 的页面位于同一目录中吗?

Is that really the URL you want to use PAddReplyProcessAJAX.php? Is it in the same directory as the page you're calling the ajax from?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文