samedi 31 mai 2014

Ajax, add to database and update div content


Vote count:

0




Okay, so I am trying to use ajax. I've tried several ways of doing this but nothing is working for me. I believe the main problem I have is that ajax won't add to my database, the rest is managable for me.


Here is the relevant ajax-code:



function posts(){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("threads").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", sessionStorage.page, true);
xmlhttp.send();


}


function update_post(){ var message = $("#message").val();



var dataString = "content=" + message;

$.ajax({
url: 'post_process.php',
async: true,
data: dataString ,
type: 'post',
success: function() {
posts();
}
});


}


Here is the post_process.php:



<?php
require_once "include/bootstrap.php";

if (isset($_POST['subject'])){
$subject = $con->real_escape_string($_POST["subject"]);
$Qnum = rand();
die ("if 1");
} else {
$subject = $_SESSION['subject'];
$Qnum = $_SESSION['qnum'];
die ("else 1");
}
$message = $con->real_escape_string($_POST["message"]);

if (validateEmpty($message) && validateEmpty($subject)){
send();
die ("if 2");
} else {
die ("Error!");
}

function send(){
global $con, $message, $subject, $Qnum;
die ("i send()");

$con->create_post($_SESSION['username'], $_SESSION['category'], $subject, $message, $Qnum);
}

//header("Location: index.php");


?>


And lastly, here is the html-form:



<div id="post_div">
<form name="threadForm" method="POST" action="" onsubmit="return validate_post();">
<label for="message">Meddelande</label><br>
<textarea id="message" name="message" id="message" maxlength="500">

</textarea><br>
<input type="submit" onclick="update_post()" value="Skicka!" name="post_btn" id="post_btn"><br>
</form>


I've tried putting alert:s in the js-code and die:s in the php-code to see how far it gets and I've concluded that it doesn't call my $.ajax. I've tried using $.post instead with no luck.



asked 47 secs ago






Aucun commentaire:

Enregistrer un commentaire