0

I know this has been asked on here but none of the answers I found answered my question.

I have a PHP/HTML file that the users view. When they click on a button, it creates a JS variable which I would like translated into a PHP variable. Using that variable, I would like to update the contents of a div box by calling a PHP function which populates the box with info from the server.

My questions: Can this be done with only 1 php file? Do I have to have the user reload the page after I refresh the content? What is the best way to do this?

CBroe
  • 86,812
  • 12
  • 88
  • 140
dukevin
  • 21,107
  • 35
  • 78
  • 109
  • Yes you can do this surely.. Use ajax for that. – Deepu Mar 12 '14 at 10:53
  • I don't understand what you need. PHP runs server side while javascript is on the client. To send a value to your PHP app you should POST it via HTTP. – ema Mar 12 '14 at 10:53
  • Ya you have to post the variable then only you can access that variable – Ninad Mar 12 '14 at 10:54
  • Why he has to use http reuest. He can use xmlhttp(ajax) request and sesnd the data. – Deepu Mar 12 '14 at 10:54
  • POST the variable to the same page and use $REQUEST to retrieve it? – dukevin Mar 12 '14 at 10:54
  • `$_REQUEST` method will give you the get and post data. Other wise you have to use the appropriate method which you used for posting the data to server. – Deepu Mar 12 '14 at 10:56
  • 1
    use ajax to post the JS variable, and update the div after a successful ajax call – d.yuk Mar 12 '14 at 10:56
  • Post the variable to the same page using ajax, and check for the $_POST to be set and then do whatever if it is. – superphonic Mar 12 '14 at 10:56

1 Answers1

3

You can avoid the user reloading the page, but that would require AJAX. That raises the question - Does this need to work without JavaScript?

If you are happy for this to be dependant on JavaScript you could use an AJAX post, such as this using JQuery:

$.post( "example.php", { variableName: "example" })
  .done(function( response ) {
    $('.example-output').html( response );
  });

example.php would be a different .php file, in example.php you would process the value of variableName and produce the required markup, which would be displayed in the original page. If you need this to work without JavaScript then I would suggest reloading the page.

Gary Stevens
  • 693
  • 8
  • 20