omer Farooq omer Farooq - 7 months ago 9
Javascript Question

Desperatly In need of all in one ajax function for jquery

<input type="text" user" id="nick" />
<input type="text" user" id="message" />
<a href="#">Send</a>


Lets keep it simple. I have two input boxes and a send link. I want to send the nick and message to shoutbox.php, where i will insert these values in database and want to get all the results from the database and show it on the front end.

Now i have implemented the saving in database part but i cant get back the values from database to front end.

I am desperately in need of an jquery function in which i can just send the parameters and it will do all the job for me. I hope you guys might have such a function for yourselves.

Answer

Use the jQuery Ajax method to send data to the shoutbox.php:

$.ajax({
  type: "POST",
  url: "shoutbox.php",
  data: { nick: "val_of_nick", msg: "val_of_msg" },
  success: function(data) {
    alert('Loaded: ' + data);
  }
});

Now in your shoutbox.php:

//read the sended data
$nickname = $_POST['nick'];
$msg = $_POST['msg'];

//to send data back, just use echo/print
echo 'You sended nickname: ' . $nickname . ' and msg: "' . $msg . '"';

If you run this code, then your js alert will show the echo line from shoutbox.php.

Hope this helps!

More info about jQuery ajax: info