-1

I have a javascript function I would like to run on a page, that when executed will send a variable to a PHP script but not open the PHP page just stay on the current page.

What I am trying to do is add this function to pages on a website and for it to return information about the page it was run on, such as URL and user IP.

Everything I have tried so far has not done the job, so any info is appreciated.

Many thanks

Thanks Quentin, but I have tried to javascript forms, but they all open the url the form is being posted to. My most recent example:

var form = new Element('form',
    {method: 'post', action: 'http://www.site.com/data.php'});
    form.insert(new Element('input',
    {name: 'u', value: ""+u, type: 'hidden'}));
$(document.body).insert(form);

form.submit();

I also tried:

document.location.href='http://www.site.com/data.php?u='+u;

but again this open the page :S

For those that have suggested AJAX, thank you but it wont work because that needs a script link in the header and I need to be able to plug this into a page without editing the header. I did try to dynamically insert it via the javascript function, it went in OK but none of the AJAX functions would execute.

Paul
  • 2,465
  • 8
  • 35
  • 60
  • 10
    You are looking for AJAX. And this has been asked numerous times, so please use the search. – hakre Jan 24 '12 at 16:13
  • @hakre Unrelated -- read your [HEAD first with PHP Streams](http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/) post last night. Nice work :) –  Jan 24 '12 at 16:15
  • hey its a valid question lets not bash him for asking... – samccone Jan 24 '12 at 16:16
  • ajax and javascript do not rely on parts of in in the header, you can put it at all the bottom if you like. Maybe you should post your ajax code in a new question with an explanation of what is not working. – jeroen Jan 24 '12 at 16:52

2 Answers2

1

What you are looking for is called AJAX (Asynchronous Javascript and XML). These type of calls in Javascript are made trivial with things like jQuery.

This is what jQuery's website says about AJAX: The jQuery library has a full suite of AJAX capabilities. The functions and methods therein allow us to load data from the server without a browser page refresh.

And an example jQuery POST:

$.post('ajax/test.html', function(data) {
  $('.result').html(data);
});
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
0

Using jQuery

for simplicity do something like this

$.ajax('foo.php',function(d){ alert(d); });
samccone
  • 10,746
  • 7
  • 43
  • 50