Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 436
Abhishek Avatar asked Jan 01 '26 08:01

Abhishek


2 Answers

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 ;
   ?>
like image 67
premananth Avatar answered Jan 03 '26 23:01

premananth


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

like image 32
Nisanth Sojan Avatar answered Jan 03 '26 23:01

Nisanth Sojan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!