Hi, new to AJAX, trying to figure out how to do a very specific thing:
I want to have a textarea update a database column and then pull down the info from that column and display it, without refreshing.
I'm using JQuery and AJAX and PHP and SQL to do this. So far I've been able to update the database row using my $.post() call. The strange thing is, my variables don't seem to be passing to the page - when I try to echo them or look at them they don't show up.
Here is my simple index page and call (page names aren't indicative of what I'm trying to do, just placeholders):
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js" type="text/javascript"></script>
<script type = "text/javascript">
$(document).ready(function() {
$("#updatefield").click(function(){
$.post(
'crud.php',
{
contentvar:$("#contentvar").val(),
inputvar:$("#inputvar").val(),
},
function(response)
{
$("#pull").fadeIn('fast');
$("#pull").load("crud.php");
$("#display").load("pull.php");
}
)
return false;
});
});
</script>
</head>
<body>
<form>
<select id ="inputvar">
<option value="page1a">page1a</option>
<option value="page1b">page1b</option>
</select>
<textarea id = "contentvar">
</textarea>
<input type="submit" value="Update" id="updatefield" />
</form>
<div id = "display"></div>
<div id="pull"></div>
</body>
</html>
And here is the processing code in my 'crud.php' file.
<?php
require_once('../Connections/ejournals.php');
$inputvar = $_POST['inputvar'];
$contentvar = $_POST['contentvar'];
$uservar = 6;
$updateSQL = sprintf("UPDATE UserTemp SET $inputvar='$contentvar' WHERE EJUID=$uservar");
mysql_select_db($database_ejournals, $ejournals);
$Result1 = mysql_query($updateSQL, $ejournals) or die(mysql_error());
?>
Again, the code IS updating the database row. I'm just getting a SQL Syntax error. The error I get is "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='' WHERE EJUID=6' at line 1"
Notice the variable $contentvar shows as '' in the error, but the row updates correctly. Why? How?
The only thing I can think is maybe I'm sending the $.post() variables to 'crud.php' the wrong way.
Thanks in advance for helpful advice.