最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to pass a jquery variable to php and then display it - Stack Overflow

programmeradmin1浏览0评论

Problem in passing Jquery variable to 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 

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>

This B.php

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

I want id variable to pass on to B.php

Problem in passing Jquery variable to 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 

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>

This B.php

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

I want id variable to pass on to B.php

Share Improve this question edited Apr 5, 2013 at 11:41 Mr. Alien 157k36 gold badges303 silver badges285 bronze badges asked Apr 5, 2013 at 11:39 AbhishekAbhishek 812 gold badges2 silver badges10 bronze badges 3
  • 1 you already go to B.php after submit,so why not making hidden input field with value you want to send and then read it on B.php? – Bojan Kovacevic Commented Apr 5, 2013 at 11:43
  • You are getting error C:\xampp \htdocs\PhpProject1. try using http://localhost – Jai Commented Apr 5, 2013 at 11:52
  • And i also want to save this variable to mysql database – Abhishek Commented Apr 5, 2013 at 12:51
Add a comment  | 

9 Answers 9

Reset to default 6

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

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

Try changing the input type to button instead of submit <input type="button" id="submit" name="submit"/>

The problem is that your form is submited ,$.post is for 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(); in your button click function

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

if( isset($_POST['id1']) )  {
// do something
}
$(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). That is a feature of 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)

Another problem: you must prevent default submit of a form. e.preventDefault() is needed for that. Without it, ajax post will be started and than usual form submit will happen. And your form has no element with name id1, suppose that second request produce that error message.

EDIT: This is to post a value with regular submit, so browser is redirected to B.php

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

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

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. You have a trailing comma here {id1 : id,} this is an issue in IE browsers but latest other browsers will just work fine.

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.

Instead try using http://localhost/yourapplication/page.php

Or you could use submit() this way

$(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);

if(isset($_POST['id1'])){
    print_r( $_POST );
}
发布评论

评论列表(0)

  1. 暂无评论