Run automatic click on form input

0

I have a search form, and it lists details. when accessing the details it writes session, but when returning the list it saves the search in the session but it does not execute the search.

What I thought was to perform a hidden input with id="send", when accessing it it makes a submit on the form, so return the page in the same user search.

I did so

<script type="text/javascript">
                $("#enviar").click();
                $(this).stop();
</script>

But it's in a loop, it seems like the stop does not work, or I would like to do this once, but not so much of javascript.

Someone with a tip.

    
asked by anonymous 19.09.2017 / 17:19

2 answers

1

Maybe .stop does not execute because it will only be processed in the loading of the page and not in the download, I can not state with conviction this because I'm not sure of the behavior in different browser engines (webkit, blink, trident, gecko , etc), but some ways you could choose are to use cookies or the parameters in the querystring (so it does not affect POST).

Assuming your form is something like:

<form action="?send=1" method="POST">
...
<input type="submit" value="">
</form>

And in JavaScript check:

<script type="text/javascript">
//Verifica se tem o GET com "expressão regular"
if (/\?send=1(&|$)|&send=1(&|$)/.test(window.location)) {
    $("#enviar").click();
}
</script>

Or if your page uses PHP you can check if one of the POST fields was sent (or rather all) using isset :

<?php if (!isset($_POST['campo1'], $_POST['campo2'], $_POST['campo3'])): ?>

<script type="text/javascript">
$("#enviar").click();
</script>

<?php endif; ?>

The !isset($_POST['campo1'], $_POST['campo2'], $_POST['campo3']) contains ! and isset , is a negation, ie if there is no field1, field2, field3, it means that it did not come from your FORM and then it will execute .click , of FORM will not go into the IF and will not run again.

Set the values within isset to the fields in your FORM.

    
19.09.2017 / 18:51
0

Do this:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">

        function sendForm() {
        $("#enviar").click();
      } 

        </script>
    </head>
    <body onload="sendForm();">

    </body>
</html>

In this way, the 'sendForm' function will be created and the 'onload' method will call it as soon as the page loads.

For more information about onload, just click here .

    
19.09.2017 / 17:23