如何将jquery变量传递给php然后显示它 [英] how to pass a jquery variable to php and then display it

查看:78
本文介绍了如何将jquery变量传递给php然后显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Jquery变量传递给php时出现问题。

Problem in passing Jquery variable to php .

当我将变量id传递给 B.php 我是收到此错误

When i pass variable id to B.php i am getting this error

"Notice: Undefined index: id1 in C:\xampp  \htdocs\PhpProject1\OtherUsableItems\B.php on line 2 

如何解决这个问题????这个 A.php

How to solve this problem ???? This A.php

<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
  var id = 23; 
$("#submit").click(function(){
 $.post("B.php",
 {
     id1: id,

  } );      
   });
  });
 </script>
 </head>
 <body>
 <form action="B.php" method="post" >
 <input type="submit" id="submit" name="submit"/> 
 </form>
 </body>
 </html>

这个 B.php

  <?php
   $a =$_POST['id1'];
   echo $a ;
   ?>

我想将id变量传递给 B.php

I want id variable to pass on to B.php

推荐答案

试试这个:

<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
  var id = 23; 
$("#submit").click(function(){
 $.ajax(
    {
    url: "B.php",
    type: "POST",

    data: { id1: id},
    success: function (result) {
            alert('success');

    }
});     
   });
  });
 </script>
 </head>
 <body>
 <form  >
 <input type="button" id="submit" name="submit"/> 
 </form>
 </body>
 </html>

然后B.php

<?php
   $a =isset($_POST['id1'])?$_POST['id1']:'not yet';
   echo $a ;
   ?>

这篇关于如何将jquery变量传递给php然后显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆