Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- php is not handling submitted data [AJAX / jQuery submission]
- <div id="content">
- <h2>Some Content</h2>
- <p>Lorem ipsum dolor...</p>
- </div>
- <div id="comments">
- <h2>Reader Comments</h2>
- </div>
- <div id="leaveComment">
- <h2>Leave a Comment</h2>
- <div class="row"><label>Your Name:</label><input type="text"></div>
- <div class="row"><label>Comment:</label><textarea cols="10" rows="5"></textarea> </div>
- <button id="add">Add</button>
- </div>
- //add click handler for button
- $("#add").click(function() {
- //define ajax config object
- var ajaxOpts = {
- type: "post",
- url: "addComment.php",
- data: "&author=" + $("#leaveComment").find("input").val() + "&comment=" + $("#leaveComment").find("textarea").val(),
- success: function(data) {
- //create a container for the new comment
- var div = $("<div>").addClass("row").appendTo("#comments");
- //add author name and comment to container
- $("<label>").text($("#leaveComment").find("input").val()).appendTo(div);
- $("<div>").addClass("comment").text($("#leaveComment").find("textarea").val()).appendTo(div);
- }
- };
- //alert("clicked!");
- $.ajax(ajaxOpts);
- });
- <?php
- //db variables
- $database = "the name of my database";
- $hostname = "localhost";
- $username = "username";
- $password = "password";
- //make connection
- $server = mysql_connect($hostname, $username, $password);
- $connection = mysql_select_db($database, $server);
- //get POST data
- $name = mysql_real_escape_string($_POST["author"]);
- $comment = mysql_real_escape_string($_POST["comment"]);
- //add new comment to database
- mysql_query("INSERT INTO comments VALUES(' $name ',' $comment ')");
- ?>
Add Comment
Please, Sign In to add comment