Jump to content

ajax call...updating the dbase


jimfog
 Share

Recommended Posts

I am trying to update the database using an ajax call but I cannot-the call itself though, is made...here is the js(jquery API) code:

$(function() {    $('.error').hide();    $(".buttonaj").click(function() {		 $('.error').hide(); 	 	    var name = $("input#name").val(); 	    if (name == "") { 	  $("label#name_error").show(); 	  $("input#name").focus(); 	  return false;     }	 $.ajax({	  type: "POST",	  url: "testajax.php",	  data: name,	  success: function() {	    $('#personal').html("<div id='message'></div>");	    $('#message').html("<h2>Personal Data Updated!</h2>")	    .append("<p>We will be in touch soon.</p>")	    .hide()	    .fadeIn(1500);		  }	 });    return false;       });  });

Here is the PHP code at testajax.php

  $conn = db_connect();		 $name=$_POST['name'];		  $result = $conn->query('update busines_users		    set name="'.$name.'"		    where crID="12"');		 		    if(!$result)			  { throw new Exception('Aδυναμία σύνδεσης με την βάση.');			  }

db_connect is the function that makes the connection to the database.I was expecting that the var name in the js code(which contains the user input) would pass to the name variable in the php file but it does not.What might be the problem here?

Link to comment
Share on other sites

you need to make data an object {"name":name}, or a string like "name=" + name. all you are sending it right now is a string of the name, there is no key for PHP to look up in POST.

Link to comment
Share on other sites

Yes you were right...but I need to make one more question.I know that data can be sent to the server with JSON...but in the example above(which is based on instructions in the jquery website)...nowhere is evident that data is being sent with JSON. How I could send the string above(which is user input) with JSON?

Link to comment
Share on other sites

i already have you that example

you need to make data an object {"name":name}
that is the simplest form of JSON. (JavaScript Object Notation) Edited by thescientist
Link to comment
Share on other sites

i already have you that example that is the simplest form of JSON. (JavaScript Object Notation)
So...that is json.You just use the data property of the jquery ajax function and send JSON.Thanks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...