Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign jquery value to php variable

Tags:

jquery

php

In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.

<input type="button" name="next"  class="next btn btn-primary" value="Proceed To Checkout Page" />

Jquery

$(".next").click(function(){
       <?php $var1="1";?>
 }

While check the php value

<?php if(isset($var1)){
     echo $var1;
 }else{
    echo "NULL";
   }?>

Every time time i got the null value. Where i getting mistake. Ps: I cant able to send the ajax call to get the value

like image 561
vignesh raj kumar Avatar asked Mar 16 '16 04:03

vignesh raj kumar


People also ask

How to assign jquery value in PHP?

If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.

How set PHP variable in JavaScript?

To Set PHP Variable in JavaScript, we define a JS variable and assign PHP value using PHP tag with single or double quotes. Most of the time you have to set PHP variable value in JavaScript. If there is only simple variable like string or any integer then you can simply echo on the JS variable with PHP tag.


1 Answers

You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.

You can use cookies to achieve this.

In Javascript:

<script type="text/javascript">
    document.cookie = "var1=1";
</script>

And in PHP

<?php 
   $phpVar =  $_COOKIE['var1'];
   echo $phpVar;
?>
like image 131
Ali Zia Avatar answered Oct 13 '22 08:10

Ali Zia