Note that there are some explanatory texts on larger screens.

plurals
  1. POboth validation messages are appearing on javascript and php
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>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. </p> <p>This is incorrect, what should happen is that :</p> <ul> <li><p>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.</p> <ul> <li>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.</li> </ul></li> </ul> <p>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? </p> <p>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. </p> <p><strong>Javascript</strong></p> <pre><code>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; }​ </code></pre> <p><strong>PHP/HTML</strong></p> <pre><code>&lt;?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-&gt;prepare($sql); $sqlstmt-&gt;execute(); $sqlstmt-&gt;bind_result($dbCourseId, $dbCourseNo, $dbCourseName); $courses = array(); // easier if you don't use generic names for data $courseHTML = ""; $courseHTML .= '&lt;input type="text" name="courses" id="coursesDrop" /&gt;' . PHP_EOL; ?&gt; &lt;form action="&lt;?php echo htmlentities($_SERVER['PHP_SELF']); ?&gt;" method="post" onsubmit="return validation();"&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Course: &lt;?php echo $courseHTML; ?&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;p&gt; &lt;input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" /&gt; &lt;/p&gt; &lt;div id="moduleAlert"&gt;&lt;/div&gt; &lt;div id="targetdiv"&gt;&lt;/div&gt; &lt;/form&gt; &lt;?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-&gt;prepare($sessionquery); // You only need to call bind_param once $sessionqrystmt-&gt;bind_param("si",$course, $active); // get result and assign variables (prefix with db) $sessionqrystmt-&gt;execute(); $sessionqrystmt-&gt;bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbCourseId, $dbSessionActive); $sessionqrystmt-&gt;store_result(); $sessionnum = $sessionqrystmt-&gt;num_rows(); if($sessionnum == 0) { echo "&lt;p&gt;&lt;span style='color: red'&gt;Sorry, You have No Assessments under this Module&lt;/span&gt;&lt;/p&gt;"; } else { echo ""; } ... } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload