0

I need to get li value onclick function in php ...and want to store that in php session.. i used my code for this but this is not working.... i need to know that how to get li value on click function in java script.. i NEED to get li tag value of on click function and want to store in php session by js or jquery

<li class="atleta" value="100" >
            <a href="#"  >Vendor Registration</a>
</li>

$('.atleta').click(function(e) {


            var <?php  $_SESSION['VENDR']?>  = $(this));

});
dkstack Kumar
  • 31
  • 1
  • 7
  • 1
    Why on earth would you use a PHP session variable as the name of a javascript variable. That makes no sense at all ? – adeneo Nov 07 '14 at 06:57
  • Oh, now I see, you didn't get the execution order of **serverside** language and **clientside** language – adeneo Nov 07 '14 at 06:58
  • 1
    There is no way to set a PHP variable like that, as the PHP code executes on the server, long before it ever reaches the browser and the javascript executes. You have to use ajax and send the value back to the server, but you can't send a jQuery object back, and there is no reason what so ever to do so either. – adeneo Nov 07 '14 at 06:59
  • http://stackoverflow.com/questions/10565055/setting-php-session-var-from-within-js – Dexter Nov 07 '14 at 07:07
  • btw you can do it by ajax – NullPoiиteя Nov 07 '14 at 07:07

4 Answers4

2

Don't mix server side and client side technologies.Instead to php session i will recommend to use localStorage instead and store clicked li value in it as shown :-

$('.atleta').click(function(e) {
   localStorage.setItem('lival',$(this).val());
});

and in order to get data stored in localStorage use this :-

var livalue = localStorage.getItem('lival');

Reference

Or Instead of localStorage you can use sessionStorage also(their basic usage is same except for some difference refer here)

Community
  • 1
  • 1
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
0

try this -

$('.atleta').click(function(e) {

        e.preventDefault(); //if needed
        var <?php  $_SESSION['VENDR']?>  = $(this).children('a').text();

});
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

i used this and its working..

<li id="atleta" value="18" onclick="vendorData();">
            <a href="#" >Vendor Registration</a>
</li>

<script>

function vendorData(){
    var h = document.getElementById('atleta').value;
    localStorage.setItem("vendor", h);
    var ven = localStorage.getItem("vendor");

    }
</script>
dkstack Kumar
  • 31
  • 1
  • 7
0

You can store your variable in cookies, so you can set it via jQuery and get in php later

xottabych007
  • 142
  • 2
  • 10