1

I am trying to send data with jQuery and Ajax using $.ajax. I simplified the case just to send one thing but it does not work. Can someone help?

HTML:

<a href="#" id="button">clic!</a>
<br>
<div id="content"></div>

JQUERY

$("#button").click(function(e){
    e.preventDefault();
    $.ajax({
        name: "John",
        type: 'POST',
        url:"3.php",
        success:function(result){
            $("#content").html(result);
        }
    });

 });

PHP:

<?php

if( $_REQUEST["name"] )
{
   $name = $_REQUEST['name'];
   echo $name;
}

?>

4 Answers 4

5

You should send data like this:

$.ajax({
    type: "POST",
    url: "3.php",
    data: { name: "John" },
    success:function(result){
        $("#content").html(result);
    }
})
Sign up to request clarification or add additional context in comments.

Comments

1

Your JQuery syntax is wrong. You should add data field and put postvars there:

$.ajax({
   type: "POST",
   url: "3.php",
   data: { name: "John" }
   success:function(result){
       $("#content").html(result);
   }
});

Comments

1

You are sending wrong name: "John",it will be data: { name: "John" } then you will get$_REQUEST['name'] on php page so full request will be

$.ajax({
    type: "POST",
    url: "3.php",
    data: { name: "John" },
    success:function(result){
        $("#content").html(result);
    }
})

Comments

0

Your code should be

$("#button").click(function(e){
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url:"3.php",
        data:{name:"John"},
        success:function(result){
            $("#content").html(result);
        }
    });

 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.