Mayur
Mayur

Reputation: 289

data not passing from ajax post method to php call back

I am trying to pass data in textarea and make the php callback to retutn the same and display it in textarea after simple manipulation to ensure transfer took place.But i can't make my ajax function pass the data.

<script type="text/javascript" src="ajax.js"></script>

This is my form

<form action="#" method="post" onsubmit="submit_notes_call();return false;">
    <textarea rows="5" cols="30" name="user_notes" id="user_notes"></textarea>
    <input type="submit" name="notes_submit" id="notes_submit" value="UPDATE NOTES"  >
</form>

Here is my Ajax function in ajax.js

function submit_notes_call()
{
    var ajaxVar;
    var user_notes = " Data from ajax call to php callback ";
                      /*document.getElementById("user_notes").value; */
    try
    {
        ajaxVar = new XMLHttpRequest();
    }catch (e)
    {
        try
        {
            ajaxVar = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e)
        {
            try
            {
                ajaxVar = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e)
            {
                alert("Your browser broke!");
                return false;
            }
        }
    }
    ajaxVar.onreadystatechange=function(){
                                            if(ajaxVar.readyState==4)
                                                {
                                                document.getElementById("user_notes").innerHTML = ajaxVar.responseText;
                                                }
                                            };
    ajaxVar.open('POST','notes_submit.php',true);
    ajaxVar.send(user_notes);


}

and here is my php code in notes_submit.php

<?php 
    $data_recieved = "Data from php call back to ajax function+".$_POST['user_notes'];
    echo "Data : " . $data_recieved;
?>

I get the output as Data : Data sent from php call back to ajax function+ inside the textarea field which means that although comunication is taking place but php call back is not able read the data sent by ajax caller or may be ajax function is not able to send the data to php call back.

What is the error here??

Upvotes: 0

Views: 273

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Replace thie line

ajaxVar.send(user_notes);

by

ajaxVar.send('user_notes='+user_notes);

Read this http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

Upvotes: 1

Related Questions