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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:59:16+00:00 2026-06-05T06:59:16+00:00

I have seen a lot of form validation tutorials with jquery ajax. I found

  • 0

I have seen a lot of form validation tutorials with jquery ajax. I found same thing that all are only telling only sucessfull form validation. I need to find how can i display errors if the server side validation fails. in other words, when we submit a form without ajax the form is re-populated with error messages And the inputs filled user provided. In this method we use some great php functions like form_prep. Here is view code.

<?php // Change the css classes to suit your needs    
$attributes = array('class' => '', 'id' => 'login');
echo form_open(SITE_URL.'user/insertUser', $attributes); 
?>

<p>
<label for="username">User Name <span class="required">*</span></label>
<input type="text" name="username" maxlength="255" value="<?php echo form_prep($this->input->post('username'));?>"  />
<span id="username"><?php echo form_error('username'); ?></span><br />
</p>
<p>
<label for="first_name">First Name <span class="required">*</span></label>
<input type="text" name="first_name" maxlength="255" value="<?php echo form_prep($this->input->post('first_name'));?>"  />
<span id="first_name"><?php echo form_error('first_name'); ?></span><br />
</p>

<p>
<label for="last_name">Last Name <span class="required">*</span></label>
<input type="text" name="last_name" maxlength="255" value="<?php echo form_prep($this->input->post('last_name')); ?>"  />
<span id="last_name"><?php echo form_error('last_name'); ?></span><br />
</p>

<p>
<label for="password">Password <span class="required">*</span></label>
<input type="text" name="password" maxlength="255" value="<?php echo form_prep($this->input->post('password')); ?>"  />
<span id = "password"><?php echo form_error('password'); ?></span><br />
</p>

<p>
<label for="email">Email <span class="required">*</span></label>
<input type="text" name="email" maxlength="255" value="<?php echo form_prep($this->input->post('email')); ?>"  />
<span id = "email"><?php echo form_error('email'); ?></span><br />
</p>


<p>
<?php echo form_submit( 'submit', 'Submit', 'id="submit"'); ?>
</p>

<?php echo form_close(); ?>

You can see here used form_prep($this->input->post(‘username’)) which is helpful for security issue. But when i use the same form to re-populate with ajax on failed validation it does not display the errors nor the input boxes filled. Instead loads the form as if this is the first time this is loaded.
Here is how i using it with ajax.

First in a div i am loading form

$('#userform').load('<?php $this->load->view('user_form')?>');

Now ajax

$(function()
{
    $('#login').submit(function()
    {
        $.ajax(
        {
            type : 'POST',
            data : $(this).serialize(),
            url  : $(this).attr('action'),
            success : function(data)
            {
                if(data == 1)
                {
                    // do something 
                }
                else
                {
                    $('#userform').load('<?php $this->load->view('user_form')?>');
                }
            }
        });
        return false;
    });
});

And finally my controller

function insertUser()
{
    if($this->form_validation->run() == FALSE)
    {
        echo 0;
    }else{
        echo 1;
    }
}
  • 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-05T06:59:18+00:00Added an answer on June 5, 2026 at 6:59 am

    what I would do is render the form in the controller here
    instead of

    echo 0;
    
    just do
    
    $html =   $this->load->view('user_form',true);
    echo $html;
    

    and
    in the ajax function
    replace

    else
    {
        $('#userform').load('<?php $this->load->view('user_form')?>');
    }
    

    with

    else 
    {
        $('#userform').html(data);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a lot of articles on this, but none dealing with jQuery,
I'm new to building jQuery plugins. I have seen and used a lot of
This is a UI element that I have seen a lot lately (in the
I have seen a lot of software that show that which registry has error.
I have seen lot projects following this kind of coding style Any significant advantage
i have seen a lot of screencasts where the author is creating methods with
I have seen a lot of shared hashmap implementations. Here is the specific scenario
Ok. I HAVE seen a LOT of topics which are similar to what I'm
This question seems to have been asked a lot, but I haven't seen an
I have a form that I want to know which control on it has

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.