Assign jquery value to php variable

后端 未结 3 1503
轻奢々
轻奢々 2020-12-21 04:05

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.



        
相关标签:
3条回答
  • 2020-12-21 04:45

    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;
    ?>
    
    0 讨论(0)
  • 2020-12-21 04:46

    It will help you ,

     <script>
     $(document).ready(function() {
     $(".next").click(function() {
        <?php echo $var     = 1 ; ?>
     });
    });
    </script>
    
    <?php
      if (!empty($var)) {
         echo $var;
     }
     ?> 
    
    0 讨论(0)
  • 2020-12-21 04:57

    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

    0 讨论(0)
提交回复
热议问题