Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:07:18+00:00 2026-05-17T18:07:18+00:00

What I want to do is alert() an error message to the user if

  • 0

What I want to do is alert() an error message to the user if my PHP script returns an error. Can someone tell me how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:07:18+00:00Added an answer on May 17, 2026 at 6:07 pm

    EDIT:

    So what i would do is simply use json and return a 200 response with a message for example

    PHP

      $validationErrors = array();
    
      foreach($data as $name => $value){
        // validate each value, if its not valid append a message to the $validationErrors array
        // like $validationErrors[$name] = $message; 
      }
    
      if(!empty($validationErrors)){
         // send json headers so jquery can parse properly
         header('Content-type: application/json');
         echo json_encode('valid'=>false, 'errors' => $validationErrors));
         exit;
      } else {
        // send json headers so jquery can parse properly
        header('Content-type: application/json');
        echo json_encode(array('valid'=>true));
        exit;
      }
    

    jQuery:

      $.ajax({
          url: '/url/for/whatever.php',
          data: {'my': 'data'},
          dataType: 'json',
          success: function(response){
             if(!response.valid){
                $.each(response.errors, function(name, msg){
                    var errmsg = $('<div class="error"></div>').text(msg);
                    $('[name='+name+']').before(errmsg);
                });
                alert('Errors were found!'); 
             }
          }
        });
    

    This way you can also display the errors for each field allowing the user to correct them. Youll probably need to modify my error message insertion but you get the idea.


    Assuming you mean using $.ajax you would simple assign an error function:

    $.ajax({
      url: '/url/for/whatever.php',
      data: {'my': 'data'},
      error: function(){
        alert('An error occurred!');
      }
      success: function(response){
         // whatever happens if the response is successful
      }
    });
    

    However for the error function to be invoked the server must respond with 400 or 500 range error code. IF for example you are handling the errors in your php and then outputting an error message then presumable you would be sending a 200 response so the success function would still be invoked. You can manipulate this by setting the header manually with the php header() function if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use WatiN to verify the error message in a JavaScript alert
I want to display script errors in a popup alert instead of showing them
When you want to alert a user of something once (one time notes about
When someone double clicks on the entire row, I want an alert. How do
I want to enable an UIAlertView-Request before the user can delete an item from
How do I get error message from webmethod, I want to initiate error. Also,
I have a JavaScript function code where I want to alert. function msgalert(x,y) {
I have a JavaScript function code where I want to alert. function msg(x,y) {
I do not want TortoiseSVN to alert me with sounds - e.g. when it
Mobile browsers. Jquery dialog alert box. I don't want to use alert() because it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.