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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:10:26+00:00 2026-05-26T07:10:26+00:00

In this basic jQuery, AJAX, PHP form I want to display errors next to

  • 0

In this basic jQuery, AJAX, PHP form I want to display errors next to inputs instead of the bottom of the form. I use if(empty(something)) { jQuery here }. Why won’t this work? Whats the best practice to do this? Thank you.

    HTML:
    Name:<input type="text" id="name" /> <span id="name_error"></span>

    <input type="button" value="Update!" id="update" /> <span id="name_error"></span>
    <span id="update_status"></span>

    PHP

    <?php
    include('init.inc.php');
    if(isset($_POST['name'])) {
        $name = mysql_real_escape_string(htmlentities($_POST['name']));

        if(empty($name)) {
            ?>

            // Why wont this work here?  It just outputs the the whole thing as text.  in the update_status div (You can see that in the ajax part at the bottom of the code).

            <script>
                $('#name_error').text('Name required');
            </script>

            <?php

            if(!empty($name)) {
        $query = mysql_query("UPDATE users SET
                                name = '$name'
                                WHERE user_id = ".$_SESSION['user_id']."
                                ");
        if($query === true) {
            echo 'Your settings have been saved';
            } else if($query === false) {
                echo 'Unable to save your settings';
                }
        }
    }

            // This is the jQuery / AJAX part -- no issues here.  Just have it to include both parts.

    $('#update').click(function() {
    var name = $('#name').val();

    $('#update_status').text('Loading...');

    $.ajax({
        type: 'POST',
        url: 'page.php',
        data: 'name='+name,
        success: function(data) {
            $('#update_status').text(data);
            }
        });
    });

CODE UPDATED

  • 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-26T07:10:27+00:00Added an answer on May 26, 2026 at 7:10 am

    Why aren’t you checking for empty before the form submit?

    You can stop the form submission and check for empty values with javascript, if all is clear then you can submit the form.

    You can do this, but you are specifiying .text()

    What you need to do is jQuery(“#update_status”).html(data);

    jQuery("#update").click( function(){
      if(jQuery.trim(jQuery("#name").val()) == ''){ alert("empty"); return false; }
      jQuery.post("page.php", {name:jQuery("#name").val()}, function(html){
        jQuery("#update_status").html(html);  
      });
    });
    

    Note that you PHP page is going to return more than just your intended code as it is now. It is going to try and return the form again also.

    You need to wrap your processing and from in separate if/else statement. Better to put them in two separate files and keep ajax stuff separate.

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

Sidebar

Related Questions

I have a pretty basic jQuery ajax thing happening, but I want to mix
If i was using this basic AJAX jQuery code: $.ajax ({ type: POST, url:
I'm using this jquery to serialize my form and pass it to a PHP
Interested in building my own drag'n'drop file uploader using JQuery/AJAX/PHP. Basically I want a
Hi this is pretty basic jQuery stuff but i am a little lost. So
I'm using a basic registration form with AJAX, but the form is not connecting
Was experimenting with some basic http post stuff with php and ran into this
I have a basic HTML file, using jQuery's ajax, that is connecting to my
I am performing asynchronous HTTP (Ajax) requests via jQuery with the basic $.ajax(). The
I'm trying out this basic authentication using jQuery. But nothing happens when I enter

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.