I use Ajax to get access to my comment_insert.php. In my comment_insert.php i want to call a public static function thats located in another php file(comments_pasta.php
). If i call the function, my program gets an
"Uncaught SyntaxError: Unexpected token < "
And if i delete the call, everything works fine. I'm new to ajax.
function comment_post_btn_click(){
var _comment = $('#comment-post-text').val();
var _userId = $('#userId').val();
var _userName = $('#userName').val();
if(_comment.length > 0 && _userId != null){
console.log(_comment + " " + _userName + " " +_userId);
$.post("/ajax/comment_insert.php",
{
//we use this in the comment_insert.php(AJAX)
task : "comment_insert",
userId : _userId,
comment : _comment
}
).success(
function(data){
//going to turn the Json from comment_insert.php(AJAX)into a javascript object
comment_insert(jQuery.parseJSON(data));
console.log("Response text = " + data);
<?php
session_start();
if(isset($_POST['task']) && $_POST['task'] == 'comment_insert' ){
require_once $_SERVER['DOCUMENT_ROOT'] . 'defines.php';
$userId = (int)$_POST['userId'];
$comment = addslashes(str_replace ("
" , "<br>" , $_POST['comment']));
$std = new stdClass();
$std -> userId = $userId;
$std -> comment = $comment;
$std -> userName = $_SESSION['userName'];
require_once ('comments_pasta.php');
Talk:hej();
echo json_encode($std);
}
?>
<?php
class Talk{
public static function hej(){
console.log("HEJ");
}
}
?>
The reality is that the unexpected syntax error is actually because PHP is throwing an error, and it's formatted error reporting contains something like <p> There was an error! </p>
and it breaks on the <
which also coincidentally invalides your JSON
response to the script. console.log()
is a javascript function. You cannot execute that in PHP.
What you actually should do is echo
or return
that variable and assign it to your object.
public static function hej(){
return "HEJ":
}
Then in your PHP script.
$std -> userName = $_SESSION['userName'];
require_once ('comments_pasta.php');
$std->HEJ = Talk:hej();
Now you can do console.log(data.HEJ)
in your success
function of your $.post()
wrapper.
Furthermore, you are doing comment_insert(jQuery.parseJSON(data));
however, you can have jQuery automatically parse the returned data in the $.post()
wrapper by adding 'json'
as the final argument to the call, please observe:
$.post({ //object
}, 'json'); // <--final line closing $.post() wrapper
And now it's just:
comment_insert(data);
This should resolve it.
try this ajax call
function comment_post_btn_click()
{
var _comment = $('#comment-post-text').val();
var _userId = $('#userId').val();
var _userName = $('#userName').val();
var form_data = new FormData();
form_data.append('_comment',_comment);
form_data.append('_userId',_userId);
form_data.append('_userName',_userName);
$.ajax({
// url : 'userListAjax.php?r='+Math.random();,
url : 'userListAjax.php?r=',
dataType : 'text',
cache : false,
contentType : false,
processData : false,
data : form_data,
// data: {params:[page,display]},
type : 'post',
success : function(data){
// alert(data);
document.getElementById("dynamicContent").innerHTML=data;
}
});
}
well nothing works.. to get the output from comment_insert(jQuery.parseJSON(data)); i simply need to delete the call of the function. if i do a console.log("Response text = " + data); before the comment_insert with the function call i get this:
Response text =
( ! ) Notice: Use of undefined constant console - assumed 'console' in C:\wamp\www\ajax\comments_pasta.php on line 7 Call Stack #TimeMemoryFunctionLocation 10.0000247944{main}( )..\comment_insert.php:0 20.0120262616Talk::hej( )..\comment_insert.php:20
( ! ) Warning: log() expects parameter 1 to be double, string given in C:\wamp\www\ajax\comments_pasta.php on line 7 Call Stack #TimeMemoryFunctionLocation 10.0000247944{main}( )..\comment_insert.php:0 20.0120262616Talk::hej( )..\comment_insert.php:20 30.0210263032http://www.php.net/function.log' target='_new'>log ( )..\comments_pasta.php:7 {"userId":1,"comment":"test commenttext","userName":"alex"}
and if i delete the function call i get what i want in the console.log:
asdasd Alexander Lundh 1 comment_insert.js:18 Response text = {"userId":1,"comment":"asdasd","userName":"alex"}