user1788736
user1788736

Reputation: 2845

How make post request and get response back using getjson method?

I want to post textarea data to a php form and get its response back to another textarea. could you guys tell me how this can be done? I tried following but it doesn't work!

$.post(url, dataToBeSent, function(data, textStatus) {

   url="./doit.php";
   var dataToBeSent = $("myform").serialize();

var siteContents2 = data.contents;  
document.myform2.outputtext2.value = siteContents2 ;

}, "json");

forms:

<form id="myform" name="myform" action="./ok1.php?Id=&title=" method="post">
<td><textarea rows="7" cols="15" name="outputtext" style="width: 99%;"></textarea></td>

</form>

<form id="myform2" name="myform2" action="./ok2.php?Id=&title=" method="post">
<td><textarea rows="7" cols="15" name="outputtext2" style="width: 99%;"></textarea></td>

</form>

php get data passed beg getjson:

$passed3 = $_post['dataToBeSent'];
echo $passed3;

Upvotes: 0

Views: 3546

Answers (1)

ndequeker
ndequeker

Reputation: 8010

You define the URL and data in the callback function. You need to define them before you call $.post(). You can try it with the code below, this is the 'shorthand' version with $.ajax().

JS

$.ajax(
{
    type: 'POST',
    url: './doit.php',
    data: $("#myform").serialize(),
    success: function(data)
    {
        $('.result').html(data);
        $('textarea[name="outputtext2"]').val(data);
    }
});

PHP

$formData = $_POST;
echo $formData['outputtext'];

More information about POST-requests with jQuery: http://api.jquery.com/jQuery.post/

Upvotes: 1

Related Questions