2

Been searching for hours how to get this sorted and I'm on the verge of crying, so figured its time to get some help. It should be easy, but I'm clearly missing something.

I want to get the text from inside a textarea, I'd then perform a MySQL update with the textarea text. I am using PHP and jQuery.

Usually, I'd use $_post in php however, I am using jQuery to display a modal popup. I want the MySQL update code to run, and then the modal popup will display some text saying "Saved". Simple.

All the code is working fine, except I can't get the text. Which is pretty crucial obviously.

So how do I get the text from the textarea?

Form:

<form name = "editProfile">
<textarea rows="2" cols="20" name="bio" id="bio">
Some text.
</textarea>
</form>

Php Code

<?php
$bio = TEXT FROM TEXTAREA;

$sql="Update members SET bio = '$bio' WHERE memberID='$memberID'";
$result=mysql_query($sql);
echo ("Profile Update Sucesful");
?>

That bio.text bit is how you'd do it in asp.net....

The above form is not using a method/action, in fact there probably isn't any need for the "form" tags as I only need the textarea. The modal is launched by a link.

Please help (and please be patient with me). If you need any more info, then let me know. Don't shout. I am new at this.

Jason
  • 15,017
  • 23
  • 85
  • 116
Will
  • 73
  • 2
  • 6
  • When using ajax through jQuery you can specify that it be either a POST or a GET. Can you please post your ajax code? – Derek Jan 03 '12 at 13:52
  • How are you transmitting the data to the PHP script? Include you JavaScript code as well. – nikc.org Jan 03 '12 at 13:53
  • How are you sending the value of the textarea? Are you using ajax? What is that code? Plus, there's no point in that last line of code, as it will always echo "Profile update successful". A better option would be to use `if($result) { echo "Profile update successful"; } else { echo mysql_error(); }` in case of error. – Thomas Clayson Jan 03 '12 at 13:53
  • 1
    also big sql injection vulnerability :) – max4ever Jan 03 '12 at 13:55
  • Well, how do you create the modal window in jQuery. If we don't know this, we can't help you. If the content of the window in loaded through AJAX it is no problem to load the php file with passed parameters like `modal_content.php?bio=text` ... the text is easy retrievable with jQuery... – bekay Jan 03 '12 at 13:56
  • I am using this modal tutorial: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial using the "simple window modal". Forget the "form" stuff and sql... an easier question would be, if there was a textarea box on that page. When you click the "simple window modal" link, it displays the text from the textarea on the modal popup? – Will Jan 03 '12 at 15:14
  • Am using this: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial @Derek – Will Jan 03 '12 at 16:08
  • @Will, you need jQuery to do a POST which will submit your contents. Get the form data in a javascript variable then lookup how to do jQuery post (http://api.jquery.com/jQuery.post/). Submit the form to the PHP file which will handle the SQL and use $_POST['formVarOne'] to get the contents of each form field. – Derek Jan 04 '12 at 13:53

3 Answers3

2

To get the text from an textarea in jquery i always use the following:

<textarea id="textareaOfInterest">some value</textarea>
<script>
   $('#textareaOfInterest').val();
</script>

The rest of your code should look something like this:

<textarea id="textareaOfInterest">some value</textarea>
<input type='button' id="doStuff" value="do" />
<script>
   $('#doStuff').live('click',function(){
      show modal window
      var val = $('#textareaOfInterest').val();
      $.ajax({
        ....
      data: "&textArea="+val,
      success: function(result)
              {
            ... do stuff with the result....

                 hide modal window
                }
      });

});
</script>

search examples with jquery ajax because I don't know the syntax by heart.

Hope it helps, regards, Alex

EdChum
  • 376,765
  • 198
  • 813
  • 562
0
 $bio = $_GET['bio']; /* or $_POST if your send data with jQuery $.POST */

is what you need to load the value of the textarea

note that you should specify anyway a send method in your form and, for the sake of application security, send a some kind of hidden token against CSRF request and before any data manipulation (like the db insertion) you should seriously sanitize every input you receive

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
0

Make an AJAX call from your form:

<!-- #dialog is the id of a DIV defined in the code below -->
<a href="#dialog" name="modal">Simple Modal Window</a>

<div id="boxes"> 

    <!-- #customize your modal window here --> 
    <div id="dialog" class="window">
       <textarea rows="2" cols="20" name="bio" id="bio">
           Some text.
       </textarea>
       <input id="saveTxt" type="submit" value="Save" />

       <!-- close button is defined as close class -->
       <a href="#" class="close">Close it</a>

    </div>

    <!-- Do not remove div#mask, because you'll need it to fill the whole screen --> 
    <div id="mask"></div>
</div>

And as your Javascript:

<script>

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });   

    //if submit is clicked
    $('input#saveTxt').click(function(event) {
       event.preventDefault();

       //Get the text from your textarea
       var text = $('textarea#bio').val();

       //Post it to your PHP page to insert it into your database
       $.ajax({
          url: "yourpage.php",                  //The URL you are posting to
          type: "POST",                         //The way you post your data
          dataType: "html",                     //The type of data you expect to get back
          data: {text : text},                  //Your data (thus the value of your textarea)
          cache: false,
          success: function(html) {             //After the AJAX call was done, update the textarea with the HTML you got back
             $('textarea#bio').val(html);
          }
       });
   });      

});

</script>

In yourpage.php (which is the page you are posting your data to) you can then simply fetch it:

<?php
   $bio = mysql_real_escape_string($_POST['text']);

   //do your query
   $sql = "Update members SET bio = '$bio' WHERE memberID='$memberID'";
   $result = mysql_query($sql);
   echo ("Profile Update Sucesful");
?>
Jules
  • 7,148
  • 6
  • 26
  • 50
  • Am using this: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial – Will Jan 03 '12 at 16:07
  • So put my form inside `
    ...
    ` and put my `$('input[type="submit"]').click...` inside the `$(document).ready(function() { ... }`. Then simply make the ajax call like I posted to **yourpage.php** which contains the PHP code you posted above and there you fetch `$bio` as I told you in my answer. I updated my answer which can now basically just be copy-pasted.
    – Jules Jan 03 '12 at 19:22