I want to pass some data as a variable using jQuery $.post() method to a PHP file and then display the result in a div after clicking a button. But the data isn't getting retrieved in PHP file.
<script> $(document).ready(function () { $("button").click(function () { $.post("add.php", { fname: 'Billy' }, function () { $('#topic').load('add.php'); }); }); }); </script> </head> <body> <br> <div class="container"> <?php echo "Welcome ".get('Name')." !"; ?> <div style="float: right"> <?php echo "<a href='logout.php'> Log Out </a>"; ?> </div> <br> <button>Add Topic</button><br> <div id='topic'></div> //the PHP file:
<?php session_start(); echo "".$_POST['fname'].""; //if(isset($_POST['fname'])) //{ //$fname=$_POST['fname']; //echo " ".$fname." topic added!"; //} ?> Notice: Undefined index: fname in C:\xampp\htdocs\forum\add.php on line 3
.loadafter the post? You should read the return from$.postand use that to fill #topic$.post("add.php", { fname : 'Billy' }, function(response){ $('#topic').html(response);}); });