duanboniao5903 2012-06-18 06:42
浏览 29

如何将变量值从javascript发布到PHP?

I am using JavaScript to pass a variable to another php file. I have a value in javascript, and I want to pass it as a post variable to another php file.

Javascript code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" >

//Important code here to post a variable to bh.php:
$(document).ready(function(){
  $('#op_comp_id').load('bh.php').show();
    $('#get_value_id').keyup(function() {
    $.post('bh.php', { id: form.get_value.value },
        function(result){
            $('#op_comp_id').html(result).show();
            });  
    });
});
</script>

</head> 
<body>
<?$id = $_GET['id'];?>
<form  name="form">
<input type="text" id="get_value_id" name="get_value" value="<?php echo $id ?>">
</input>
</form>

<div id="op_comp_id"></div> 

</body> 
</html>

This was my attempt to pass a variable value from javascript to bh.php in post mode.

bh.php?id=value

Is there an alternative way or better way to do this?

  • 写回答

2条回答 默认 最新

  • dongshan0202405 2012-06-18 06:57
    关注

    Use following method to pass a JavaScript variable to php with a post:

     $(input[name=get_value]).bind('keyup click blur focus change paste',function(){ 
                    var id = $(this).val();
                    $.ajax({    
                        type: "POST",                                  
                        url: "bh.php",
                        async: false,
                        data: "clientId="+id ,
                        success:function(response){
                        $('#op_comp_id').html(response).show();
    
                        }                        
                    });
                   } 
    

    bh.php use following code

    <?php
     $id = $_REQUEST['clientId'];
     // your code
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大