Note that there are some explanatory texts on larger screens.

plurals
  1. POgetting data from functions when posting with jquery
    primarykey
    data
    text
    <p>I'm beginning to hate AJax, I'm finding it really difficult to get any kind of useful information back when posting with jQuery.</p> <p>I have a script that adds or removes some info when a button is clicked. The jquery posts to a file which calls a function in a class. This part works, but I cant get a success message back to manipulate the front end. Here is my code.</p> <p>The php works but I keep getting the error JSON.parse: unexpected character which I have googled, but my json looks ok?</p> <p>jQuery</p> <pre><code>$('.fave').click(function(){ var favId = $(this).attr('data-user-fave'); var params = {}; params['fave_id'] = favId; params['fav_flag'] = '1'; $.post('index.php?link=my_applications', params, function(data){ var data = $.parseJSON(data); if(data.message === 'success'){ alert(data.flag); } else{ alert("Fail"); } }); </code></pre> <p>code in file that jQuery posts to </p> <pre><code>$profile = new profile(); if($_POST['fav_flag'] == '1'){ $js = $profile-&gt;fave_user(); echo json_encode($js); } </code></pre> <p>function in class profile</p> <pre><code>function fave_user(){ $query = "SELECT * FROM `favourite` WHERE user_id = '{$_SESSION['loginArr']['user_id']}' AND fave_id = '{$_POST['fave_Id']}'"; $nr = $GLOBALS['DB']-&gt;num_rows($query); if($nr &gt;= 1){ //exists so remove $query = "DELETE FROM `favourites` WHERE user_id = '{$_SESSION['loginArr']['user_id']}' AND fave_id='{$_POST['fave_Id']}'"; $GLOBALS['DB']-&gt;deleteQuery($query); $return["message"]="success"; $return["flag"]="del"; return $return; } else{ //not a fave so add $query = "INSERT INTO `favourite` (user_id, fave_id) VALUES ('{$_SESSION['loginArr']['user_id']}', '{$_POST['fave_id']}')"; $GLOBALS['DB']-&gt;insertQuery($query); $return["message"]="success"; $return["flag"]="ins"; return $return; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload