1

Please help me on this problem. It is small but causing me lot of trouble.I have set the session variable in my index.php and when clicked on anchor tag , it is not changing the session variable. Relevant PHP part:

session_start();
$_SESSION["amount"] = "99";

Now the html part (in same PHP file).

<a onclick="change('<?php echo $_SESSION['amount']='399'; ?>')" href="#"><h4>Order 399</h4></a>

<a onclick="change('<?php echo $_SESSION['amount']='200'; ?>')" href="#"><h4>Order 200</h4></a>

I am checking the value of Session variable in another div in same file.

<td>Amount: <?php echo $_SESSION["amount"] ?> </td>

the value shown is 200 though clicked on "Order 399".

How do I change session variable?Am I mixing Client side and server side? Please help me and do suggest the shortest possible way to do it.

Mohit Kumar
  • 500
  • 6
  • 24
  • Yes. You are mixing Client side and server side. Look for ajax. – Sougata Bose Aug 05 '15 at 12:50
  • You need to dive into `ajax` – Daan Aug 05 '15 at 12:51
  • You need to use AJAX to achieve this. You seem confused about the difference between client-side and server-side code. – Rory McCrossan Aug 05 '15 at 12:51
  • http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Zsw Aug 05 '15 at 12:51
  • Yes your are mixing client- and server-side. The PHP Code is interpreted in HTML-Code. So you set the Session-Variable to 200 in your second anchor tag and printing something like "change ('200')" in your onclick attribute of this tag. The "change" is a Javascript function and does not respect PHP. – Tim Wißmann Aug 05 '15 at 12:57

2 Answers2

0

Actually you are mixing it up!

It could be done like this:

You need a js-function, which calls a PHP-File, which then will change the value of the Session Variable. And don't forget: Locate all the values on the currently loaded page via JS, because they won't change instantly. For sure they will change on Page-Reload, but if you want to avoid a page reload, then change the values, which are currently on the page.

jQuery ajax and append will be a huge help here.

If you have tried this and have still problems, then just ask again, but please try it to do first by yourself!

Haudegen
  • 498
  • 1
  • 4
  • 17
0

The last value of your session parameter is 200, you need to send an ajax request and then retrieve the latest changed value of your parameter.

you shouldn't change in in 'view' port.

Rabih
  • 298
  • 1
  • 6
  • 18