not able to receive POST data in php file
UPDATE
this is the screenshot of the alert:
i am using the following JQuery function to successfully POST data to the user_submit.php file, its just that the php file is unable to receive the data.
$(function() {
$("#submit_js").click(function() {
$.post("user_submit.php", {
comment: $("#comment").val()
});
});
});
upon finishing the Firebug consoloe shows this:
<html>
<body>
Your vote was successfully registered!any text but this
</body>
</html>
I am pasting the entire user_submit.php file here:
<html>
<body>
<?php
$vote = $_REQUEST['vote'];
$aid = $vote;
//$comment = $_REQUEST['#comment'];
//$comment = print_r($_POST);
$comment = htmlspecialchars($_POST["comment"]);
//$comment = $_POST['comment'];
//echo htmlspecialchars($com开发者_StackOverflow社区ment);
//$comment = $_POST['comment'];
//echo $comment;
// include configuration file
include('config.php');
// open database connection
$connection = mysql_connect($host, $user, $pass) or die('ERROR: Unable to connect!');
// select database
mysql_select_db($db) or die('ERROR: Unable to select database!');
// update vote counter
$query = "UPDATE answers SET acount = acount + 1 WHERE aid = $vote";
$result = mysql_query($query);
$cadd = "INSERT INTO comments (comment,aid) VALUES ('$comment', '$aid')";
mysql_query($cadd);
// close connection
mysql_close($connection);
// print success message
echo 'Your vote was successfully registered!';
echo $comment;
//print_r(array_count_value($comment));
?>
</body>
</html>
please help me out. i have been stuck with this for hours now.
**EDIT: As requested the HTML for the Submit Button **
<div id="greetings">
You are voting out <b style="color: #00b0de;" id=roadiename></b>. Care to explain why?<br/><br/>
<textarea name="textarea" id="comment" cols="38" rows="7">textarea</textarea><br>
<a href="#" id="submit_js"><img src="images/submit.gif" style="border: none; float:right; padding-top: 10px;padding-right: 10px;"/></a>
</div>
try:
$("#submit_js").click(function() {
$.post("user_submit.php", {comment: $("#comment").val()}, function(data){
alert(data);
//this is the function where you would handle the returned text/html/json
//whatever that you're php script returns. The data is what you're seeing
//inside firebug.
});
});
Here's the documentation for $.post
EDIT
The issue is that you're expecting PHP to handle an echo, which it is doing, but because it's an AJAX request, the javascript needs to do something with what the server sends to it. Your javascript did not have any sort of handler in it. PHP was returning the values you told it to, and the browser was getting it, it just wasn't doing anything with it.
As for the database, I would make sure you're actually making a connection to the db and that the variables you're using have something in them. You're also not sending any $_REQUEST['vote']
value to the script in your POST and it's not in your url, so that would be why the first query isn't updating
EDIT:
Ok good the screen shot means everything is getting submitted correctly to your php page. So then as we were suggesting the issue is with your SQL. Since the vote SQL seems to work fine its localized to insert youre trying to perform.
If you could, can you post the sql schema for your "comments" table.
also while youre doing thathave you tried runng the same query with a some test data directly form the commandline or a gui client to the sql server?
for example from the commandline: mysql -u $USER -p $DATABASE INSERT INTO comments (comment,aid) VALUES ('i have something to say!', $AID);
Where $USER
is your username, and $DATABASE
is your dbname... youll be prompted for the password for your user of course.
Now im not sure what your schema looks like so if aid
is a auto-increment you shouldnt be supplying a value, if its pk something arbitrary but unique for $AID
(making sure it fits the column type - ie. int, text, varchar, etc..)
post back your schema and whatever result the test query gives you.
EDIT:
OK I want you to do this and then ammend your question with the output:
use this code for submission (assuming #comment is still the id of the textarea:
$("#submit_js").click(function() {
$.post(
"user_submit.php",
{comment: $("#comment").val()},
function(data){
alert(data);
});
});
Put this in your user_submit.php
and comment out everything else:
print "Request Data is:\n";
print_r($_REQUEST);
print "\n\nPost data is:\n";
print_r($_POST);
Post back what you see in the alert window that pops up.
EDIT:
i have posted the updated user_submit.php. this now even updates text in ajax. it is still unable to print the comment - amit
I think i might know what it is... Exactly what type of element is #comment
?
Can you post the relevant HTML from your submission page?
EDIT:
I think youre confused about how ajax works. Youre assuming it works just like if you submit a form to a plain php page, then that page renders to the browser. This is not what happens. Instead you post to a page and you get a http response back, instead of rendering that response to the browser you get it as a javascript variable. Then you have to do something with that variable - like add it to the page somewhere. The problem is you havent done anything with the response youre getting back.
Ok so...
"@BranTheMan: then why isnt it echo'ing the text? and why isnt it adding anything to the DB? – amit"
Its not echoing anythign in you page because youre not telling it to. What you seen in the firebug console is the response body. That doesnt go into your page with $.post
. It jsut makes the call and give you the response. if you want to echo it in the current page then you need to tell it to do so.
for example with the things you listed above:
$("#submit_js").click(function() {
$.post(
"user_submit.php",
{comment: $("#comment").val()},
function(data){
$('#submit_js').append($('body', data).html());
});
});
As for your SQL i dont know why its not adding properly... it looks fine. Have you checked your logs for any sql errors? Might want to through some error checking on the return from the query.
精彩评论