I am trying to do something very simple: Pass 2 text variables to a php script and insert them into a MySQL db. For some reason however I can’t get the variables to pass (so I just get empty records in my DB).
JavaScript
x
function ajaxCall(){
$.ajax({
type: "GET",
url: "http://www.*.be/bubblingAjax.php",
cache: false,
data: "colour="+colour+"&size="+size,
dataType: "html",
success: onSuccess
});
return false;
};
And the PHP:
JavaScript
<?php
try
{
$connection = mysql_connect("#");
mysql_select_db("#");
$colour = mysql_real_escape_string($_GET['colour']);
$size = mysql_real_escape_string($_GET['size']);
mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
mysql_close($connection);
echo "SUCCESS";
echo $colour;
echo $size;
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
Anyone willing to take a quick look at it and point out my -probably obvious- mistake? It’s been driving me nuts for over a day.
Advertisement
Answer
This has to work:
JavaScript
<script type="text/javascript">
$(document).ready(function() {
//you can wrap the code into an event, e.g click()
var colour=
var size=
$.post("http://www.website.com/bubblingajax.php", { colour: colour, size: size },
function(data) {
alert("Respond: " + data);
});
});
</script>
and the PHP (only changed get to post)
JavaScript
<?php
try
{
$connection = mysql_connect("#");
mysql_select_db("#");
$colour = mysql_real_escape_string($_POST['colour']);
$size = mysql_real_escape_string($_POST['size']);
mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
mysql_close($connection);
echo "SUCCESS";
echo $colour;
echo $size;
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
Also to debug, I would suggest using firebug or chrome’s build in inspect tools.