简体   繁体   English

如何将jquery变量传递给php然后显示它

[英]how to pass a jquery variable to php and then display it

Problem in passing Jquery variable to php . 将Jquery变量传递给php时出现问题。

When i pass variable id to B.php i am getting this error 当我将变量id传递给B.php时,我收到此错误

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

How to solve this problem ???? 如何解决这个问题呢 ???? This A.php 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>

This B.php B.php

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

I want id variable to pass on to B.php 我希望id变量传递给B.php

Try this one: 试试这个:

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

Then B.php 然后B.php

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

尝试将输入类型更改为按钮而不是提交<input type="button" id="submit" name="submit"/>

The problem is that your form is submited , $.post is for ajax sumit. 问题是你的表单被提交, $.post用于ajax sumit。 For that you need to prevent form submit. 为此,您需要阻止表单提交。

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

           });  
           event.preventDefault() ;
        });    
    });
</script>

Add event.preventDefault(); 添加event.preventDefault(); in your button click function 在你的按钮点击功能

$(document).ready(function(){
  var id = 23; 
  $("#submit").click(function(e){
     $.post("B.php", {id1: id} );     
     e.preventDefault();
   });
 });

Try code above. 试试上面的代码。 First: JS does not allow to do something like this: {id1: id,} (see comma at the end). 首先:JS不允许这样做: {id1: id,} (最后请参见逗号)。 That is a feature of PHP. 这是PHP的一个特性。 Actually, your JS code should not be executed at all because that is a syntax error (suppose you will find it in console of your browser) 实际上,你的JS代码根本不应该执行,因为这是一个语法错误(假设你会在浏览器的控制台中找到它)

Another problem: you must prevent default submit of a form. 另一个问题:您必须阻止默认提交表单。 e.preventDefault() is needed for that. e.preventDefault()需要e.preventDefault() Without it, ajax post will be started and than usual form submit will happen. 如果没有它,将启动ajax帖子,而不是通常的表单提交。 And your form has no element with name id1, suppose that second request produce that error message. 并且您的表单没有名称为id1的元素,假设第二个请求产生该错误消息。

EDIT: This is to post a value with regular submit, so browser is redirected to B.php 编辑:这是通过常规提交发布值,因此浏览器被重定向到B.php

$(document).ready(function(){
      var id = 23; 
      $("#submit").click(function(){
              $(this).closest("form")
                     .append($("<input>", {"type":"hidden", "name":"id1", "value":id}));
       });
     });

Try this: 试试这个:

<script>
$(document).ready(function(){
   var id = 23; 
   $("#submit").click(function(e){
     e.preventDefault();
     $.post("B.php", {id1: id}, function(data){ //<---get the data from B.php
        console.log(data);   //<--------show it here
        alert(data);
     });      
   });
  });
 </script>

Since you are using ajax feature so you have to stop the form submit feature. 由于您使用的是ajax功能,因此必须停止表单提交功能。 You have a trailing comma here {id1 : id,} this is an issue in IE browsers but latest other browsers will just work fine. 这里有一个尾随逗号{id1 : id,}这是IE浏览器中的一个问题,但最新的其他浏览器才能正常工作。

Note: 注意:

You posted this error: 你发布了这个错误:

"Notice: Undefined index: id1 in C:\\xampp \\htdocs......

Don't run the page in filesystem $.post() won't work this way. 不要在文件系统中运行页面$.post()不会以这种方式运行。

Instead try using http://localhost/yourapplication/page.php 而是尝试使用http://localhost/yourapplication/page.php

Use a hidden field to set the value you want to send across. 使用隐藏字段设置要发送的值。 Since you are using submit, your entire form is submitted. 由于您使用的是提交,因此您的整个表单都已提交。 The hidden field is also sent and this can be accessed directly using the name of the hidden field. 隐藏字段也会发送,可以使用隐藏字段的名称直接访问。

An E_NOTICE is thrown by php because the index of the POST-Array doesnt exist. php抛出了E_NOTICE,因为POST-Array的索引不存在。 You can switch error_reporting of for notices, see http://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reporting or check if the index exists: 您可以切换通知的error_reporting,请参阅http://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reporting或检查索引是否存在:

if( isset($_POST['id1']) )  {
// do something
}

Try this code, 试试这段代码,

 <html>
 <head>
 <script src="jquery.js"></script>
 <script>
 $(document).ready(function(){
 var id = 23; 
 $("#submit").click(function(){
 $('#id1').val(id);
 });
 });
</script>
 </head>
 <body>
 <form action="B.php" method="post" >
 <input type="submit" id="submit" name="submit"/> 
 <input type="hidden" name="id1" id="id1" value=""/>
</form>
</body>
</html>

Here you can pass value of 'id' to B.php,And will be access as $_POST['id1'] 在这里你可以将'id'的值传递给B.php,并且将作为$ _POST ['id1']访问

Or you could use submit() this way 或者你可以这样使用submit()

$(function(){
    var id = 23;
    $("#myform").submit(function() {
        $.post("B.php", { id1: id }).done(function(data) {
            alert( data );
        });
        return false; //true(post form data)
    });
});

And form 形式

<form id="myform" method="post">
<input type="submit" id="submit" name="submit" />
</form>

EDIT: Oh and yeah for B.php isset check or error_reporting(0); 编辑:哦,是的B.php isset check或error_reporting(0);

if(isset($_POST['id1'])){
    print_r( $_POST );
}

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

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