Sebastian
Sebastian

Reputation: 3628

AJAX POST: echoing posted anchor tag value

HTML/jQuery:

<a href=# id="friends">Friends</a>
<script type="text/javascript">
    $(document).ready(function() {
        $('a#friends').click(function() { 
            $.ajax({
                type: "POST",
                url: "data.php",
                data:  $('#friends').html(),
                success: function(data) {
                    $('#questions').html(data);
                },
                dataType: "HTML"
            });
        });
    });
</script>

data.php:

<?php
    echo $_POST['#friends'];
?>

How do I return this POST value of an id in an anchor tag? The variable is being passed to PHP because I can alert it, but the problem is getting it back.

Upvotes: 1

Views: 1940

Answers (6)

mitesh
mitesh

Reputation: 1

Pass the data variable in data field. For more info see below example

$(document).ready(function() {
    $('a#friends').click(function() { 
        alert("");
        $.ajax({
            type: "POST",
            url: "data.php",
            data:  "#friends="+$('#friends').html(),
            success: function(data) {
                alert(data);
                $('#questions').html(data);
                },
            dataType: "HTML"
        });
    });
    });

Upvotes: 0

NappingRabbit
NappingRabbit

Reputation: 1918

alternatively you can use

<a href=# id="friends">Friends</a>
<script type="text/javascript">
    $(document).ready(function() {
        $('a#friends').click(function() { 
            $.post("data.php",{
                friends: $("#friends").html()
            },function(data){
                $("#questions").html($.trim(data)); // trim to be sure
            });
        });
    });
</script>

and in the php:

<?php
   echo $_POST['friends'];
?>

Upvotes: 0

asprin
asprin

Reputation: 9823

First of all you can't send data to the ajax page in this way

data:  $('#friends').html(),

A more suitable way would be

data : {'key1':'val1', 'key2':'val2'}

Then on the php page, you can retrieve these values in this fashion

$key1 = $_POST['key1']; // will contain 'val1'
$key2= $_POST['key2']; // will contain 'val2'

Upvotes: 1

Hkachhia
Hkachhia

Reputation: 4539

Your syntax is wrong for passing friends value to data.php

Try this

$(document).ready(function() {
    $('a#friends').click(function() { 
        $.ajax({
            type: "POST",
            url: "data.php",
            data: "friends="+$('#friends').html(),
            success: function(data) {
                $('#questions').html(data);
                },
            dataType: "HTML"
        });
    });


<?php

echo $_POST['friends'];

?>

Upvotes: 1

Vinoth Babu
Vinoth Babu

Reputation: 6852

How you are passing the data to PHP,

please use the following code,

<a href=# id="friends">Friends</a>
<script type="text/javascript">

    $(document).ready(function() {
    $('a#friends').click(function() { 
        $.ajax({
            type: "POST",
            url: "data.php",
            data:  {'friends' : $('#friends').html()},
            success: function(data) {
                $('#questions').html(data);
                },
            dataType: "HTML"
        });
    });

</script>

<?php
   echo $_POST['friends'];
?>

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

You need to specify the name of the value you are sending across in your AJAX request. Try this:

$.ajax({
    type: "POST",
    url: "data.php",
    data:  { 'friends': $('#friends').html() }, // Note the value is sent in an object with a key of 'friends'
    success: function(data) {
        $('#questions').html(data);
    },
    dataType: "HTML"
});
<?php
    echo $_POST['friends']; // retrieve the 'friends' value
?>

Upvotes: 5

Related Questions