Cassidy
Cassidy

Reputation: 3365

Post Javascript to PHP file?

So I have this problem.

I have the following form on my webpage:

  <form>
 <textarea rows="3" id="pasteText"> </textarea>
 <button class="btn btn-success btn-block" type="submit" id="paster" onclick="captureText();">Paste!</button>

And I have a JavaScript method to capture the text in the textarea here:

function captureText() {
var value = $("#pasteText").val();

//var sent = "../../lib/_add.php?data="+value;
$.post('../../lib/_add.php', {data: value});
}

I'm trying to send the value inside the textarea to the data in PHP (the add.php adds the data variable to my database). What's the best way to go about it? Everything I've tried so far hasn't worked.

Thanks!

Upvotes: 0

Views: 799

Answers (3)

srs
srs

Reputation: 68

    function captureText() {
    var value = $("#pasteText").val();

    //var sent = "../../lib/_add.php?data="+value;
    $.post('../../lib/_add.php', {data: value})

    .done(function(data) {
                 alert("Data Loaded: " + data);
                          //  add some result confirmation code from php

                     });

    alert("data received");
   }

check jquery path and php file path

Upvotes: 0

Subedi Kishor
Subedi Kishor

Reputation: 5996

You are using input type "Submit". So you have to return false from your function if you want to continue uisng ajax to submit form. In this case your javascript would look like:

function captureText() {
    var value = $("#pasteText").val();
    //var sent = "../../lib/_add.php?data="+value;
    $.post('../../lib/_add.php', {data: value});
    return fasle;
}

Otherwise a simple form works fine.

Enjoy!.

Upvotes: 1

Antony
Antony

Reputation: 15114

You don't need jQuery $.post to submit the form. A simple HTML form would do.

<form action="../../lib/_add.php" method="post">
<textarea rows="3" name="data" id="pasteText"></textarea>
<input class="btn btn-success btn-block" id="paster" type="submit" value="Paste!" />
</form>

Upvotes: 3

Related Questions