0

I would like to know how I change a PHP variable if I click on a button. Let's say I have the following webpage:

_______________________________________________
|   Menu    |         A Webpage                |
|           |                                  |
| - item1   |                                  |
| - item2   |                                  |
| - item3   |          Content                 |
| - item4   |                                  |
|           |                                  |
|           |                                  |

And if I click on 'item1' the page changes with the data that is related to the user that is linked with that button. So does it with the other items.

This is currently my code:

<script>
function test1()
{
alert("Hello World1!");
}
function test2()
{
alert("Hello World2!");
}
function test3()
{
alert("Hello World3!");
}
function test4()
{
alert("Hello World4!");
}


</script>


    <a id="test1" href="#" onClick="test1();"><h5>item1</h5></a><br /><br />
    <a id="test2" href="#" onClick="test2();"><h5>item2</h5></a><br /><br />
    <a id="test3" href="#" onClick="test3();"><h5>item3</h5></a><br /><br />
    <a id="test4" href="#" onClick="test4();"><h5>item4</h5></a><br /><br />

This is is enough to let a window appear or to hide a division or something. But what I want is if a button is clicked I want to change a PHP variable. Like this:

<script>
function test1()
{
<? $userid == '1'; ?>
}
</script>


    <a id="test1" href="#" onClick="test1();"><h5>item1</h5></a><br /><br />

With this I can easily change the page to what the user wants to see, without the need to make multiple pages. But my question is: 'how can I make this possible'?

I would like to thank you all in advance!

ejo
  • 446
  • 1
  • 9
  • 23
martini1993
  • 35
  • 1
  • 1
  • 5
  • 5
    PHP runs on a server javascript runs on the client. If you want to change a php variable you will need to send a request to the server to change it and reload the changed content. – Anigel Jun 26 '13 at 08:19
  • You cannot mix php and javascript like that. One is server side and the other is client side. But there is a way to comunicate between them. Post the code that generate you table and we can help you from there. – Sergio Jun 26 '13 at 08:20
  • So i have to work with post and fetch? Could you give me an sample based on the example i gave above? – martini1993 Jun 26 '13 at 08:21
  • You can't modify PHP with JavaScript, but you can use http Requests in JavaScript to call PHP scripts, that way you don't need to refresh the page and then you can offer a "all-in-one-site" – Avinor Jun 26 '13 at 08:21

4 Answers4

2

PHP is not the same language or type of language as JavaScript. PHP runs in a linear manner and once processing is complete, you can't go back and call a function again or update a variable for example. PHP runs code from the top of a file to the bottom of a file and when it hits the bottom it ends.

The closest to the solution you are looking for would be to use jQuery AJAX to call a PHP script to update variables. It looks to me that you want to use AJAX to change the pages data. You could achieve this using the following example.

$('#menu A').click(function (e) {
    var jqxhr = $.get('phpscript.php?data=' + $(this).attr('href'));

    jqxhr.success(function(data) {
        $('#content').html(data);
    });
});

What the above will do is when you click on a link within <div id="menu" /> it will attempt to get the content from a script running in the same directory called phpscript.php. This uses the data variable to determine what page data to return. Once complete, it will output the HTML returned by your script into the #content container, e.g. <div id="content">

I hope I have understood your question correctly. And also keep in mind that this would need a little validation, checking you get a 200 OK and there is actually data received etc.

Chris
  • 1,939
  • 1
  • 19
  • 38
0

Use Ajax, this way you could load the content of a webpage inside something, like a div in your example, without needing to reload the entire page.

Ajax & PHP

Wallack
  • 782
  • 4
  • 15
  • Just take some time to understand the concept. With Ajax you will retrieve all the HTML content of the other page you want to access to, so that include header and footer. In a normal Ajax Scenario your second page (the one you want to get information from) only includes the information to gather and put it in the container you want. After you familiarize with the technology and do some test, create a new question if you need some help and close this one. – Wallack Jun 26 '13 at 08:55
0

You'll need to use Ajax requests to do this.

amulous
  • 704
  • 2
  • 6
  • 15
0

I dont get why you want to change the value of the php variable

because this variable will not exist after execution of this page. where as you can use some session variable

also if we want to set some valus we a have to assignment operator (=)

for setting a php variable you can use ajax call, in server there should be a page to change the value

shanavas
  • 1
  • 2