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

65

Problem in passing Jquery variable to php .

When i pass variable id toB.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 ???? ThisA.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>

ThisB.php

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

I want id variable to pass on toB.php

621

Answer

Solution:

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 ;
   ?>
80

Answer

Solution:

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

19

Answer

Solution:

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>

Addevent.preventDefault(); in your button click function

946

Answer

Solution:

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.

183

Answer

Solution:

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

Answer

Solution:

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

Answer

Solution:

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

119

Answer

Solution:

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 usinghttp://localhost/yourapplication/page.php

897

Answer

Solution:

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 );
}

People are also looking for solutions to the problem: database - In PHP if I want each user to have data unique to them that I want to be retained, and to be loaded just whenever they login how would I do so?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.