开发者

AJAX calling PHP-page, need help

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"
}); 



?>


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜