1

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 for 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

 ..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() {

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...

3
  • There are several ways to pass values from php to Jquery, but you need to post some of your code to provide you the better way for your case... Commented Jun 23, 2010 at 14:57
  • What templating library are you using? How do you include PHP values in the template? Can you provide an example of a variable in the tpl file? Commented Jun 23, 2010 at 15:44
  • Does that really matter? Other jquery functions are working, the template system too...as i said i tried clear testing php with the following code: <?php echo 1; ?> and it worked...so probably something is bad with echo or what...and i thought about passing values from variable as a solution (+ echo is looked too bad for me) Commented Jun 23, 2010 at 15:48

4 Answers 4

5

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

0

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>'; 
?>

Comments

0

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.

2 Comments

then in the page register.php, you've got to have other elements besides your echo "1" (check your includes). What does the firebug console tells you in the response part?
I see that in your $.post there is no submit : true ou submit : *, so $_POST['submit'] doesn't exist.
0

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.

1 Comment

but that's not the case...i need to post firstly and then get a value...and now the problem is with setting up the post values in jquery the right way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.