Jump to content

need help in sending html form data to php file using ajax


doforumda

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.