Mohd Maaz
Mohd Maaz

Reputation: 278

undefined data when posting to php by ajax?

this is my javascript code

    var ActivityType ='d';
    var TotalActivities = 2;
    function marker(ActivityType,TotalActivities)

    {

    var dataTosend='typ='+ActivityType+'&total='+TotalActivities;

    $.ajax({

    url: 'activity.php',

    type: 'POST',

    data:dataTosend,

    async: true,

    success: function (data) {

    alert(data)

    },

    });

    }
marker();

this is my activity.php file

<?php
echo $_POST['typ'];
echo $_POST['total'];

?>

when i call marker(); in js i got undefined undefined in alert

why does it says undefined data ?

but there is no error means typ ,& total parameter are reaching there

but why it says undefined

Upvotes: 0

Views: 31

Answers (2)

Sandeep K.
Sandeep K.

Reputation: 759

When you have call marker function the arguments are missing.

marker(ActivityType,TotalActivities);

you can also use this format when you have send the data using ajax.

    data:{typ:ActivityType,total:TotalActivities}

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

You aren't passing in the variables as parameters when you call marker().

The arguments in the function are same name as the outer variables so inside the function scope, the argument versions are undefined and the outer variables are shadowed by the same name arguments

Try calling:

marker(ActivityType,TotalActivities);

Upvotes: 2

Related Questions