Getting variable value from PHP with jQuery

前端 未结 4 1487
一向
一向 2021-01-16 07:50

So how do i get variable value from php file with jquery...? the jquery code is in other file (tpl)

for example i have register.php and register.tpl (template file f

相关标签:
4条回答
  • 2021-01-16 07:58

    Either you make a Jquery Ajax Request that will request a php page which will return whatever you want or you echo a javascript variable with php

    <?php 
      echo '<script> var javascript_variable = "whatever"; </script>'; 
    ?>
    
    0 讨论(0)
  • 2021-01-16 07:59

    Best bet is to output the PHP variable as a hidden field or a JavaScript variable:

    <input type="hidden" id="my_var" name="my_var" value="<?php echo($my_var); ?>" />
    
    // access it like this:    
    alert($('#my_var').val());
    

    or

    <script type="text/javascript">
        var my_var = <?php echo($my_var); ?>;
    </script>
    
    // access it like this
    alert(my_var);
    

    That should do it :-)

    0 讨论(0)
  • 2021-01-16 08:15

    It will work if you do

    echo "1";
    

    and then

    if(result == "1") {
    

    If it doesn't (but I've checked on a code of mine without the quotes, it didn't work, with, it was ok), check the response from Firebug console.

    0 讨论(0)
  • 2021-01-16 08:16

    In situations where my company's application needs to call Jquery on a dynamic element and we have the Jquery call IN the php file we'll directly call php in the Jquery call.

    For example:

    alert($('#').val());

    Not for all situations, certainly. If you have to call a variable where you don't have PHP access to the file (possibly such as a .tpl file, depending on your setup) you might resort to setting a hidden input as detailed above.

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