开发者

Passing PHP variable value to jquery function

开发者 https://www.devze.com 2022-12-21 16:01 出处:网络
I\'m new to Jquery. I have a project that has checkboxes and if their status change, then I update some sql table.

I'm new to Jquery. I have a project that has checkboxes and if their status change, then I update some sql table.

But i decided to start with the simple things first to learn how this works...

So, I want to pass to a Jquery function, a php variable if a checkbox status changes.

I have this code:

<?php ?>

<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
    <script type="text/javascript" src="jquery-1.4.2.js"></script>
    <script type="text/javascript">
      function my_function(var mensagem_id)
        {
            alert("Sucesso, Mensagem nº: " + mensagem_id);
        }
    </script>
 </HEAD>

 <BODY>
 <?php 
    $mensagem_id = "3";
    echo $mensagem_id; 
 ?>
  <input type='checkbox' name='lida' value='mensagem_id' onchange='my_function($mensagem_id)'/>
 </BODY>
</HTML>

But in firebug I keep getting the message (function my_function() not defined)...

开发者_开发知识库

Can anyone help me?


jQuery runs on the client, whereas PHP runs on the server. So, there is no way to pass a PHP variable to a jQuery function. You could write directly to the onchange callback:

<input type='checkbox' name='lida' value='mensagem_id' onchange='my_function( <?php 
    $mensagem_id = "3";
    echo $mensagem_id; 
 ?>
)'/>

Also note that in jQuery you should avoid setting events this way and use jQuery events instead:

$('#myinputid').change(function() {
});

Another way will be to set a hidden field with the PHP variable and use javascript/jQuery code to read it on client side.


Take "var" away from the function definition, and use <?php echo and ?> around $mensagem_id in the onchange call.

But you're not using jQuery at all, just simple Javascript.


i think you don't have to use that var in your function parameter...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号