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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:05:19+00:00 2026-06-15T05:05:19+00:00

In the code below I have two types of validation. I use a javascript

  • 0

In the code below I have two types of validation. I use a javascript validaton which displays the error message for when the user does not enter in anything in the course text input.

Then I have a php validation where that if it does not contain a row for the result from the query which checks to see if there are any assessments within the course the user has typed in the course text input, then it displays a message stating that no assessments are found.

The problem I am having though is that if the user does not enter in anything in the “Course” text input and they click on the submit button, it displays both the javascript validation and the php validation.

This is incorrect, what should happen is that :

  • if the user has not written anything in the course text input, then it should simply show only the javascript validation and NOT the php validation.

    • if the user has written something in the course text input and submits the form, but it then cannot find any results from the query, then it should display the php validation ONLY.

My question is what do I need to change in the code in order to be able to not show both validation messages at the same time and show only the correct validation messages when they should be shown?

In other words how do I stop the form from submitting if the javascript validation fails? And then obviously how do I make sure that if the javascript validation succeeds, then it does submit the form.

Javascript

function validation() {

    var isDataValid = true;

    var courseTextO = document.getElementById("coursesDrop");

    var errModuleMsgO = document.getElementById("moduleAlert");

    if (courseTextO.value == "") {
        $('#targetdiv').hide();
        $('#assessmentForm').hide();
        $('#updateForm').hide();
        $('#submitupdatebtn').hide();
        errModuleMsgO.innerHTML = "Please Select a Course";
        isDataValid = false;
    } else {
        errModuleMsgO.innerHTML = "";
    }
    return isDataValid;

}​

PHP/HTML

<?php

// connect to the database
include('connect.php');


/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
}


$sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseId"; 

$sqlstmt=$mysqli->prepare($sql);

$sqlstmt->execute(); 

$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);

$courses = array(); // easier if you don't use generic names for data 

$courseHTML = "";  
$courseHTML .= '<input type="text" name="courses" id="coursesDrop" />' . PHP_EOL; 

?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
    <table>
        <tr>
            <th>Course: <?php echo $courseHTML; ?></th>
        </tr>
    </table>
    <p>
        <input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" />
    </p>
    <div id="moduleAlert"></div>
    <div id="targetdiv"></div>
</form>


<?php

if (isset($_POST['moduleSubmit'])) {    

    $sessionquery = "
    SELECT SessionId, SessionName, SessionDate, SessionTime, CourseId, SessionActive
    FROM Session
    WHERE (CourseId = ? AND SessionActive = ?)
    ORDER BY SessionName 
    ";

    $active = 1;

    $sessionqrystmt=$mysqli->prepare($sessionquery);
    // You only need to call bind_param once
    $sessionqrystmt->bind_param("si",$course, $active);
    // get result and assign variables (prefix with db)

    $sessionqrystmt->execute(); 

    $sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbCourseId, $dbSessionActive);

    $sessionqrystmt->store_result();

    $sessionnum = $sessionqrystmt->num_rows();   

    if($sessionnum == 0) {
        echo "<p><span style='color: red'>Sorry, You have No Assessments under this Module</span></p>";
    } 
    else 
    { 
        echo "";
    }

    ...

}
?>
  • 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-15T05:05:21+00:00Added an answer on June 15, 2026 at 5:05 am

    Try stopping the submit event thoroughly (you’re suffering from jQuery-itis, causing you to abuse return false):

    method="post" onsubmit="return validation(event);">
    

    And in JS:

    function validation(e)
    {
        //your checks
        if (isDataValid === false)
        {
            if (e.preventDefault)
            {
                e.preventDefault();
                e.stopPropagation();//VERY important
            }
            e.returnValue = false;
            e.cancelBubble = true;
        }
        return isDataValid;
    }
    

    To find out what both methods do, have a look at what MDN has to say
    To find out what you’re stopping when calling stopPropagation (or setting cancelBubble to true) I’d recommend quirksmode: events order very easy to follow, reasonably comprehensive description, and, in case you need it: their introduction to JS events, too.

    Update
    In response to your comments:

    <?php
    
    // connect to the database
    include('connect.php');
    
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        die();
    }
    
    
    $sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseId"; 
    
    $sqlstmt=$mysqli->prepare($sql);
    
    $sqlstmt->execute(); 
    
    $sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);
    
    $courses = array(); // easier if you don't use generic names for data 
    
    $courseHTML = "";  
    $courseHTML .= '<input type="text" name="courses" id="coursesDrop" />' . PHP_EOL; 
    $pHTML = '&nbsp;';//default paragraph inner
    if (isset($_POST['moduleSubmit'])) {    
            $sessionquery = "
        SELECT SessionId, SessionName, SessionDate, SessionTime, CourseId, SessionActive
        FROM Session
        WHERE (CourseId = ? AND SessionActive = ?)
        ORDER BY SessionName 
        ";
        $active = 1;
        $sessionqrystmt=$mysqli->prepare($sessionquery);
        // You only need to call bind_param once
        $sessionqrystmt->bind_param("si",$course, $active);
        // get result and assign variables (prefix with db)
        $sessionqrystmt->execute(); 
        $sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbCourseId, $dbSessionActive);
    
        $sessionqrystmt->store_result();
    
        $sessionnum = $sessionqrystmt->num_rows();   
    
        if($sessionnum == 0) {//error msg?
            $pHTML =  "<span style='color: red'>Sorry, You have No Assessments under this Module</span>";
        } 
    }
    
    ?>
        <p id="warnings"><?php echo $pHTML;?></p><!-- echo the innerHTML created server-side -->
    <form id="myForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation(event);">
        <table>
            <tr>
                <th>Course: <?php echo $courseHTML; ?></th>
            </tr>
        </table>
        <p>
            <input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" />
        </p>
        <div id="moduleAlert"></div>
        <div id="targetdiv"></div>
    </form>
    

    Tweaked JS – since you’re using jQuery, I’ll use that as delegating a change event in IE is a pain:

    $('#myForm').delegate('change','select',function()
    {
        $('#warnings').html('');//clears current warnings
    });
    

    Or, a more efficient but somewhat more complex take:

    $('#myForm').delegate('change','select',(function(warnings)
    {
        return function()
        {
            warnings.html('');
        };
    }($('#warnings'))));
    

    Don’t forget to wrap this in a $(document).ready(function(){[here]});

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

Sidebar

Related Questions

In the code below I have two types of validation. I use a javascript
I have two OptionMenu widgets in the simple pieces of code shown below: variable
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have two controls whos code are run time rendered as below: ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl03_txtTravelDate ctl00_PlaceHolderMain_SPWebPartManager_g_3c1ba10a_23ec_4ab5_b303_18f8bd7ee7e7_ctl00_gdvItinerary_ctl04_txtTravelDate
I have this code below. As you can see I am passing two variables
I have a chart (code to replicate will be below) that has two lines
I get this error, while I'm testing the code below: You have an error
I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I have below code: class Program { static void Main(string[] args) { Task[] tasks

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.