5

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

vignesh raj kumar
  • 181
  • 1
  • 1
  • 9

3 Answers3

9

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;
?>
Ali Zia
  • 3,825
  • 5
  • 29
  • 77
0

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.

Do some thing like this.

$(".next").click(function (){

var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.

//make ajax call with this as follows

$.post("a.php",{php_val:php_val},function(data){

//a.php is page name where u want to send php value.
})


});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>

I hope this answers your questions

-2

It will help you ,

 <script>
 $(document).ready(function() {
 $(".next").click(function() {
    <?php echo $var     = 1 ; ?>
 });
});
</script>

<?php
  if (!empty($var)) {
     echo $var;
 }
 ?> 
Vishnu R Nair
  • 335
  • 2
  • 17