jonhyhvaftn Posted January 20, 2016 at 05:28 PM Report Share #592211 Posted January 20, 2016 at 05:28 PM Boas, tenho o seguinte código: $query = "SELECT * FROM users WHERE user='".$_POST['user']."' AND password='".$_POST['password']."'"; $result = $link->query($query); $yes = $result->num_rows; json_encode({"result": $yes}); $.ajax({ url: "vlogin.php", cache: false, dataType: "json", success: items += result; $("#loginMain").html(items); }); Infelizmente na página index.html que é onde está o ajax nada é retornado, sendo que a variável $yes contem um número. Estou a fazer algo mal? Link to comment Share on other sites More sharing options...
tiago.f Posted January 20, 2016 at 05:39 PM Report Share #592212 Posted January 20, 2016 at 05:39 PM (edited) No sucess tens que colocar uma função. Algo como Success: function (xhr) { console.log(xhr) } Entretanto convém veres que valores o user e a pass teem. No teu pedido Ajax não passas nada por isso quando chega ao php, o POST não terá nada.... Edited January 20, 2016 at 05:40 PM by tiago.f Link to comment Share on other sites More sharing options...
jonhyhvaftn Posted January 20, 2016 at 05:45 PM Author Report Share #592213 Posted January 20, 2016 at 05:45 PM (edited) success: function a(){ items += result; $("#loginMain").html(items); } Adicionei ainda: <body onload="a()"> Nao funciona Sim mas retornaria o numero 0, porque se eu for diretamente ao PHP e fizer um echo $yes; retorna 0 Edited January 20, 2016 at 05:45 PM by jonhyhvaftn Link to comment Share on other sites More sharing options...
tiago.f Posted January 20, 2016 at 06:51 PM Report Share #592215 Posted January 20, 2016 at 06:51 PM (edited) A função não deve ter nome e deve receber (até) 3 parametros. Põe exactamente o código que eu escrevi antes e vê o que aparece na consola.. "success callback option is invoked, if the request succeeds. It receives the returned data(1), a string containing the success code(2), and the jqXHR object(3)."--> http://api.jquery.com/jquery.ajax/ É também é importante: "Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead." Edited January 20, 2016 at 07:01 PM by tiago.f Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted January 20, 2016 at 08:12 PM Report Share #592218 Posted January 20, 2016 at 08:12 PM que PHP é este ? json_encode({"result": $yes}); IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Solution tiago.f Posted January 20, 2016 at 08:53 PM Solution Report Share #592221 Posted January 20, 2016 at 08:53 PM O que o hhh quer dizer, construtivamente, é que devias ter $arr["result"]=$yes; echo json_encode($arr); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now