I'm new to JavaScript, and working on a hobby project with a few developers. We have a simple page that is used to submit requests to a database.
I decided to try learning JQuery, and started implementing some AJAX functionality into this request page. It works fine in FireFox, IE and Safari, but for some odd reason, I can't get it to work in Chrome.
I've been debugging it for a couple of hours now, and I have no idea why it's not working. Here's the relevant part of the HTML form (post action removed due to JavaScript):
<form method="POST">
<input type="text" name="amount" value="0" size="7" />
<input type="submit" value="Fulfill!" /><br />
<input type="hidden" name="index" value="69" />
<input type="hidden" name="item" value="Iron Ore" />
</form>
And here's the PHP form that it posts to:
<?php
require_once("../classes/Database.php");
$database = new Database();
$amount = $database->sanitizeString($_POST['amount']);
$index = $database->sanitizeString($_POST['index']);
$username = $database->sanitizeString($_POST['username']);
$item = $database->sanitizeString($_POST['item']);
$database->connect();
$database->setRequest($amount, $index, $username, $item);
$database->setKarma($username, $amount, $item);
?>
And most importantly, here's my newbie JavaScript code that's simply in the HTML file:
<script language="JavaScript">
var amount = 0;
var index = 0;
var item = 0;
var username = "";
var data = "";
$(document).ready(function(){
$("form input[type=submit]").click(function(){
amount = $(this).siblings("input[name=amount]").val();
index = $(this).siblings("input[name=index]").val();
item = $(this).siblings("input[name=item]").val();
username = "<?=$_SESSION['username']; ?>";
//var stuff = $.post("pages/ajaxfulfill.php", {amount:amount,index:index,item:item, username:username}, function(data){alert(data)}, "html");
var stuff = $.ajax(
{
url: "pages/ajaxfulfill.php",
type: "POST", data:"amount=" + amount + "&index=" + index + "&item=" + item + "&username=" + username,
success: function(data)
{
alert("Success")
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert("error" + XMLHttpRequest.status);
alert("error" + XMLHttpRequest.responseText);
}
}
)
$(this).parents("td").text("Submitted");
});
});
</script>
At first, I was using the commented out $.post function, which works with the above mentioned browsers. I attempted to switch to the $.ajax function during my debug process, and found out that FF, IE, and Safari always return a "Success" alert, whereas Chrome is returning an Error with status 0 and a blank response.
Being a JavaScript newbie, I'm completely lost at why this would fail. If anyone could point me in the right direction, you'll have my profound respect and well wishes. Thanks.