0

hy, i have a problem save to the database. Cateory's name to db. Can you help, what's the problem?

VM888:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at handle_result (add:410) at XMLHttpRequest. (add:397)

Category.php

function show_add_new(){
    var show_add_box = document.querySelector(".add_new");
    var category_input = document.querySelector("#category");
    
    if(show_add_box.classList.contains("hide")){
        show_add_box.classList.remove("hide");
        
        category_input.focus();
    }else{
        show_add_box.classList.add("hide");
        category_input.value = "";
    }
}


function collect_data(e){

    var category_input = document.querySelector("#category");
    if(category_input.value.trim() == "" || !isNaN(category_input.value.trim())){
        alert("Please enter a valid category name");
    }

    var data = category_input.value.trim();
    send_data({
        data:data,
        data_type:'add_category'
    });
}

function send_data(data = {}){
    var ajax = new XMLHttpRequest();

    ajax.addEventListener('readystatechange', function(){
        if(ajax.readyState == 4 && ajax.status == 200){
            handle_result(ajax.responseText);
        }
    });

    ajax.open("POST","<?=ROOT?>ajax",true);
    ajax.send(JSON.stringify(data));
}

function handle_result(result){
    if(result != ""){
        var obj = JSON.parse(result);
        
        if(typeof obj.message_type != 'undefined'){
            if(obj.message_type == "info"){
                alert(obj.message);
                show_add_new();
            }
            else{
                alert(obj.message);
            }
        }
    }
}

ajax.php -> Undefined index: error in D:\Honlap\xampp\htdocs\eshop\app\controllers\ajax.php on line 17
{"message":"Category added succesfully","message_type":"info","data":""}

Its said "Category added succesfully" but nothig happening, no datatrasfer to db.

<?php

Class Ajax extends Controller{

public function index(){
    
   $data = file_get_contents("php://input");
   $data = json_decode($data);
    
    if(is_object($data)){
        if($data->data_type == 'add_category'){

            //add new category
            $category = $this->load_model('Category');
            $check = $category->create($data);

            **if($_SESSION['error'] != ""){                      -> this is line 17**
                $arr['message'] = $_SESSION['error'];
                $_SESSION['error'] = "";
                $arr['message_type'] = "error";
                $arr['data'] = "";

                echo json_encode($arr);
            }else{
                $arr['message'] = "Category added succesfully";
                $arr['message_type'] = "info";
                $arr['data'] = "";

                echo json_encode($arr);
            }
        }
    }
}

}

  • The problem is that the Session value you're trying to check doesn't exist. Make sure you've called session_start() earlier in the script and that you've actually defined that field at some point earlier in the user session. If it's uncertain whether it would be populated or not, use `isset` to check before attempting to access it – ADyson Nov 13 '21 at 18:38
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – ADyson Nov 13 '21 at 18:39

0 Answers0