SVerheijen Posted January 26, 2017 Share Posted January 26, 2017 $.ajax({ type: "POST", url: "aj/write.php", dataType: "html", data: update }).done(function (returned) { var data; data = JSON.parse(returned); message = data.message; if (data.code == 200) { lastChanged = data.users; var params = {}; params.changes = lastChanged; params.newTask = data.new; if (data.new.projectleider == false) { params.organizer = { 'naam': currentUser.naam, 'email': currentUser.email }; } else { params.organizer = { 'naam': data.new.projectleider.naam, 'email': data.new.projectleider.email }; } message = message + " <a href='#' onclick='outlookMail(" + JSON.stringify(params) + ", \"FREE\")'>Mail wijzigingen (Vrij)</a> <b>OF</b> <a href='#' onclick='outlookMail(" + JSON.stringify(params) + ", \"BUSY\")'>Mail wijzigingen (Bezet)</a> <b>OF</b> <a href='#' onclick='outlookMail({}, \"NONE\")'>Melding sluiten (geen mail)</a>"; } else if (data.code == 204) { message = data.message; } $("#notifybar").html(message); $("#notifybar").animate({'height': 'show'}, 400); }); <?php include '../../includes/open_database_beheer.php'; include '../resources/database/MysqliDb.php'; $db = new MysqliDb ($server, $user, $password, $database); // OLD // $queryForCheckInLijst = "SELECT `gebruikersid`, `datum`, `dagdeel`, `legid` FROM `planning_lijst` WHERE `gebruikersid` = ( SELECT ID FROM planning_gebruikers WHERE login = ? LIMIT 1) AND `datum` = ? AND `dagdeel` = ?"; // NEW $queryForCheckInLijst = " SELECT `gebruiker` gebruikersid, `datum`, `dagdeel`, `taak` legid FROM `planning` WHERE `gebruiker` = ( SELECT `id` FROM `gebruikers` WHERE `login` = ? LIMIT 1) AND `datum` = ? AND `dagdeel` = ? "; $return = []; if (!empty($_POST)) { $return['postedIn'] = $_POST; $return['users'] = []; foreach ($_POST as $data) { if ($data['changeto'] == "griditem undefined") { $id = 0; $newName = "Standaardindeling"; $projectLead = false; } else { $id = substr($data['changeto'], 14); $newName = $db->rawQueryValue(" SELECT `naam` FROM `legenda` WHERE `id` = ? limit 1 ", array( ))[0]; $projectLead = $db->rawQueryOne(" SELECT `legenda`.`projectleider`, `gebruikers`.`naam`, `gebruikers`.`email` FROM `legenda` INNER JOIN `gebruikers` ON `legenda`.`projectleider` = `gebruikers`.`id` WHERE `legenda`.`id` = ? ", array( $id )); } if (!isset($return["new"])) { $return["new"] = [ "id" => $id, "name" => $newName, "projectleider" => ($projectLead == null ? false : $projectLead) ]; } /* if ($data['daypart'] == "AM") { $daypart = 0; }; if ($data['daypart'] == "PM") { $daypart = 1; }; if ($data['daypart'] == "NIGHT") { $daypart = 2; }; */ //OLD $daypart = (($data['daypart'] == "AM") ? 0 : 1); //NEW $daypart word een (0 als AM oftewel ochtend, 1 als PM oftewel middag, 2 als NIGHT oftewel avond) switch ($data['daypart']) { case "AM": $daypart = 0; break; case "PM": $daypart = 1; break; case "NIGHT": $daypart = 2; break; default: $daypart = 0; break; } //var_dump ($daypart); die(); $date = $data['date']; //echo '<h1>'.$date.'<h1>'; die(); $CheckInLijstResult = $db->rawQueryOne($queryForCheckInLijst, array($data['uid'], $date, $daypart)); if (!isset($return['users'][$data['uid']])) { $return['users'][$data['uid']] = []; } if (!isset($return['users'][$data['uid']]['changes'])) { $return['users'][$data['uid']]['changes'] = []; } if (!isset($return['users'][$data['uid']]['info'])) { $return['users'][$data['uid']]['info'] = []; $userdata = $db->rawQueryOne(" SELECT `naam`, `email` FROM `gebruikers` WHERE `id` = ( SELECT `id` FROM `gebruikers` WHERE `login` = ? LIMIT 1) ", array( $data['uid'] )); $return['users'][$data['uid']]['info'] = $userdata; } $temp = []; unset($exists); // Warning: This might be a security hole (unsafe query, vulnerable to injection). Unpatched because it's running on the internal network and includes only non-critical data. if (isset($data['changingDefault']) && $data['changingDefault'] == 1) { //OUD WERKT NIET VOOR NIGHT //$field = strtolower(preg_replace("/^(\w{2})([ab])(\w\w)$/", "$1_$2_$3", $data['date'])); //NIEUW WERKT WEL VOOR NIGHT $array = array(); $array = $data['date']; $dag = $array[0].$array[1]; $week = $array[2]; $dag_deel = substr($data['date'],3); $field = $dag.'_'.$week.'_'.$dag_deel; $field = strtolower($field); //OLD //$query = "UPDATE planning_gebruikers SET $field = {$id} WHERE login = ?"; //NEW $query = "UPDATE `gebruikers` SET $field = {$id} WHERE login = ?"; $db->rawQuery($query, array($data['uid'])); } else if (count($CheckInLijstResult) > 0) { $leaveLegendId = $db->rawQueryValue("SELECT `id` FROM `legenda` WHERE `naam` = 'VRIJ' LIMIT 1"); //Als de nieuwe indeling gelijk is aan de standaardindeling van de gebruiker,verwijder de gevonden rij in planing_lijst. if ($id == 0) { //OLD //$queryDelete = "DELETE FROM `planning_lijst` WHERE `gebruikersid` = {$CheckInLijstResult["gebruikersid"]} AND `datum` = '{$CheckInLijstResult["datum"]}' AND `dagdeel` = {$CheckInLijstResult["dagdeel"]}"; //NEW $queryDelete = "DELETE FROM `planning` WHERE `gebruiker` = ? AND `datum` = ? AND `dagdeel` = ?"; $db->rawQuery($queryDelete, array( $CheckInLijstResult["gebruikersid"], $CheckInLijstResult["datum"], $CheckInLijstResult["dagdeel"] )); } else { //OLD //$db->rawQuery("UPDATE `planning_lijst` SET `legid` = ? WHERE `gebruikersid` = (SELECT ID FROM planning_gebruikers WHERE login = ? LIMIT 1) AND `datum` = ? AND `dagdeel` = ?", array($id, $data['uid'], $date, $daypart)); //NEW $db->rawQuery(" UPDATE `planning` SET `taak` = ? WHERE `gebruiker` = (SELECT `id` FROM `gebruikers` WHERE `login` = ? LIMIT 1) AND `datum` = ? AND `dagdeel` = ? ", array($id, $data['uid'], $date, $daypart) ); } if ($data['changeFrom'] == "griditem item_" . $leaveLegendId . " selected") { try { $exists = $db->rawQueryValue(" SELECT `verlof_dagen`.`id` FROM `verlof_dagen` INNER JOIN `verlof` ON `verlof_dagen`.`verlof` = `verlof`.`id` WHERE `verlof`.`gebruiker` = ? AND `verlof_dagen`.`datum` = ? AND `verlof_dagen`.`dagdeel` = ? LIMIT 1 ", array( $CheckInLijstResult["gebruikersid"], $CheckInLijstResult["datum"], $CheckInLijstResult["dagdeel"] )); if (isset($exists)) { $db->rawQuery(" DELETE FROM `verlof_dagen` WHERE `verlof_dagen`.`id` = ? ", array( $exists[0] )); } } catch (Exception $ex) { $return = []; $return['code'] = 500; $return['message'] = "Er is iets mis gegaan met het verwijderen in de tabel verlof_dagen"; echo json_encode($return); return; } } } else { if ($id == 0) { //OLD //$db->rawQuery("DELETE FROM planning_lijst WHERE gebruikersid=( SELECT ID FROM planning_gebruikers WHERE login =? LIMIT 1) AND datum=? AND dagdeel=?", array($data['uid'], $date, $daypart)); //NEW $db->rawQuery(" DELETE FROM `planning` WHERE `gebruiker`=( SELECT `id` FROM `gebruikers` WHERE `login` = ? LIMIT 1) AND `datum` = ? AND `dagdeel` = ? ", array($data['uid'], $date, $daypart)); } else { $legid = substr($data['changeto'], 14); //OLD //$query = "INSERT INTO planning_lijst(gebruikersid, datum, dagdeel, legid) VALUES(( SELECT ID FROM planning_gebruikers WHERE login =? LIMIT 1), ?, ?, ?)"; //NEW $query = " INSERT INTO `planning`( `gebruiker`, `datum`, `dagdeel`, `taak` ) VALUES( ( SELECT `id` FROM `gebruikers` WHERE `login` = ? LIMIT 1), ?, ?, ? ) "; $db->rawQuery($query, array($data['uid'], $date, $daypart, $legid)); } } $temp = array( "date" => $date, "daypart" => $daypart ); array_push($return['users'][$data['uid']]['changes'], $temp); unset($temp); } $return['lastQuery'] = $db->getLastQuery(); $return['code'] = 200; $return['message'] = "Planning bijgewerkt!"; } else { $return['code'] = 204; // "Geen inhoud" $return['message'] = "Er is voor zo ver er gecontroleerd kan worden, niks meegestuurd. Probeer tenminste 1 blokje te selecteren en probeer het dan opnieuw <a href='#' onclick='outlookMail({}, \"NONE\")'>Klik hier om de melding sluiten</a>"; } echo json_encode($return); VM2851:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at Object.<anonymous> (index.php?filter=0&w=10:236) at n (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at w (jquery.min.js:4) at XMLHttpRequest.d (jquery.min.js:4) I don't know why there is an '<' in this error??? I can't find any place where it adds the '<'. Is this just my code or is this something else? Someone knows the solution??? Quote Link to comment https://forums.phpfreaks.com/topic/303034-uncaught-syntaxerror-unexpected-token-in-json/ Share on other sites More sharing options...
Solution maxxd Posted January 26, 2017 Solution Share Posted January 26, 2017 Typically this happens when PHP outputs an error. Check your console and see what the output is from the server. Looking at your $queryForCheckInLijst, it may be a SQL error - it looks like you've added a couple column names without commas. I haven't looked through the rest of the code, but that's a place to start. Quote Link to comment https://forums.phpfreaks.com/topic/303034-uncaught-syntaxerror-unexpected-token-in-json/#findComment-1542004 Share on other sites More sharing options...
SVerheijen Posted January 31, 2017 Author Share Posted January 31, 2017 Typically this happens when PHP outputs an error. Check your console and see what the output is from the server. Looking at your $queryForCheckInLijst, it may be a SQL error - it looks like you've added a couple column names without commas. I haven't looked through the rest of the code, but that's a place to start. Thanks for the answer. The query was right. I asked a friend and he found the problem. Somewhere in the query I forgot to put an variable. So it works fine now. Still thanks for helping ! Quote Link to comment https://forums.phpfreaks.com/topic/303034-uncaught-syntaxerror-unexpected-token-in-json/#findComment-1542136 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.