1

New to PHP and script and have come across a problem that I can’t resolve.

I have a booking page that users tick a checkbox to book times. The on submit button then runs a script to check if they have booked the number of times that they asked for. So if asked for 3 booking times and they have only select 2 it fires the alert window. On pressing OK the window should reload so they can select again. This works great in Firefox and will reload all the select checkboxes are cleared.

However if you do this in IE and chrome the alert box hijacks the page and the window still has the checkbox’s selected.

function CheckBox() {
  try {

    var max = document.mainForm.serNo.length;
    var count = 0;
    var tot = <?php echo $Num ?>;

    for (var i = 0; i < max; i++) {
      if (document.mainForm.serNo[i].checked == true) {
        count++;
        serNoChecked = i;
      }
    }

    if (count < tot) {
      for (var i = 0; i < max; i++) {
        window.alert('THERE IS A PROBLEM WITH YOUR BOOKING. \n You have asked to book ' + tot +  ' times, but have only selected ' + count + ' times.\n Please re-select the same number of times as asked at the start or re-start your booking');

        window.location.assign("url");
      }
    }     

  } catch (e) {
     alert(e.message);
  }
}
ganzogo
  • 2,516
  • 24
  • 36
Rik
  • 11
  • 3
  • if you want to reload the page i suggest `location.reload(true)` this 'should' clear the checkboxes – Kevin Kloet Nov 11 '16 at 10:47
  • Thats Kevin, Still doen't work, The alert box still stays when the page re-loads – Rik Nov 11 '16 at 12:50
  • [this might help you](http://stackoverflow.com/questions/16955019/how-to-reload-a-page-after-the-ok-click-on-the-alert-page) – Kevin Kloet Nov 11 '16 at 13:21
  • Thanks Again Kev, Didn't work. Internest this did just keep going in a loop in IE. In firefox, it stop after the first message and loaded the page. alert('foo'); window.location.reload(true); alert('bar'); window.location.reload(true); alert('foobar'); window.location.reload(true); – Rik Nov 11 '16 at 13:50
  • remind me, why is the alert and reload in a for loop? – Kevin Kloet Nov 11 '16 at 13:55
  • It shouldn't, Firefox works OK, I.E and Chrome seem to loop the alert box. When I tried the alert(foo) etc, Firefox didnt loop it, but IE did and the message just kept going around. – Rik Nov 11 '16 at 14:16
  • it shouldn't loop, than why is it in a for loop? – Kevin Kloet Nov 11 '16 at 14:19
  • Does the code in the first post suggest a loop becasue it shouldn't. – Rik Nov 11 '16 at 14:24
  • yes `if (count < tot) { for (var i = 0; i < max; i++) { window.alert('your alert'); window.location.assign("url"); } }` loops as long as i < max, assuming max is 3 the loop would be executed 3 times – Kevin Kloet Nov 11 '16 at 14:27
  • $Num is the number of books they want, If they select less than I need an alert message to popup and say that. Once they click Ok I need the page to re-load and not go onto the next page. If the number select = the $num the it can move on. – Rik Nov 11 '16 at 14:35
  • Why reload the page? You can reset the form by calling `document.mainForm.reset()`. – Heretic Monkey Nov 11 '16 at 15:15

1 Answers1

0

you have a loop which is giving multiple times the popup, this is redundant.

also the use of location.reload(true) is suggested as the trueparameter reloads the current page from the server instead of from the cache.

the if(!alert("your text")){} checks if the alert box is not closed, when closed it executes the window.reload(true)

the submit button and form must have a unique id for this.

i have posted the updated code below:

document.getElementById("mySubmitButtonId").addEventListener("click", function(event) {
  try {
    event.preventDefault();
    var max = document.mainForm.serNo.length;
    var count = 0;
    var tot = <?php echo $Num ?>;

    for (var i = 0; i < max; i++) {
      if (document.mainForm.serNo[i].checked == true) {
        count++;
        serNoChecked = i;
      }
    }

    if (count !== tot) {
        if(!alert('THERE IS A PROBLEM WITH YOUR BOOKING. \n You have asked to book ' + tot +  ' times, but have only selected ' + count + ' times.\n Please re-select the same number of times as asked at the start or re-start your booking')){
        window.location.reload(true);
        }
      } else {
            document.getElementById("myFormId").submit();
        }

    } catch (e) {
     alert(e.message);
  }
});
Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21
  • Thanks again Kevin. It works great in IE, and I suspect Chrome. But now Firefox shows the message, but then moves on to the comformation page, when it should re-load this page. I have aslo just tested the IE when all ccorrect number of times are checked and it comes up with IE canot opne the URl page Option aborted. – Rik Nov 11 '16 at 15:13
  • that is a feature in the browser to prevent people from accidentally losing data they are sending to the server, as far as i know you can't disable this popup. – Kevin Kloet Nov 11 '16 at 15:18
  • `This may occur on lower versions of Internet Explorer and to the newly updated ones. This may be a server side or client side issue.` what version of IE are you using? – Kevin Kloet Nov 11 '16 at 15:22
  • Why isn't it working with Firefox, if I wanted 3 times, but only select 2 it gives the waring and that move to the conformation page. It should re-load this page. I tried the IE agian and it worked, Properly me playing around with it. – Rik Nov 11 '16 at 15:28
  • i have edited my answer, and `document.mainForm.serNo.length` = how much checkboxes there are and `` is how much you are allowed select, right? – Kevin Kloet Nov 11 '16 at 15:35
  • The $num the number of bookings they have asked for. They then pick times from a list of availabe time. Once they pick 3 or how many slots they want to book all the checkboxes grey out. If they have asked for 3 but only 2 picked I want to warn them. You can see the form from this link, https://dl.dropboxusercontent.com/u/144531/Untitled.png – Rik Nov 11 '16 at 15:55
  • couldn't you prevent the user from submitting with an onsubmit event? that would be much easier. – Kevin Kloet Nov 11 '16 at 16:01
  • Do you mean the submit buitton, This is what I have – Rik Nov 11 '16 at 16:06
  • @Rik i have updated my answer with the onsubmit event. P.s. the onclick in your submit button can be deleted. – Kevin Kloet Nov 14 '16 at 08:03
  • Thanks for coming back to me with this. The onclick was pointing to the function, No I have taken the onclick off the page goes straight to the confirm page. Are you saying that the code above needs to go with the Booking Button. If so I have no idea how to do this. – Rik Nov 14 '16 at 09:17
  • yes the code above needs to be executed with the event handler for click on the submit button. as said in the answer the form and the submit button both need an unique id. instead of the onclick in the html page here is a onclick in the javascript, event is passed in the function bound to it, the default handling of the submit button is prevented and if the `count == tot` the form will be submitted. anything you don't understand about this? – Kevin Kloet Nov 14 '16 at 09:29
  • I have tried to work this out, but failing badly, The popup is working in Firefox, but not IE. In Firefox if correct number of booking made I get "document.getElementById(...) is null" . Form is
    and button is now . So I need a function above the document .getelementByid somelike window.onload = function()? As I said I am new to this.
    – Rik Nov 14 '16 at 10:42
  • you get the error because you have not specified an id for the form, `
    `
    – Kevin Kloet Nov 14 '16 at 10:48
  • This is driving me nuts, I did add this and it didn't work so took it out, I have added it as you suggested, Firefox popup OK if not enough booking made, but if the correct number made the page just reload again. In Internet Explorer I get webpage has expired :( I have changed the button to – Rik Nov 14 '16 at 10:59
  • the webpage has expired is a problem with the caching in ie, [as explained here](http://support.microsoft.com/kb/183763) – Kevin Kloet Nov 14 '16 at 11:08
  • when the correct number made, does it show the alert? – Kevin Kloet Nov 14 '16 at 11:13
  • NO not in Firefox. – Rik Nov 14 '16 at 11:22
  • the form action # could have impact on this try this: `
    ` and tell me if it still reloads the page or if the submit actually worked.
    – Kevin Kloet Nov 14 '16 at 11:31
  • Popup box works great in Firefox and chrome. Unfortunatly still only reloading the the same page when correct number of bookings made. Thanks again for your help. – Rik Nov 14 '16 at 11:44
  • so, you want to reload the page even if the user has the correct number of bookings? – Kevin Kloet Nov 14 '16 at 12:13
  • NO, If the user books the number of booking they have asked for the page should load a conformation page. As it is now if I pick the right number it re-loads the page we are on. If I don't select enough booking the warning popup works. I have checked the script, the only thing I don't understand is the extra } before the else statement. – Rik Nov 14 '16 at 12:20
  • the extra `}` is to close the if statement, there is an if statement with an if statement nested inside, the else is for the outer most if statement. as far as loading another page you need to specify to where the form is posting using the action attribute [read more about action attribute](https://developer.mozilla.org/nl/docs/Web/HTML/Element/form#attr-action) – Kevin Kloet Nov 14 '16 at 12:28
  • I presumed that the document.getElementById("myFormId").submit(); would point ito the isset($_POST["submit"]), which has a $updateGoTo = after the insert query. I did read the link and I seem to have it as they say. – Rik Nov 14 '16 at 13:01
  • can you add that piece of php to the question? also put an minified version of your html form in the question, that would make it easier to spot the problem – Kevin Kloet Nov 14 '16 at 13:10
  • You can see it here: https://dl.dropboxusercontent.com/u/144531/php.txt – Rik Nov 14 '16 at 13:28
  • i notice that your first part of php doesn't have ``, your body tag appears to be missing an `<`, also there are 2 body tags?, also have i noticed that there is some random text inside an redundant php tag. (`if Code for form }`) , and the part of `$updateGoTo` is missing. – Kevin Kloet Nov 14 '16 at 14:32
  • I did delete things to cut it down, The , and the is also there. Hadn't noticed the 2nd Body tag and have deleted it. If I remove the code you kindly gave me and change the name of the button to submit the php works as it should do and updates the sql table and moves to the conformation page. – Rik Nov 14 '16 at 14:52
  • you should change this line, `if(isset($_POST['submit']))` to this instead, `if($_SERVER['REQUEST_METHOD'] === 'POST')` – Kevin Kloet Nov 14 '16 at 14:56
  • You are a star, I don't understand why the isset method needed to be different and haven't seen if($_SERVER['REQUEST_METHOD'] === 'POST') before. Could you point me to the describtion, or explain. Either way it works great. – Rik Nov 14 '16 at 15:10
  • `$_SERVER` is a PHP super global variable which holds information about headers, paths, and script locations. `['REQUEST_METHOD']` Returns the request method used to access the page. why do you need this instead of the `$_POST`? because the `$_POST` only works when the form is submitted with the submit button, here we have prevented the default behavior of the submit. [you can read more about super globals here](http://www.w3schools.com/php/php_superglobals.asp) – Kevin Kloet Nov 14 '16 at 15:33