Danny
Danny

Reputation: 61

Pass data from jQuery to php file

Hello I have this code

$('.preview a').click(function() {
  $('.presentation > .images > .margin').load('images.php');

  return false; 
});

Is there possibility to pass data from jquery into that images.php file and then let's say echo that data? I've tried everything I suppose so, but nothing seemed to work. Could anybody help me with it, if it is possible?

Upvotes: 2

Views: 40

Answers (2)

Peter
Peter

Reputation: 447

To complete the Tomasz Nguyen answer, if you want to pass data via POST method you can do this:

$('.preview a').click(function() {
   $.ajax({
       url: 'images.php',
       type: 'POST',
       data: {foo:value}
   }).done(function(response){
       $('.presentation > .images > .margin').html(response);
   });
});

You can read the foo var in images.php with $_POST['foo'] and build your response to append inside $('.presentation > .images > .margin')

If you want to pass the var via get method, just change the type parameter to 'GET'

Upvotes: 2

Tomasz Nguyen
Tomasz Nguyen

Reputation: 2611

You could encode the data into the url. Assume you want to send the string Hello to the server, you could do the following:

$('.presentation > .images > .margin').load('images.php?v=Hello');

In PHP, you can access the string Hello via $_GET['v'].

Upvotes: 3

Related Questions