0

i am passing my variable throught an AJAX request in javascript but not getting it in my php file. NOt sure where i am going wrong.?

JS code

var build = {
m_count : (document.getElementById('count').value),
}
$.ajax({
    data: build,
    type: "POST",
    url: "tabs.php",});

PHP code

<?php

$module_c = $_POST['data'];
echo $module_c;
?>
1
  • 1
    var_dump or print_r are your friends. i.e.: var_dump($_POST); Commented Jun 2, 2017 at 18:06

2 Answers 2

2

You have to get the data by the name of the variable you want to get, which is m_count.

<?php
    $module_c = $_POST['m_count'];
    echo $module_c;
?>

EDIT:

Like suggested in the comments, change your JavaScript code to:

var build = {
    m_count : (document.getElementById('count').value)
}

$.ajax({
    data: build,
    type: "POST",
    url: "tabs.php",
    success: function(data) {
        alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

9 Comments

@veenu this is a right answer but you still need to use ajax success or .done() callback function
Complete your answer by add success or .done() and alert the returned data
i am fairly new to this. can you please tell how to use success callback function
@veenu 1st- always keep eyes on console for errors .. 2nd- for everything you use something like { value1 : value1, value2 :value } no need for last , before closing } you may got a syntax error , .. so always remove the last , comma before closing } .. that mean for m_count : (document.getElementById('count').value), no need for the last , remove it
@veenu Good. To put stuff on the screen you'd usually set the HTML div or element you want with the value you got back from the php code. Like, when you have a div with id "my-div" in your HTML code you could do $('#my-div').append(data); in your success function.
|
0

PHP:

   <?php
        $module_c = $_POST['m_count'];
        echo $module_c;
    ?>

JS:

var build = {
    m_count : (document.getElementById('count').value),
}

$.ajax({
        url: 'php/server.php',
        type: 'POST',
        data: build,
    })
    .done(function(msg) {
            // JSON.parse turns a string of JSON text into a Javascript object.
            var message = JSON.parse(msg);
            alert(message);
        }
    })
    .fail(function(err) {
        console.log("Error: "+err);

    })
    .always(function() {
        console.log("Complete");
    })

;

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.