AJAX - Comment Box with [ Javascript jQuery JSON PHP MySQL ] - Step by Step How to Guide - Part 13

preview_player
Показать описание
In this tutorial we do some server side validation about the data which has been sent back.

Important is that we only allow a post request to access this file data. So we check to see if a post request has been sent back. If not then we simply redirect the user back to the home page by using the header function in php.

Within the callback function of ajax, we have sent back the task=comment_insert. We will use this information to validate if this was a post request.

PHP provides us with the isset function which checks to see if a variable is actually set and available to be used. If this $_POST['task'] is set then we can check to see if the task == to comment_insert. If yes, then we will proceed, if not then we dont do anything at all as we didnt send this information back.

As we havent yet made the database and tables, we simply make a object of the information needed and send it back in JSON for. This basically is a string representation of a array or object, which can then be transmitted over the web, to be decoded back in to an object on the other side. Its also as way for different programming languages to communicate with another using the same standards.

With the information now in object form we can insert all of this data into the html which we will insert into the comment list.
Рекомендации по теме
Комментарии
Автор

great. Thank you very much for this tut.

nemanjastankovic
Автор

You could just use then nl2br function to get the correct line breaks :)

earthsbrawler
Автор

 I have figured out that it is not the JSON that is the problem.  It is the "defines" page that stops the JSON.  I have gone into MYSQL and have figured out how to query the data base with right code that will talk to my database and get the right connection.  I encourage anyone working on this to keep at it and take the time to understand the lingo and how the code works.  It is a huge learning curve.  I am dedicated to making this box a success and using it on my site. 

jeannetteantry
Автор

Hi, someone can help me, i have the same problem :

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

return window.JSON.parse( data );

 Thank you very much!

andreescudragos
Автор

to solve the error. You should use this below code instead of $.post in comment_insert.js page

$.ajax({
type: "POST",
url: "ajax/comment_insert.php",
data: {
task : "comment_insert",
userId : "_userId",
comment : _comment
},
success : function(data)
{
comment_insert( jQuery.parseJSON(data));
console.log("ResponseText " + data);
}
});

RegionalCityHunter
Автор

The parseJSON is giving me an error  Uncaught SyntaxError: Unexpected token A In jquery and i have this warning as well event.returnValue is deprecated. Please use the standard event.preventDefault() instead.  can u help me please ?

Umar_Mharmeh
Автор

on comment_insert( jQuery.parseJSON(data) ) I get "Invalid Character Error"

yosmomasbli
Автор

Anyone found out how to solve the JSON problem?

henrikmller
Автор

dude i watched it so far,
i wanna like get your version and make some changes to it
can you make me a .zip to download?

master-azazel
Автор

Uncaught ReferenceError: jquery is not defined 


solicrime
Автор

Uncaught SyntaxError: Unexpected token d

samha
Автор

Uncaught ReferenceError: jquery is not defined 


solicrime
visit shbcf.ru