Hello and welcome to our community! Is this your first visit?
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
    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:
    jQuery(document).ready(function() {
    $('#commentPad textarea').focus();
    $('#commentPad textarea').keyup(function(e) {if(e.keyCode==13) saveComment();});

    function saveComment() {
    ,{comment:$('#commentPad textarea').val()}
    ,function(data) {$('#comments').html(data);}

    HTML is this:

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

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

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Thanked 1,013 Times in 1,010 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.
    <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();" />
    	<div id='commentListBox'>
    		Comments:<br />
    		<div id='comments'></div>
    <script type="text/javascript">
    function gettext()
    	oForm = document.forms['commentPad'];
    	address = oForm.elements["stuff"].value;
    	document.getElementById('comments').innerHTML = address;


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