Slim return on the same page as a POST post

1

I'm sending a post request to a URL, but I need to return a message or alert to the same page whether the ID is already registered or not. I already have the query and method to do this, however I am not able to send the return to the same page.

Shipping method:

$("sendFormCadastroprod").submit(function(event){
    event.preventDefault();
    if (request) {
        request.abort();
    }
    var $form = $(this);
    var $inputs = $form.find("input, select, button, textarea");
    var serializedData = $form.serialize();
    $inputs.prop("disabled", true);
    request = $.ajax({
        url: "http://localhost/projetoecomerce/admin/cadastroprod",
        type: "post",
        data: serializedData
    });


    request.done(function (response, textStatus, jqXHR){

        console.log("Dados enviados");
    });


    request.fail(function (jqXHR, textStatus, errorThrown){

        console.error(
            "Error ao enviar: "+
            textStatus, errorThrown
        );
    });

    request.always(function () {

        $inputs.prop("disabled", false);
    });
});

Slim receiving method:

$app->post('/admin/cadastroprod',function () use ($app) {       
    $sql = new Sql();

    $results = $sql->select("SELECT idproduct FROM tb_products WHERE idproduct = :idproduct",array(

            ":idproduct"=>$_POST["idproduct"]
    ));

    if(empty($results)){
        $products = new Products();

        $products -> insert(    $_POST["idproduct"],
                $_POST["desproduct"],
                $_POST["vlprice"],
                $_POST["vlwidth"],
                $_POST["vlheight"],
                $_POST["vllength"],
                $_POST["vlweigth"],
                $_POST["desurl"]); 
    }else{

        $app->get('/admin/cadastroprod-existente',function () {

        $retorno = array();

        array_push($retorno, array(

                "retorno"=>"code-invalid:already-exists.",
        ));

        return json_encode($retorno);           
        });

    }
    $resposta = "erro";

    header("Location: http://localhost/projetoecomerce/admin/cadastroprod");

    exit;
});
    
asked by anonymous 12.05.2017 / 17:53

0 answers