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

  • SEARCH
  • Home
  • 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 8021223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:55:08+00:00 2026-06-04T21:55:08+00:00

I am submitting a form using ajax, function ajax_post() { // …(code); var hr

  • 0

I am submitting a form using ajax,

 function ajax_post() {
            // ...(code);
        var hr = new XMLHttpRequest();
        var url = "http://domain.com/submit_action.php";
        var vars = "element_1=" + ln + "&element_2=" + fn;
        hr.open("POST", url, true);
        hr.send(vars);
            // ...(code);
}

having the php exc the query:

$sql = 'SELECT *
        FROM ' . table. '
        WHERE ' . $db -> sql_build_array('SELECT', $data);
$result = $db -> sql_query($sql);
$sql = 'INSERT INTO ' . table. ' ' . $db -> sql_build_array('INSERT', $data);
$db -> sql_query($sql);

In my above code, it will run. But when the ‘fn’ and ‘ln’ are the same or already exist in the db, then there will be a error. But because im using ajax to submit it, I stay on the current page of the form without getting a error, without knowing if the query exc or not.

Question is, is there a way to have php tell ajax what kind of error occured during the exc of query? Thanks in advance.

  • 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-06-04T21:55:09+00:00Added an answer on June 4, 2026 at 9:55 pm

    anything echoed during your php script will be returned into the hr.responseXML and hr.responseText attributes.

    Catch these 2 values within your hr.onreadystatechange callback, whenever the status is OK, to know what happened in your php.

    hr.onreadystatechange = function() {
        if (hr.readyState == 4 && (hr.status == 200)) {
            //do something with hr.responseText
        }
    }
    

    I personnaly systematically encapsulate my php answers inside a generic xml response, with a customized status: error/ok/business error, and a customized message, so I know what to do with it at the javascript layer.

    In addition to this, I would suggest you catch the 1062 mysql error to know that the error is due to an already existing value, and raise a more user-friendly message.

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

Sidebar

Related Questions

I'm submitting a form using an ajax request (POST method), and checking the HTTP
I am getting Error in submitting the form using AJAX and Jquery, following is
So I have a form and I'm submitting the form through ajax using jquery
I am submitting a form using ajax and what I need to do is
I am submitting a form in spring g MVC using ajax. The form is
I faced the following issue while I submitting my form using jQuery FORM and
Can I validate a form on button click without submitting the whole form using
Currently I use this strategy: After submitting a HTML form or data sent using
I want to interrupt and prevent a form from submitting using jQuery. Here's my
I have a form on the following page: http://mmicet.yazminmedia.com/qseries (Click the Keep me updated

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.