简体   繁体   English

使用jQuery从PHP获取变量值

[英]Getting variable value from PHP with jQuery

So how do i get variable value from php file with jquery...? 那么,如何使用jquery从php文件中获取变量值? the jquery code is in other file (tpl) jQuery代码在其他文件中(tpl)

for example i have register.php and register.tpl (template file for register.php) 例如我有register.php和register.tpl(register.php的模板文件)

register.php register.php

 ...some includes here...



if(isset($_POST['submit'])) {
    $username = mysql_real_escape_string(trim($_POST['username']));
    $email = mysql_real_escape_string(trim($_POST['email']));
    $check = $mysql->query("SELECT username FROM ".TBL_USERS." WHERE username = '".$username."' OR email = '".$email."'");
$rows_check = mysql_num_rows($check);
if($rows_check > 0) {
    echo 1;
} else {
    $password = mysql_real_escape_string($_POST['password']);
        $salt = generate_salt($email);
        $hash = hash_password($password, $salt);
        $q = $mysql->query("INSERT INTO ".TBL_USERS." (username, password, email, salt) VALUES ('".$username."', '".$hash."', '".$email."', '".$salt."')");
        if($q) {
            header("Location: index.php");
        } else {
            die(mysql_error());
        }   
    }
} else {
.. calling parse template function ...
}

register.tpl register.tpl

 ..jquery library included..
    <form id="register" action="register.php" method="post">
       <tr>
       <td>Username</td>
       <td><input type="text" id="username" name="username" class="register" style="width: 200px;" />
    </td>

email ...other inputs... $("#username").blur(function() { 电子邮件...其他输入... $(“#username”)。blur(function(){

var email_v = $("#email").val();
 $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
 $.post("register.php",{ username:$(this).val(), email: email_v, submit: true } ,function(data)
 {
  if(data=="1") 
  {
   $("#msgbox").fadeTo(200,0.1,function()
   {

    $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
   });
  }
  else
  {
   $("#msgbox").fadeTo(200,0.1,function() 
   {
    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
   });
  }
 });
});
</script>

when i changed the whole register.php for testing purposes to or the script worked...however with the original version it shows always that username is available... 当我出于测试目的将整个register.php更改为或脚本工作时...但是对于原始版本,它始终显示用户名可用...

Best bet is to output the PHP variable as a hidden field or a JavaScript variable: 最好的选择是将PHP变量输出为隐藏字段或JavaScript变量:

<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 :-) 那应该做的:-)

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 您可以发出一个Jquery Ajax请求 ,该请求将请求一个php页面,该页面将返回您想要的任何内容,或者您​​用php回显一个javascript变量

<?php 
  echo '<script> var javascript_variable = "whatever"; </script>'; 
?>

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. 如果没有(但是我检查了没有引号的我的代码,那没用,可以),请从Firebug控制台检查响应。

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. 在我公司的应用程序需要在动态元素上调用Jquery且在php文件中有Jquery调用的情况下,我们将在Jquery调用中直接调用php。

For example: 例如:

alert($('#').val()); 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. 如果必须在无法通过PHP访问文件的地方调用变量(可能是.tpl文件,具体取决于您的设置),则可以使用设置隐藏输入的方法,如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM