Alex
Alex

Reputation: 3

How to replace or update existing content in textarea by using http.post method?

I plan to POST my text content to http://language.cs.usm.my/synthesis/read.php form's textarea which is third party web page, below is the form that i get from the URL.

  <form method="post" action="">
 <p>Key in sentences in Malay. </p> 
 <textarea name="malayText" rows="4" cols="100">Malaysia ialah sebuah negara raja berperlembagaan persekutuan di Asia Tenggara yang terdiri daripada 13 negeri dan tiga wilayah persekutuan. Ia menduduki bumi berkeluasan 329,847 kilometer persegi.</textarea>
 <input type="submit" value="OK" name="submit" />
  </form>

The method i use to post data as below:

$scope.AudioCont = function(){
        var req = $http({
                 method: 'POST',
                 url: 'http://language.cs.usm.my/synthesis/read.php',
                 data:{
                     test:"Nama saya ialah Ali"
                 }
        })
        .then(
            function (response) {
            alert("The data has been posted");
            console.log(response);
        },
        function () {
            alert("Failed to post!");
        })
    }

How can i replace the content in the existing textarea with my data? Thanks

Upvotes: 0

Views: 91

Answers (3)

Alex
Alex

Reputation: 3

I used ajax method to solve this problem with auto submit the form when i perform the POST action. Below is my code and solution:

$.ajax({
        type: 'POST',
        url: 'your url',
        data: {'submit': 'submit', 'malayText' : "data that wish to POST"}, // you can use as much as data you want to send,
        dataType: 'JSON' // so you can use the json_encode php function
        });

Upvotes: 0

Simon H
Simon H

Reputation: 21005

You need to use ng-model

 <textarea name="malayText" rows="4" cols="100" ng-model="malayText">

and then you can readily access it in your controller

var req = $http({
             method: 'POST',
             url: 'http://language.cs.usm.my/synthesis/read.php',
             data:{
                 test: $scope.malayText
             }

Upvotes: 1

AlainIb
AlainIb

Reputation: 4728

you have to add ng-model on the input or textarea

  <form method="post" action="">
     <p>Key in sentences in Malay. </p> 
     <textarea name="malayText" rows="4" cols="100" ng-model="yourtextarea"></textarea>
     <input type="submit" value="OK" name="submit" />
  </form>

and in the controller get him with $scope.yourVarName

$scope.yourtextarea ;
$scope.AudioCont = function(){

    var req = $http({
             method: 'POST',
             url: 'http://language.cs.usm.my/synthesis/read.php',
             data:{
                 test:$scope.yourtextarea
             }
    })
    .then(
        function (response) {
        alert("The data has been posted");
        console.log(response);
    },
    function () {
        alert("Failed to post!");
    })
}

Upvotes: 1

Related Questions