views:

199

answers:

2

UPDATE

this is the screenshot of the alert:

alt text

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($comment);
//$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>
+2  A: 

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

munch
it alerts the firebug console text complete with <html><body> tags. doesnt help much.
amit
i didnt follow your EDIT part. cqn you please elaborate?
amit
when you use AJAX, the javascript makes a request to your (PHP) server, your server does the processing, and optionally hands information back to the javascript. The js is responsible for handling the information returned by the browser. The information you're seeing in firebug and the alert() is what the browser is receiving from your server. It's up to the callback function in the $.post to handle the data that is passed back to it.
munch
You're probably not going to want to have the `<html>` and `<body>` tags in the html that's being returned to the browser
munch
the mysql connection is fine. if u notice in the code i posted, i am making 2 SQL queries. the first query is through the GET process. it works fine. the second query is through the POST method. its not working. because it has nothing to insert into the DB. the $connect is empty.
amit
believe me i dont. but they are coming. how to remove them? the javascript is alert-ing the text fine. the text just doesnt seem to be going into the variable.
amit
to clear the above: the text just doesnt seem to be going into the variable in the user_submit.php file.
amit
Do you have an auto-incrementing id in that table? Is a new row being inserted at all? If so, what's the value that's being inserted in the comments.comment row?
munch
No it is there, otherwise you would not see the text in Firebug because that is the result of your `echo $comment` in the php file;
prodigitalson
i dont have an incrementing id in the table. if u see the updated code i pasted, there are now 2 sets of data inserted in to the "comments" table. the 'aid' row is being added to the DB just fine.
amit
What's the data type of the MySQL column comments.comment? It's a silly mistake to have it accidentally be an int or something, but I've seen it happen
munch
Also, just fyi, you want to use `mysql_real_escape_string()` rather than `htmlspecialchars()` on the `$_POST['comment']`, but you want to do it after you've made the database connection because the connection is one of the parameters that gets passed to the function
munch
If you echo the query strings `$query` and `$cadd`, what the output in firebug?
munch
+1  A: 

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.

prodigitalson
i didnt get you. why doesnt that go to php page with $.post?and how do i make it go?
amit
NOTHING is wrong with you PHP in terms of getting the post value - Firebug demonstrates that. Follow my instructions or Munch's for echoing the response message in the page youre posting from.
prodigitalson
even with the above example, things have not improved. this really is killling me..
amit
i have posted the updated user_submit.php. this now even updates text in ajax. it is still unable to print the comment.
amit
@amit: see my most recent edit to my answer (@top).
prodigitalson
i have updated my post to include the HTML as you asked. please take a look.
amit
question updated with screenshot.
amit
+1 for the effort!
Shadi Almosri
thank you so much for your help. it was the SQL after all.
amit