0

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.

Edited by adamholwerda: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by hielo
0

when you click the submit button, a $.post() is emitted. This in turn sends/POSTs contentvar and inputvar to crud.php which ends up updating the db. After the php script is done executing all php variables are "obliterated" - they cease to exist on the SERVER. THEN lines 15-22 execute on the BROWSER. Within that callback function you have $("#pull").load("crud.php"); which essentially boils down to calling crud.php AGAIN, but this time WITHOUT posting anything. Since you are NOT providing parameters, your script is attempting to execute the following: UPDATE UserTemp SET ='' WHERE EJUID=6 clearly, an sql syntax error.

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.

I don't see you echoing anything in crud.php, but assuming that you WERE doing so before posting your problem here, what you needed to do was to change: $("#pull").load("crud.php"); to: $("#pull").html( response ); Whatever you are echoing in the php file will be available as your first param in your callback function.

<?php
require_once('../Connections/ejournals.php');
if( isset($_POST) && !empty($_POST) )
{
   mysql_select_db($database_ejournals, $ejournals);

  $inputvar = mysql_real_escape_string($_POST['inputvar']);
  $contentvar = mysql_real_escape_string($_POST['contentvar']);
  $uservar = 6;


 $updateSQL = sprintf("UPDATE UserTemp SET $inputvar='$contentvar' WHERE EJUID=$uservar");

  $Result1 = mysql_query($updateSQL, $ejournals) or die(mysql_error());
  echo "Updated " . $inputvar;
}
exit;
?>
...
                                     function(response)
 
					{
					$("#pull").fadeIn('fast');
					/* $("#pull").load("crud.php");
					   $("#display").load("pull.php"); */
$('#pull').html(response);
					}
...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.