Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-14-2012, 08:48 AM   PM User | #1
christopherc
New to the CF scene

 
Join Date: Jul 2010
Location: Philippines
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
christopherc is an unknown quantity at this point
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..
christopherc is offline   Reply With Quote
Old 03-14-2012, 04:13 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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>
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, jquery, post, return everything

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:59 AM.


Advertisement
Log in to turn off these ads.