Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    Philippines
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JQuery post returns everything.

    Hi all, I simply want to post what was entered in a textarea in a div I have just below it but when I fire the post ajax function it returns everything, and I mean the entire site. I don't understand why, can somebody help me please? This is the code I have:
    [code]
    jQuery(document).ready(function() {
    $('#commentPad textarea').focus();
    $('#commentPad textarea').keyup(function(e) {if(e.keyCode==13) saveComment();});

    function saveComment() {
    $.post(
    'index.php/dashboard/saveComment'
    ,{comment:$('#commentPad textarea').val()}
    ,function(data) {$('#comments').html(data);}
    );
    }
    });
    [code]

    HTML is this:

    [code]
    <div id='commentArea'>
    <div id='commentPad'>We'd like to hear from you:<br /><span>(Enter to submit)</span><textarea></textarea></div>
    <div id='commentListBox'>Comments:<br /><div id='comments'></div></div>
    </div>
    [code]

    and my controller php function is this:
    [code]
    public function saveComment() {
    if(!empty($_POST['comment'])) {
    $comment=$_POST['comment'];
    echo "<p>$comment</p>";
    }
    }
    [code]
    Last edited by christopherc; 03-14-2012 at 08:53 AM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,205
    Thanks
    23
    Thanked 605 Times in 604 Posts
    christopherc, AJAX is used when you need to get something off the server. You are getting something off the HTML page so good old javascript and DOM is all that's needed. Here is a simple way of doing it. It's javascript, because I see no reason to complicate things using jquery.
    Code:
    <body>
    <div id='commentArea'>
    	<form id='commentPad'>
    		We'd like to hear from you:<br />
    		<textarea name="stuff" rows="3" cols="25"></textarea><br />
    		<input type="button" name="submit" value="Submit Comments" onclick="gettext();" />
    	</form>
    	<div id='commentListBox'>
    		Comments:<br />
    		<div id='comments'></div>
    	</div>
    </div>
    <script type="text/javascript">
    function gettext()
    {
    	oForm = document.forms['commentPad'];
    	address = oForm.elements["stuff"].value;
    	document.getElementById('comments').innerHTML = address;
    }
    </script>
    </body>
    </html>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •