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 6359473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:31:24+00:00 2026-05-24T23:31:24+00:00

I have an HTML form that submits to a PHP file for MySQL insertion

  • 0

I have an HTML form that submits to a PHP file for MySQL insertion using the Jquery Form Plugin’s .ajaxSubmit method. I was having trouble getting error responses from the server if there were errors, and a success response if there were no errors. I’ve got the code working, but I don’t understand why!

Pertinent snippet of the PHP submission file:

$reqs = array('userName', 'Pwd', 'firstName', 'lastName', 'email', 'cellPhone', 'homePhone', 'role');
foreach($reqs as $req) {
    if((!isset($_POST[$req])) || (empty($_POST[$req]))) {
            $newerr = "The field " . $req . " is required.";
            $errors[] = $newerr;
    }
}
if(!empty($errors)) {
echo json_encode($errors, JSON_FORCE_OBJECT);
} else {

then create the MySQL record and

$success = array('response'=>"Request successfully submitted. Your account must be configured before you can access the user panel. Please watch for an email confirming your registration and configuration.");
        echo json_encode($success);

The success function in the .js is:

function processJson(data) {
    if(data.response) {
        $("#frmPrntRgstr").slideUp("normal", function() {
            $("#frmPrntRgstrRspns").append(data.response).slideDown("normal");
        })
    } else {
        for(var error in data) {
        $("#frmPrntRgstr").prepend(data[error]);
        }
    }
}

This does what I want it to, and I understand the first part. There is only a 'response' key in the JSON object if it’s gotten past error checking. Firebug shows this as the server Response:

{"response":"Request successfully submitted. Your account must be configured before you can access the user panel. Please watch for an email confirming your registration and configuration."}

I don’t understand why the syntax 'data[error]' or simply 'data' works for reading back the errors. The response shown in Firebug is:

{"0":"The field cellPhone is required.","1":"The field homePhone is required."}

The JSON shown in Firebug is:

0        "The field cellPhone is required."

1        "The field homePhone is required."

It doesn’t say error in the key, and it doesn’t define a new “Errors” object. So why does that Javascript allow the definition `’data[error]’` if there’s neither an object nor a key defined as error in the response? I guess `’data’` works because it’s just reading back each value in the returned object.

  • 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-24T23:31:25+00:00Added an answer on May 24, 2026 at 11:31 pm

    JSON encodes the contents of a variable, but does not include the variable’s own name. Nothing says that you have to pass back only a single string via JSON. You can encode an entire message system in a single variable. So encode any messages you want, a sub-variable to say “error occured”, any error message(s) associated with that error condition, etc…

    $msgs = array();
    if (...) {
        $msgs['message'] = 'blah blah blah';
    }
    
    if (...) {
        $msgs['errors'][] = 'error message here';
    }
    
    echo json_encode($msgs);
    

    Then you can have your JS check for any errors with if (data.errors.length > 0) or similar.

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

Sidebar

Related Questions

I have a form that submits parameters using standard HTML controls to a PHP
If have a html form like <form method=POST action=http://.../file.php> <input type=text name=data id=data />
I have a simple html form that submits information with POST function. But when
I have a form that contains an html file browse button ( <input type=file
I have following html form: <form method=post action=> <input type=hidden name=userid id=user value=<?php echo
I have an html input field, such as <form method=post action=process.php enctype=multipart/form-data> <div> <h3>Files:</h3>
I have this code in my HTML form: <form action=cart.php method=post> <input type=hidden value=1
I have a html form that posts to a new page on submit. If
I have a form on an HTML page with multiple submit buttons that perform
I have a basic HTML form that gets inserted into a server side div

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.