Display an error message with Ajax

Solved
BlaBiks Posted messages 22 Status Membre -  
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.

3 réponses

jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
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:
 <?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 } }); } 
1
BlaBiks Posted messages 22 Status Membre
 
I don't have any other Ajax code that retrieves the error message, that's exactly what I don't know how to do ^^

and I'm trying to adapt that tomorrow :)
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
Well, I've given you everything...
there's nothing left to do
..
0
BlaBiks Posted messages 22 Status Membre
 
I can't adapt your code to mine, I'm not going to make several files for each function, huh, but I found another solution, thanks to you! :)
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830 > BlaBiks Posted messages 22 Status Membre
 

I'm not going to create multiple files for each function, huh?

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)
0
BlaBiks Posted messages 22 Status Membre
 
Alright, I'll keep that in mind for next time ;)
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
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
0
BlaBiks Posted messages 22 Status Membre
 
I know that echo is in PHP lol, I was displaying error messages in PHP since the page was refreshing, but since it no longer refreshes, I need to make an Ajax request (I think) to display the error message, but Ajax and I don't really get along, so that's why I'm coming to you for help. :)
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
I’ll say it again... WITHOUT SEEING YOUR CODE... IMPOSSIBLE TO HELP YOU.
0
BlaBiks Posted messages 22 Status Membre
 
But what do you want to see? It's the only thing that can be useful to you https://prnt.sc/b9rbx9 ..
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
Well, what do you want us to tell you more?
0
BlaBiks Posted messages 22 Status Membre
 
But OMG just tell me what I need instead of beating around the bush..
0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 
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...
0
BlaBiks Posted messages 22 Status Membre
 
I'm sorry, I cannot assist with that.
0