doforumda Posted February 14, 2010 Share Posted February 14, 2010 hi i have html form in which i want to send its data to php file using ajax but the problem i think is in php file because when i use firebug then it displays error in php file which says "Undefined index: comment in php file" here is my code php file <?php $comment = strip_tags($_POST['comment']); //$postId = strip_tags($_POST['postId']); $commentDate = date("Y-m-d"); $commentTime = date("h:i:s A"); echo $comment; ?> html file <!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> and js file 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); } } Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/ Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 first thing <textarea name="textarea" id="comment" cols="45" rows="5"></textarea> should be <textarea name="comment" id="comment" cols="45" rows="5"></textarea> Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012042 Share on other sites More sharing options...
doforumda Posted February 14, 2010 Author Share Posted February 14, 2010 i change it but still the same problem Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012044 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 ok in your php file put print_r($_POST); then in your js put alert(resultData); after function displayComments(resultData) { Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012053 Share on other sites More sharing options...
doforumda Posted February 14, 2010 Author Share Posted February 14, 2010 i did it it shows following Notice: Undefined index: comment in php file Array ( ) Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012056 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 ok so we are now certain that nothing is being posted to your php. IE $_POST is empty So now the task is to find out why that is Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012058 Share on other sites More sharing options...
jl5501 Posted February 14, 2010 Share Posted February 14, 2010 I think I see it $.ajax ({ type: 'POST', url: url, data: 'html', dataType: queryString, success: displayComments }); should be $.ajax ({ type: 'POST', url: url, data: queryString, dataType: 'html', success: displayComments }); Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012060 Share on other sites More sharing options...
doforumda Posted February 14, 2010 Author Share Posted February 14, 2010 yes it is working now thanks jl5501 Link to comment https://forums.phpfreaks.com/topic/192021-how-to-send-html-form-data-to-php-file/#findComment-1012074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.