I'm not sure if I should post this here or on a JavaScript board.
I have a PHP Web page which contains a few forms that will pop up a JavaScript alert if the user tries to submit any one of the forms without filling in each of the input fields. There is an alert for each field.
The JavaScript is embedded in the PHP. The problem I am encountering is that the page goes blank when any one of the alerts pops up. Once the OK button in the alert is clicked, the page comes back. I just want the page to remain visible in the background when an alert pops up. I guess it has something to do with the page refreshing. Most all of the PHP code (including the JavaScript alerts) is at the beginning of the page, before the HTML starts. I read somewhere that the JavaScript would have to be moved to the end of the page to avoid this issue. Iโve tried and tried, but cannot get it to work.
Here is a sample of the code:
...
if( isset($_POST['addNum']) ) {
$Error = false;
$sg = $_POST['code'];
if (!$sg) {
$Error = true;
print "<script type=\"text/javascript\">\n";
print "window.alert(\"You must enter a number\")\n";
print "</script>";
}
$rp = $_POST['rePg'];
if (!$Error) {
if (!$rp) {
$Error = true;
print "<script type=\"text/javascript\">\n";
print "window.alert(\"You must enter a page\")\n";
print "</script>";
}...
I tried changing it to this:
...
if( isset($_POST['addNum']) ) {
$Error = false;
$sg = $_POST['code'];
if (!$sg) {
$Error = true;
?>
<script type="text/javascript">
window.alert("You must enter a number")
</script>
<?
}
$rp = $_POST['rePg'];
if (!$Error) {
if (!$rp) {
$Error = true;
?>
<script type="text/javascript">
window.alert("You must enter a page")
</script>
<?
}
...
It functioned just the same, so I then took the JavaScript out of the PHP coding and placed it down lower in the page, just before the closing body tag in the HTML.
I tried both of the following:
<script type="text/javascript">
window.alert("You must enter a number")
</script>
<script type="text/javascript">
window.alert("You must enter a page")
</script>
and
<script type="text/javascript">
window.alert("You must enter a number")
window.alert("You must enter a page")
</script>
(There's actually many more alerts than listed here.)
Both scenarios functioned the same. The page remains in the background (instead of being blank), but now when the page is loaded for the first time or when one of the fields is left blank and submitted, all of the alerts pop up one by one.
Forgive me if this should be posted on a JavaScript board. I wasn't sure. any help would be greatly appreciated