-2

I have made a Formula with on onchange="this.form.submit()" and a automatic page refresh function with window.location.replace();. My Problem is, that i need to know where the user clicked in the last time. My Solution was a second onclick="" event to get the id of the selected field. This works fine if i make no changes in the form. When i change some values and go to a other field, the onclick function didn't work. How can i solve this problem? And by the way, sorry for my bad english.

    <input name="'.$idname.'" value="'.$cont_field.'" type="text" class="loginField" size="'.$breite.'" style="width:98%;" id="'.$idname.'" onchange="this.form.submit()" onclick="focusCookie(event)">

    <script type="text/javascript">
    function focusCookie (event) {
        event = event || window.event;
        var target = event.target || event.srcElement;
        var anch = target.id;
        createCookie("anchor", anch, new Date(new Date().getTime() + 10000));
    }
    function createCookie(name, value, expires, path, domain) {
      var cookie = name + "=" + escape(value) + ";";
      if (expires) {
        if(expires instanceof Date) {
          if (isNaN(expires.getTime()))
           expires = new Date();
        }
        else
        expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
        cookie += "expires=" + expires.toGMTString() + ";";
      }
      if (path)
        cookie += "path=" + path + ";";
      if (domain)
        cookie += "domain=" + domain + ";";
      document.cookie = cookie;
    }
    </script>

And this ist the loadingfunction

    //--> Loaders
    function Loader($https_url,$seite,$loader_id){
    $loaderurl = '<script type="text/javascript">window.location.replace("'.$https_url.'/index.php?inhalt=extranet&extra=formular&seite='.$seite.'#'.$loader_id).'");</script>';
    return $loaderurl;  
    }

Ok 2 Problems solved and 1 still pending. I can send the form and catch the anchor tag befor. But after the Siteload the focus(); is lost...

Here my Code:

    <script type="text/javascript">
    function submitform (event) {
    event = event || window.event;
    var target = event.target || event.srcElement;
    var anch = target.id;
    createCookie("anchor", anch, new Date(new Date().getTime() + 10000));
    sendeForm();
    document.getElementById(anch).reload();
    }
    function createCookie(name, value, expires, path, domain) {
    var cookie = name + "=" + escape(value) + ";";
    if (expires) {
    if(expires instanceof Date) {
    if (isNaN(expires.getTime()))
    expires = new Date();
    }
    else
    expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
    cookie += "expires=" + expires.toGMTString() + ";";
    }
    if (path)
    cookie += "path=" + path + ";";
    if (domain)
    cookie += "domain=" + domain + ";";
    document.cookie = cookie;
    }
    function sendeForm(){
    document.getElementById("ID").submit(); 
    }
    </script>
Mr.Robot
  • 3
  • 6
  • I need the onclick Event to set the focus on the last selected inputfield. – Mr.Robot Jul 20 '17 at 15:08
  • Why not do `window.location.reload()` instead? It'll automatically scroll back to the last known position. – James Gould Jul 20 '17 at 15:13
  • THX for your answere, the problem is that i need to reload the site to check the database results. Is there a possibility to get the last focus by sending the form and refreshing the site with a hiddenfiled or by submitting the id of the field by sending and refreshing the Form? – Mr.Robot Jul 20 '17 at 15:23
  • Sounds like a job for `AJAX`. – James Gould Jul 20 '17 at 15:24
  • Is there any way to trigger the last focus of the user by using javascript. My Solution was to set a cookie with the information (ID) with js. The Problem ist that the onchange Event fires first on a change an not the onclick event. – Mr.Robot Jul 20 '17 at 15:27
  • Without seeing the rest of your code it's hard to say. Any answers you'd receive would be guesswork, hence the downvotes I suppose. – James Gould Jul 20 '17 at 15:28
  • Can i change the order how onclick and onchange is executed? – Mr.Robot Jul 20 '17 at 15:33
  • Yes. If you want help with your code, you need to share it. – James Gould Jul 20 '17 at 15:35
  • How can i send you my code - i am new in this forum. – Mr.Robot Jul 20 '17 at 15:40
  • Include it in your post. – James Gould Jul 20 '17 at 15:54
  • Ok this is the important part, tell me if you need more informations please. As you see, the inputfield has to eventhandler onclick and onchange. The onchange submits the form and the onclick get the focused field. – Mr.Robot Jul 20 '17 at 16:09
  • Is there a way to save the clicked filed onclick="" by executing a js before sending the informations onchange? I have found this, can this work for my problem? https://stackoverflow.com/questions/10718730/onclick-event-not-triggered-when-onchange-triggered-right-before?rq=1 – Mr.Robot Jul 20 '17 at 16:25
  • I have found this, but now i need to addapte this to my code. https://stackoverflow.com/questions/29254189/how-to-fires-onclick-event-before-onchange-even-if-the-value-has-changed – Mr.Robot Jul 20 '17 at 17:24
  • ok problem solved, but there is one last thing to do. How can i focus the last field with js? document.getElementById(ANCHOR ID).focus(); does not work. I use the same ID as before. – Mr.Robot Jul 20 '17 at 17:58

1 Answers1

0

I solved it with a AJAX Form by sending the Data with a XMLHttpRequest. This works fine. The Variables are generated with PHP.

    function ajax_post(){
    //Vars
    var xhr = new XMLHttpRequest();
    var url = 'URL';
    <?php echo $VARLIST; ?>
    var ausgabe = <?php echo $VARS; ?>;
    //Send
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200){
    var return_data = xhr.responseText;
    document.getElementById("Register").innerHTML = return_data;
    }
    }
    xhr.send(ausgabe);
    document.getElementById("Register").innerHTML = "Reload..";
    }
Mr.Robot
  • 3
  • 6