M.D Waheed M.D Waheed - 4 months ago 8
jQuery Question

i am trying to send data but unable to find the error jquery ajax

this is my index
and i want to send form data without redirecting to another page i want to add details with out refresh by using ajax j query..
is my jquey library is correct..?



$(document).ready(function(){
$(#button).click(function(){
$.post("jq.php",{
fname:$('#fname').val(),
lname:$('#lname').val()
},
function(data){
$('#results').html(data);
});
});
});

<script src="jquery-1.12.4.min.js"></script>
<input type="text" id="fname" name="fname">First Name</br>
<input type="text" id="lname" name="lname">Last Name</br>
<button id="button">Submit</button></br>
<textarea id="results" style="width:100 height:200> </textarea>





and this is action page

$fname=$_post['fname'];
$lname=$_post['fname'];

echo "$fname $lname";

Answer

$_POST is not $_post change this

$fname=$_POST['fname'];
$lname=$_POST['fname'];

echo "$fname  $lname";

Also you forgot to close " here

<textarea id="results" style="width:100;height:200;"> </textarea>

And change here too, use quotes

$("#button").click(function(){

Complete code will look like

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="test" action="">
    <input type="text" id="fname" name="fname">First Name</br>
    <input type="text" id="lname" name="lname">Last Name</br>
    <button id="button">Submit</button></br>
    <textarea id="results" style="width:100 height:200"> </textarea>
</form>
<script type="text/javascript">
    $(function(){
        $("#button").click(function(){
            $.post("index1.php",{
                fname:$('#fname').val(),
                lname:$('#lname').val()
            },
            function(data){
                $('#results').val(data);
            });
        });
    });
</script>
Comments