Guest User

Untitled

a guest
Jul 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. php is not handling submitted data [AJAX / jQuery submission]
  2. <div id="content">
  3. <h2>Some Content</h2>
  4. <p>Lorem ipsum dolor...</p>
  5. </div>
  6. <div id="comments">
  7. <h2>Reader Comments</h2>
  8. </div>
  9. <div id="leaveComment">
  10. <h2>Leave a Comment</h2>
  11. <div class="row"><label>Your Name:</label><input type="text"></div>
  12. <div class="row"><label>Comment:</label><textarea cols="10" rows="5"></textarea> </div>
  13. <button id="add">Add</button>
  14. </div>
  15.  
  16. //add click handler for button
  17. $("#add").click(function() {
  18.  
  19. //define ajax config object
  20. var ajaxOpts = {
  21. type: "post",
  22. url: "addComment.php",
  23. data: "&author=" + $("#leaveComment").find("input").val() + "&comment=" + $("#leaveComment").find("textarea").val(),
  24. success: function(data) {
  25.  
  26. //create a container for the new comment
  27. var div = $("<div>").addClass("row").appendTo("#comments");
  28.  
  29. //add author name and comment to container
  30. $("<label>").text($("#leaveComment").find("input").val()).appendTo(div);
  31. $("<div>").addClass("comment").text($("#leaveComment").find("textarea").val()).appendTo(div);
  32. }
  33. };
  34. //alert("clicked!");
  35. $.ajax(ajaxOpts);
  36. });
  37.  
  38. <?php
  39.  
  40. //db variables
  41.  
  42. $database = "the name of my database";
  43. $hostname = "localhost";
  44. $username = "username";
  45. $password = "password";
  46.  
  47. //make connection
  48. $server = mysql_connect($hostname, $username, $password);
  49. $connection = mysql_select_db($database, $server);
  50.  
  51. //get POST data
  52. $name = mysql_real_escape_string($_POST["author"]);
  53. $comment = mysql_real_escape_string($_POST["comment"]);
  54.  
  55. //add new comment to database
  56. mysql_query("INSERT INTO comments VALUES(' $name ',' $comment ')");
  57.  
  58. ?>
Add Comment
Please, Sign In to add comment