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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:54:57+00:00 2026-05-24T16:54:57+00:00

Easy points for someone willing to help a beginner. Using JQuery Form and Validation

  • 0

Easy points for someone willing to help a beginner. Using JQuery Form and Validation Plugins to submit a form with PHP and create a MySQL record if certain criteria are met. I’m returning the response as JSON and I want the form to slideUp if server-side validation shows OK, or prepend an error message to the div containing the form and leave the form in place if there are errors. I’ve gotten valid JSON responses to echo back using:

<?php

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

if(is_null($errors)) {
    $auser = new User();
    $hshd = sha1($_POST['Pwd']);
    $auser->userName = $_POST['userName'];
    $auser->hshdPwd = $hshd;
    $auser->firstName = $_POST['firstName'];
    $auser->lastName = $_POST['lastName'];
    $auser->email = $_POST['email'];
    $auser->cellPhone = $_POST['cellPhone'];
    $auser->homePhone = $_POST['homePhone'];
    if(!is_null($_POST['school'])) {
    $auser->school = $_POST['school'];
    } else {
        $auser->school = "0";   
    }
    $auser->prelim_role = $_POST['role'];
    $auser->approved = $_POST['approved'];
    if($auser->create()) {
        $abc = 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.");
    } else { 
        $abc = array('response'=>"An unknown error occurred. Please send an email to info@aSite.com describing the error event.");
    }
    echo json_encode($abc);
}

?>

And the pertinent portion of the JS:

submitHandler: function(form) {

$("#frmPrntRgstr").ajaxSubmit({
                                                        dataType: 'json',
                                                        success:    processJson,
                                                        })
                            }
});
function processJson(data) { 
        $("#frmPrntRgstr").slideUp("normal", function() {
            $("#frmPrntRgstrRspns").append(data.response).slideDown("normal");
        })
        }
});

But with this setup, both the errors and success messages have a key of 'response' in the JSON Object. The form will slide up whether or not there are errors. I’m thinking of something analogous to if(array_key_exists) in PHP. So if(array_key_exists('errors', $response)) then just prepend, but if(array_key_exists('success', $response)) append and slide up. Only in JSON.

EDIT:

This seems to be working for now. Thanks to Marc B and citizen conn. Please let me know if there are obvious issues or limitations with this structure.

<?php  header("Content-type: application/json"); ?>

<?php

$errors = array();

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

if(!empty($errors)) {
echo json_encode($errors, JSON_FORCE_OBJECT);
} else {
    $auser = new User();
    $hshd = sha1($_POST['Pwd']);
    $auser->userName = $_POST['userName'];
    $auser->hshdPwd = $hshd;
    $auser->firstName = $_POST['firstName'];
    $auser->lastName = $_POST['lastName'];
    $auser->email = $_POST['email'];
    $auser->cellPhone = $_POST['cellPhone'];
    $auser->homePhone = $_POST['homePhone'];
    if(!is_null($_POST['school'])) {
    $auser->school = $_POST['school'];
    } else {
        $auser->school = "0";   
    }
    $auser->prelim_role = $_POST['role'];
    $auser->approved = $_POST['approved'];
    if($auser->create()) {
        $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);
    } else { 
        $failure = array('error'=>"An unknown error occurred. Please send an email to info@aSite.com describing the error event.");
        echo json_encode($failure);
    }

}

?>

With the JS callback:

function processJson(data) {
    if(data.response) {
        $("#frmPrntRgstr").slideUp("normal", function() {
            $("#frmPrntRgstrRspns").append(data.response).slideDown("normal");
        })
    } else {
        $("#frmPrntRgstr").prepend(data[0].error);
    }
}
});
  • 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-24T16:54:58+00:00Added an answer on May 24, 2026 at 4:54 pm

    If you’re expecting JSON you should use $.getJSON

    function processJson(data) { 
        if(!data.response.errors){
            $("#frmPrntRgstr").slideUp("normal", function() {
              $("#frmPrntRgstrRspns").append(data.response).slideDown("normal");
            })
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An easy jQuery question. I have several identical forms ( except their name )
Is there an easy way in C# to create Ordinals for a number? For
Is it easy to create GLUT pop-up menus for my OpenGL application? If yes,
In this SO question, someone asked for calculating an angle from three points .
The problem is easy: I've a lot of desktop shortcuts which points to a
This should be easy. In MySQL I could just do: select sum(column1*column2) as sum1
I am looking to create an easy java script plugin for my site that
Easy question this time. I'm trying to test whether or not a string does
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
Is there an easy way to iterate over an associative array of this structure

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.