Slim return on the same page of a POST submission

Asked

Viewed 318 times

1

I’m sending a request post to a URL, but I need to return to the same page a message or alert saying if the ID is already registered or not.
I already have the query and method to do this, but I am unable 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);
    });
});

Method of receiving Slim:

$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;
});
  • Ué, you are sending the request by ajax no need to redirect. Just give an echo in the json, but first you need to put the header header('Content-type: application/json; charset=utf-8'); (attention to the charset, by the way, that can be another)

  • Daniel I’m sending to Slim by ajax, but when I fall in the method of slim I can not get the answer, I want to have some answer saying that the user already exists but when it falls in I he gets the white screen, not redirect to nowhere

  • barter return json_encode($retorno); for echo json_encode($retorno);. See if it solves

  • So I can get this return, but if I put echo json my header to redirect does not work, I need something to answer the POST to the same page understand ? needed a return of the post sending saying that the code is already registered.

  • But can’t you just return a JSON saying it’s already registered? You’ve already done the check and such... You can use a include, or a class method to avoid code duplicity. By the way, remember that the function header() always need to come before you echo anything, because it just moves the header of the request.

  • Incidentally, a redirect header is just handled on the browser side, you would have to pick up the response headers and make a new Ajax based on redirect, which is counterproductive from my point of view.

  • actually yes I wanted to return this JSON saying that it is already registered, the problem is how to do this redirecting to the same page ? and pick up with ajax to send to form ?

  • If you send by Ajax, it does not reload the page, only returns there to javascript what you send from the server. If you want to send a new request based on the previous one, you can read the answer headers, then you need to read the jqXHR object, follow the documentation of the $.ajax(), search for the property success: http://jqapi.com/#p=jQuery.ajax

  • I managed to catch the return, Thanks he returns on variable date, and I treat the JSON

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.