Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP doesn't echo posted value from jQuery

Tags:

html

jquery

php

Clicking on div #some_id calls details.php but it doesn't succeed in retrieving data from jQuery. details.php is supposed to echo 123.

My goal is that when I click on div #some_id, details.php shows 123 in the browser.

html:

<div id="some_id">
<a href="details.php"> Details </a>
</div>

jQuery:

function detailsClick(){
    $("#some_id").live('click', function(){
        var data = {'myData': 123};
        $.post('details.php', data);
    });
}

details.php:

<?php
$idNumber =  $_POST["myData"];
echo ($idNumber);
?>
like image 798
lioli Avatar asked Jan 20 '26 15:01

lioli


2 Answers

I believe that you are not getting click event here. Change jQuery code as below.

$(function(){
    $("#some_id").on('click', function(){
        var data = {'myData': '123'};
        $.post( "details.php", data)
            .done(function( data ) {
                 alert( "Data Loaded: " + data );
        });
    });
});
like image 168
Pratik Soni Avatar answered Jan 23 '26 06:01

Pratik Soni


  1. .live() is deprecated. Use .on() instead.
  2. The function detailsClick() is never called. So the onclick listener is NOT registered.

Either do this.

function detailsClick(){
    $("#some_id").live('click', function(){
        var data = {'myData': 123};
        $.post('details.php', data);
    });
}

detailsClick();

OR do this..

$(document).ready(function(){
    $("#some_id").live('click', function(){
        var data = {'myData': 123};
        $.post('details.php', data);
    });
});

ALSO, you need to use AJAX for what you want to do.

Refer AJAX with jQuery()

like image 36
Shrinivas Shukla Avatar answered Jan 23 '26 06:01

Shrinivas Shukla



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!