Run automatic click on form input

Asked

Viewed 1,098 times

0

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

What I thought is to execute a hidden input with id="send", when accessing it makes a Submit in 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 is in loop, it seems that the stop does not work, or would like to run a single time this function, but I don’t know so much of javascript.

Someone with a hint.

2 answers

1


Maybe the .stop do not perform because it will only be processed on page loading and not on download, I can not affirm with conviction this because I am not sure of the behavior in different browsers engines (Webkit, Blink, Trident, Gecko, etc.)but some paths you could choose are to use cookies or the parameters in querystring (so it doesn’t affect the POST).

Supposing 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 better 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 denial, that is if there is no campo1, campo2, campo3, it means it did not come from your FORM and then it will execute the .click, chance has come from FORM will not enter IF and will not run again.

Adjust the values within isset to the fields of your FORM.

  • my form works in PHP on the same page without refresh. So my form looks like this: form class="form-inline" id="busca" method="POST" action="" Maybe I haven’t understood your second answer, but let’s see the idea. I already do a check in the&#Xa field;if(!empty($select_empresa)){&#xA; $sql_empresa = "AND empresa_id='$select_empresa' ";&#xA; } This way, I think your second suggestion does not match the result I need. Actually I need the click to run with the value that is already in the POST fields, and not clean it, maybe I got it wrong.

  • @Marcelorossi yes you got it wrong, I gave an example Enerico, because I can’t guess your current PHP structure, since you didn’t post it, right? So just you adapt the code I posted, the above situations are simple examples, adapting will work.

  • I will try here, adapt as you suggested. Thank you very much for the collaboration.

0

Do it that way:

<!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>

This way, the 'sendform' function will be created and the 'onload' method will call it as soon as the page is loaded.

For more information about onload, just click here.

  • Can explain why your code is different from the one presented in the question?

  • I just corrected, there really was no difference. Take a look now please.

  • 1

    I say explain in the sense of describing in text your answer. Not all users will have the knowledge to abstract what you did just by reading the code.

  • There is not much secret, I tried to explain a little of what was done. I hope it helped.

  • 1

    @Andersoncarloswoss out that the problem is not with sending, the AP itself said that the problem is the infinite LOOP.

  • @Samuel - Your solution worked like mine, but the result is still looped. the page keeps being loaded, I need a solution to perform only one click event, and not in loop. But thanks for your collaboration.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.