0

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!

3
  • It's probably best to use ajax for this. It's just a function within jquery which you already seem to be using. It's not that hard/scary to use. Commented Mar 12, 2013 at 5:08
  • 1
    Why don't you just give a form action to your php page ? Commented Mar 12, 2013 at 5:09
  • try removing onclick="captureText();" from button tag Commented Mar 12, 2013 at 5:13

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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!.

Comments

0
    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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.