I’m creating a webpage which has several fields. I do not want the php code to execute unless all the fields are filled out as that will create an error.
Here is my html code:
<html>
<body bgcolor="AliceBlue">
<form action="WorkingVersionV2.php" method="post">
<p>Which database do you wish to access? <center> <input type="text" name="database"/> </center></p>
<p>What table would you like to examine? <center><input type="text" name="oldtablename"/></center></p>
<p>What would you like to name the table produced? <center><input type="text" name="newtablename"/></center></p>
<p>Start at row: <center><input type="text" name="startrow"/></center></p>
<p>Number of rows to search through:<center> <input type="text" name="numberrows"/> </center></p>
<center><input type = "submit" onclick="checkFields()"/></center>
</form>
<script type="text/javascript">
function checkFields()
{
var x = document.getElementById("database").innerHTML;
if(x=="")
{
alert("You forgot to enter a database name");
}
}
</script>
</body>
</html>
I figured throwing a pop up box would halt the user if the fields were not filled out. However, when the user clicks submit, the pop up box is not thrown up, and I’m unsure whether the php code will still execute once they close it.
HTML/javascript is not my forte, if you have a better or easier way of doing this then please let me know.
If you are using the input type=submit for non-javascript browsers then you need to make a few changes.
You javascript function needs to return a value like this:
And you should change your input button like so: