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
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
– Felipe Sangiorge
barter
return json_encode($retorno);
forecho json_encode($retorno);
. See if it solves– Daniel
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.
– Felipe Sangiorge
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.– Daniel
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.
– Daniel
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 ?
– Felipe Sangiorge
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 propertysuccess
: http://jqapi.com/#p=jQuery.ajax– Daniel
I managed to catch the return, Thanks he returns on variable date, and I treat the JSON
– Felipe Sangiorge