Display an error message with Ajax
Solved
BlaBiks
Posted messages
22
Status
Membre
-
jordane45 Posted messages 30426 Registration date Status Modérateur Last intervention -
jordane45 Posted messages 30426 Registration date Status Modérateur Last intervention -
Hi everyone,
I have a small question about Ajax. I created a chat that sends messages via Ajax to my send.php document.
But the problem is that I can no longer display error messages with echo "..."; since the page no longer refreshes. I would like to know how to display error messages. I was told to make an Ajax request, but I don't really handle that at all. Thank you all, I also want to point out that I can't check with jQuery if everything is fine since I have quite a few features on it.
I have a small question about Ajax. I created a chat that sends messages via Ajax to my send.php document.
But the problem is that I can no longer display error messages with echo "..."; since the page no longer refreshes. I would like to know how to display error messages. I was told to make an Ajax request, but I don't really handle that at all. Thank you all, I also want to point out that I can't check with jQuery if everything is fine since I have quite a few features on it.
3 réponses
Better..
However:
The first js code ... (used with the second code (php)) is used to add messages to the database... okay.
But your third code ... the one you want to be able to use via ajax... how is it triggered?
When a button is pressed?
In that case, you'll need to put an ONCLICK on that button (instead of doing a submit) to call a javascript function that will, via ajax, call a php file containing the following script:
On the javascript ajax side
However:
The first js code ... (used with the second code (php)) is used to add messages to the database... okay.
But your third code ... the one you want to be able to use via ajax... how is it triggered?
When a button is pressed?
In that case, you'll need to put an ONCLICK on that button (instead of doing a submit) to call a javascript function that will, via ajax, call a php file containing the following script:
<?php //Display php errors error_reporting(E_ALL); // Don't forget to include your database connection file require_once 'cnxBdd.php'; //proper retrieval of variables $users = isset($ban['1']) ? trim(addslashes($ban['1'])) : NULL; $result = array(); //Initialization of the output variable if($users){ $sql = 'SELECT * FROM users WHERE username = :username'; $datas = array(':username' => stripslashes($users)); try{ $infoUsersReq = $db->prepare($sql); $infoUsersReq->execute($datas); $infoUsers = $infoUsersReq->fetch(); }catch(Exception $e){ //in case of error in the query $result['error'] = $e->getMessage(); } if($infoUsers){ if($username != $infoUsers['username']) { if($infoUsers['grade'] != 'administrateur') { if($infoUsers['chatban'] == '0'){ $banUsersChatReq = $db->prepare('...'); $banUsersChatReq->execute(array(...)); // HERE WE CHANGE THE STATUS OF THE MEMBER TO BAN HIM } else { $result['message'] = "The member is already banned!"; } } else{ $result['message'] = "You cannot ban an administrator!"; } } else{ $result['message'] = "You cannot ban yourself!"; } } else { $result['message'] = "The member does not exist!"; } } //Convert to JSON for use in javascript echo json_encode($result); On the javascript ajax side
function ban(user){ // data to be sent to the ajax php script var data = {user:user}; //ajax call $.ajax({ type: "POST", url: urlAjx, data: data, async: async, dataType: "json", success: function(reponse){ // response is a json object containing the info returned by the php page console.log(reponse); //display the result in the browser console. var error = (typeof(reponse.error) !='undefined' && reponse.error!=null) ? reponse.error : null; if(error){ alert("Error executing the query! " + error); } var message = (typeof(reponse.message) !='undefined' && reponse.message!=null) ? reponse.message : null; if(message){ alert(message); } }, error:function(jqXHR, textStatus){ alert('error'); console.log(jqXHR); //display in the browser console } }); }
Hello,
Without seeing your code... IMPOSSIBLE to help you.
However... you talk about doing an echo... but... in ajax... it's with JAVASCRIPT that you display the information....
As for having an ajax example, here is one (that uses JQUERY): https://forums.commentcamarche.net/forum/affich-33258760-remplir-un-formulaire-dynamiquement-en-fonction-d-une-combobox#2
--
Sincerely,
Jordane
Without seeing your code... IMPOSSIBLE to help you.
However... you talk about doing an echo... but... in ajax... it's with JAVASCRIPT that you display the information....
As for having an ajax example, here is one (that uses JQUERY): https://forums.commentcamarche.net/forum/affich-33258760-remplir-un-formulaire-dynamiquement-en-fonction-d-une-combobox#2
--
Sincerely,
Jordane
But what do you want to see? It's the only thing that can be useful to you https://prnt.sc/b9rbx9 ..
Can you, as I have already asked you, POST the code directly on the forum instead of putting images?
(the same two pieces of code will be fine).
But... what kind of error do you want to display? I can't find a match between the given example (the grade = administrator...) and the two pieces of code you just provided us...
(the same two pieces of code will be fine).
But... what kind of error do you want to display? I can't find a match between the given example (the grade = administrator...) and the two pieces of code you just provided us...
and I'm trying to adapt that tomorrow :)
there's nothing left to do
..
No, you can definitely just make one single file... but in which you pass parameters to choose the code you want to execute... (Using an IF or a SWITCH CASE)