我显然有一个错误的Ajax调用

时间:2014-06-10 09:33:10

标签: javascript php jquery sql ajax

我有一个项目,用户可以在线拥有自己的VirtualDiary。他/她注册登录和所有这些,并带到日期和文本区域的输入页面。我们的想法是,他们可以点击标题为NextPagePreviousPage的页面顶部的两个按钮。这些将调用jquery ajax函数,该函数将反过来更改EntryID + 1EntryID -1的值。这应该会改变页面上几乎所有内容的价值。但即使ajax调用记录成功,也没有任何反应。我对ajax很新,所以我可能做了一件非常愚蠢的事。提前致谢

PHP

<?php 
 session_start();

 //error_reporting(E_ERROR|E_WARNING);
 mysql_connect("localhost","root","") or die ("cannot");
 mysql_select_db("virtualdiary") or die ("db");

$JoinDateQuery = mysql_query("SELECT * FROM users WHERE UID = '".$_SESSION['UID']."' ");

if($JoinDateQuery === FALSE) {
die(mysql_error()); // TODO: better error handling
}

while($row = mysql_fetch_array($JoinDateQuery))
{
$JoinDate =  $row[4];
}


 $TodayDate = date("Y/m/d");
 $today = strtotime($TodayDate);
 $joinTime = strtotime($JoinDate);
 $datediff = $today - $joinTime;
 $_SESSION["EntryID"] = floor($datediff/(60*60*24));
 $EntryID = $_SESSION["EntryID"];
 $_SESSION['EntryDate'] = date('Y-m-d', strtotime($JoinDate. ' + '.$EntryID .'days'));
 $EntryDate = $_SESSION['EntryDate'];


$id = $_SESSION["UID"] ;








if (isset($_POST["entry"])){
$entry = $_POST["entry"];

$deletion = "DELETE FROM entries WHERE UserID = '".$_SESSION['UID']."' and EntryID = '".$EntryID."' ";
mysql_query($deletion);
$submission = "INSERT INTO `virtualdiary`.`entries` (`Entry`, `UserID`,`EntryID`) VALUES ('". $entry . "',                    '".$_SESSION['UID']."', '".$EntryID."')";
mysql_query($submission);


}
$ThePost = 'SELECT * FROM entries WHERE UserID = "'.   $_SESSION['UID'] .'" and EntryID = "'.$EntryID.'"';
$result = mysql_query($ThePost);

if($result === FALSE) {
die(mysql_error());
}




 ?>

<html>

 <head>
 <link type="text/css" rel="stylesheet" href="Entry.css"/>
 <title>Home</title>
 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
 <script>
  $(document).ready(function(){
 $('#NextDay').click(function(){
 $.ajax({
 type: "GET",
 url: "Home.php",
 data: "$EntryID = $EntryID + 1",
success: console.log("success")     

}); //ajax call
 });//on click next day
 });//document ready</script>

</head>
<body>
<section>
<button id="previousDay" class="day">Previous Day</button>
<button class = "day" id = "date">Joined: <?php echo $JoinDate; ?></br>
                 Entry Number: <?php echo $EntryID + 1; ?></br>
                 EntryDate: <?php echo $EntryDate ; ?>
                 </button>
<button class="day" id = "NextDay">Next Day</button>
<h1>Entry:  </h1>
<form method="post" action="Home.php">
<textarea name="entry" rows="24" cols="80">
<?php 
 while($row = mysql_fetch_array($result))
{
echo $row['Entry'];
}
?>
</textarea>
 </br>
 </br>
 <input name="submit" type="submit"/>    
 </form>

<a href="Calender.php"> <button id="calender" class = "day"><h1>Calender</h1></button></a>

 <button id="LogOut"><a href="LogOut.php">Log Out</a></button>
 </section>
 </body>

 </html>

顺便说一句EntryID返回4的结果(或者当天用户的默认值),所以很明显这个问题与ajax的Data:部分有关或者我我没有在正确的语境中使用ajax来实现我想要的目标。

编辑:我刚刚意识到$EntryID = $EntryID + 1必须在某处定义,但我不能在某处将其删除,因为这会改变我认为的第一个条目id实例。

2 个答案:

答案 0 :(得分:0)

来自http://api.jquery.com/jquery.ajax/

数据 键入:PlainObject或String 要发送到服务器的数据。如果不是字符串,它将转换为查询字符串。它附加到GET请求的URL。请参阅processData选项以防止此自动处理。对象必须是键/值对。如果value是一个数组,jQuery会根据传统设置的值使用相同的键序列化多个值(如下所述)。

你正试图将“$ EntryID = EntryID + 1”改为“home.php”

“home.php $ EntryID = EntryID + 1”似乎不是正确的网址?

如果您想了解有关请求状态的更多信息,则应尝试使用错误回调集。

答案 1 :(得分:0)

首先,我认为你应该制作一个AJAX&#34; POST&#34;要求,而不是&#34; GET&#34;请求。 (只是行业标准,因为您将值发送到服务器)。

其次,我不确定,你在哪里获得了为该GET请求创建数据值的语法(真的很奇怪)!在我编辑代码的ajax调用中,现在应该是正确的创造和创造的方式发送可反序列化的数据。

修改 忘记提及,可以使用$_REQUEST['xyx']$_POST['xyz']检索从AJAX发送的值。如果您使用的是get,则可以使用$_GET['xyz']

<强> UNTESTED     

//error_reporting(E_ERROR|E_WARNING);
mysql_connect("localhost", "root", "") or die ("cannot");
mysql_select_db("virtualdiary") or die ("db");

$JoinDateQuery = mysql_query("SELECT * FROM users WHERE UID = '" . $_SESSION['UID'] . "' ");

if ($JoinDateQuery === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

while ($row = mysql_fetch_array($JoinDateQuery)) {
    $JoinDate = $row[4];
}


$TodayDate = date("Y/m/d");
$today = strtotime($TodayDate);
$joinTime = strtotime($JoinDate);
$datediff = $today - $joinTime;
$_SESSION["EntryID"] = floor($datediff / (60 * 60 * 24));
$EntryID = $_POST["EntryID"];
$_SESSION['EntryDate'] = date('Y-m-d', strtotime($JoinDate . ' + ' . $EntryID . 'days'));
$EntryDate = $_SESSION['EntryDate'];


$id = $_SESSION["UID"];


if (isset($_POST["entry"])) {
    $entry = $_POST["entry"];

    $deletion = "DELETE FROM entries WHERE UserID = '" . $_SESSION['UID'] . "' and EntryID = '" . $EntryID . "' ";
    mysql_query($deletion);
    $submission = "INSERT INTO `virtualdiary`.`entries` (`Entry`, `UserID`,`EntryID`) VALUES ('" . $entry . "',                    '" . $_SESSION['UID'] . "', '" . $EntryID . "')";
    mysql_query($submission);


}
$ThePost = 'SELECT * FROM entries WHERE UserID = "' . $_SESSION['UID'] . '" and EntryID = "' . $EntryID . '"';
$result = mysql_query($ThePost);

if ($result === FALSE) {
    die(mysql_error());
}




?>

<html>

<head>
    <link type="text/css" rel="stylesheet" href="Entry.css"/>
    <title>Home</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#NextDay').click(function () {
                $.ajax({
                    type: "POST",
                    url: "Home.php",
                    data: {
                        EntryID: '1' //not sure (this is quite conditional I guess)
                    },
                    success: console.log("success")

                }); //ajax call
            });//on click next day
        });//document ready</script>

</head>
<body>
<section>
    <button id="previousDay" class="day">Previous Day</button>
    <button class="day" id="date">Joined: <?php echo $JoinDate; ?></br>
        Entry Number: <?php echo $EntryID + 1; ?></br>
        EntryDate: <?php echo $EntryDate; ?>
    </button>
    <button class="day" id="NextDay">Next Day</button>
    <h1>Entry: </h1>

    <form method="post" action="Home.php">
        <textarea name="entry" rows="24" cols="80">
            <?php
            while ($row = mysql_fetch_array($result)) {
                echo $row['Entry'];
            }
            ?>
        </textarea>
        </br>
        </br>
        <input name="submit" type="submit"/>
    </form>

    <a href="Calender.php">
        <button id="calender" class="day"><h1>Calender</h1></button>
    </a>

    <button id="LogOut"><a href="LogOut.php">Log Out</a></button>
</section>
</body>

</html>