Currently if a user leaves the exercise title input blank and has text input in the exercise text, an alert is shown telling the user to complete the full form and vice versa. When the alert appears and ok is pressed the form data is lost and the user has to add all the input again. How would I make the alert appear and pressing ok keeps the user on the same page? I'm unsure if its because I have a hide and show div on clicking 'create new exercise' or something else.
My code is below
CourseA.php
html:
<script>
function showDiv(el1,el2){
document.getElementById(el1).style.display = 'block';
document.getElementById(el2).style.display = 'none';
}
</script>
<div class="right">
<h2 id = 'course_admin_titleA'>Course A</h2>
<button id= 'create_btnA' onClick="showDiv('is', 'ie')">Create new session</button>
<button id= 'create_btnB' onClick="showDiv('ie', 'is')">Create new exercise</button>
</div>
<div id= "ie">
<!-- input form for admin -->
<form action="courseA.php" method="post" enctype="multipart/form-data">
<table class ="table_exercise">
<td id = "exercise_titles"><strong> Exercise Title:</strong></td>
<td><input type="text" name="exercise_title" value="<?php if(isset($_SESSION['a'])){echo $_SESSION['a'];} ?> " size="60" /></td>
</tr>
<tr>
<!-- input text area for admin to type exercise content -->
<td id = "exercise_titles"><strong> Exercise Text:</strong></td>
<td><textarea name="exercise_text" rows="15" cols="50" ><?php if(isset($_SESSION['b'])){echo $_SESSION['b'];} ?></textarea></td>
</tr>
</table>
<!-- button with input type 'submit' which is referenced by the php code -->
<button id= 'create_btnB'input type="submit" name="submit1" > Publish Exercise Now</button>
<!-- Hide if not clicked -->
<input type="hidden" value="ie" name="type">
</form>
</div>
php:
<?
session_start();
//if submit button is set i.e 'publish exercise now' pressed then:
if(isset($_POST['submit1'])){
$_SESSION['a'] = $_POST['exercise_title']; //assign to session variable
$_SESSION['b'] = $_POST['exercise_text'];
//local variables taking the input names from the html forms above
$exercise_title = $_POST['exercise_title'];
$exercise_text = $_POST['exercise_text'];
//validation
if($exercise_title =='' OR $exercise_text==''){
echo "<script>alert('Please make sure the exercise has a title and exercise info')</script>";
//exit();
} else {
//query to insert the form data from the admin into the exercises table
$insert_exercises = "insert into exercises (exercise_title,exercise_text)
values ('$exercise_title','$exercise_text')";
//runs query
$run_exercises = mysqli_query($con,$insert_exercises);
//JS to tell user exercise has been published
echo "<script>alert('Exercise Has been Published!')</script>";
//returns to courseA page on the admin panel
echo "<script>window.open('courseA.php','_self')</script>";
} //end of else
} //end of IF
//end of if & end of PHP
} ?>
Thanks