-2

I have a simple form where the user chooses a value that I will be saved in the database. HTML

<form  method="get" >
        <div class="select-block1">
            <select name="proximID" onchange="this.form.submit(); ">
                <option value="" disabled selected> Votre Proxim'IT</option>
                <?php foreach ($results as $output){?>

                <option value="<?php echo $output['id']; ?>"> <?php echo $output["first_name"], $output["last_name"]; ?></option>
                <?php } ?>
            </select>
         
        </div>
    </form>

PHP

<?php 
if(!empty($_GET['proximID'])) { 

$stmt6 = $pdo1->query("UPDATE proximit SET ProximID = {$_GET['proximID']} "); ?>

when i choose a value, pop-up window appear " leave site?" changes you made may not be savecd". Please how can i disable this pop-up window for chrome

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Does this answer your question? [How to prevent "Are you sure you want to navigate away from this page?" alert on form submission?](https://stackoverflow.com/questions/11183682/how-to-prevent-are-you-sure-you-want-to-navigate-away-from-this-page-alert-on) – Yogi Jun 16 '22 at 16:27
  • So where is your onbeforeunload code? – epascarello Jun 16 '22 at 16:33
  • @epascarello I forgot to mention that I already did some research before asking my question. and actually I used onbeforeunload like the solution suggested below and it didn't work. I will be very grateful if you can suggest me a solution. – user17664547 Jun 16 '22 at 18:54
  • Does your code have onbeforeunload set anywhere? Browser does not do it by default. – epascarello Jun 16 '22 at 19:01
  • @epascarello actually i am working on the open source Osticket so i don't know if there is onbeforeunload set anywhere – user17664547 Jun 16 '22 at 19:43
  • I think that uses jQuery? So try `$(window).unbind('beforeunload');` – epascarello Jun 17 '22 at 04:28
  • Indeed they use JQuery 3.4.0. And still not working :/ – user17664547 Jun 17 '22 at 08:38

1 Answers1

0

That popup is shown because the onsubmit event triggers a page refresh which also calls onbeforeunload event.

From the docs: The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point.

To take control over this popup, you need to modify this event.

Add an id attribute to the <form> tag

<form  method="get" id="selectForm" >

Assign onChange a function

<select name="proximID" onchange="onSelectHandler();">

which triggers

function onSelectHandler() {
    const form = document.getElementById('selectForm');
    window.onbeforeunload = null;
    form.submit();
}    
sid
  • 1,779
  • 8
  • 10