doforumda Posted February 14, 2010 Share Posted February 14, 2010 I have html form and i want to send data from this form to php file using ajax. currently i am having problem with this which is when i submit data it displays error which says undefined variable comment in php file. this html form <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script> <script src="js/script.js" type="text/javascript"></script> </head> <body> <form id="form1" name="form1" method="post" action=""> <p> <label> <textarea name="textarea" id="comment" cols="45" rows="5"></textarea> </label> </p> <p> <label> <input type="button" name="button" id="button" value="Submit" onclick="postcomment()" /> </label> </p> </form> </body> </html> this is js function postcomment() { var url = 'comments.php'; //var queryString = $("#commentForm").serialize(); var comment = $('#comment').val(); //var postId = $('input#postId').val(); var queryString = 'comment=' + comment;// + '&postId=' + postId; alert(queryString); $.ajax ({ type: 'POST', url: url, data: 'html', dataType: queryString, success: displayComments }); function displayComments(resultData) { $('#displayComments').html(resultData); } } and this is php <?php $comment = strip_tags($_POST['comment']); //$postId = strip_tags($_POST['postId']); $commentDate = date("Y-m-d"); $commentTime = date("h:i:s A"); echo $comment; ?> please tell me what am i doing wrong in my code Link to comment https://forums.phpfreaks.com/topic/192012-need-help-in-sending-html-form-data-to-php-file-using-ajax/ Share on other sites More sharing options...
siwelis Posted February 17, 2010 Share Posted February 17, 2010 where it says in HTML <textarea name="textarea" id="comment" cols="45" rows="5"></textarea> try changing that to <textarea name="comment" id="comment" cols="45" rows="5"></textarea> Link to comment https://forums.phpfreaks.com/topic/192012-need-help-in-sending-html-form-data-to-php-file-using-ajax/#findComment-1013523 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.