php - Using javascriptjquery to submit a form upon the loading of a page - Stack Overflow

I wrote an array to pass variables in the GET to a search page. The search page only has 4 fields but I

I wrote an array to pass variables in the GET to a search page. The search page only has 4 fields but I'm only passing the most important variables, first name and last. Here is the array:

<?php echo "<td><a href='" . matry::base_to('test/trace', array('first'=>$patient->first , 'last' =>$patient->last)) . "'><ul class='controls'>
                    <li id='check_orders'><`span class='symbols'>L</span><span class='label'>Skip Trace</span></li>

                </ul></a></td>";?>

When the page loads i'm just echoing the _GET to pre populate the first and last input fields on that page..

What I'm looking for is a script that will execute the search with the first and last name fields populated as that page loads automatically. Additionally, when the search is executed it's populating in an iframe. (forgot about that part)~!

I tried using:

<script>document.getElementById('stack').submit();</script>

 <form action='' name='es' target="my_iframe" id="stack">

with no avail.

I wrote an array to pass variables in the GET to a search page. The search page only has 4 fields but I'm only passing the most important variables, first name and last. Here is the array:

<?php echo "<td><a href='" . matry::base_to('test/trace', array('first'=>$patient->first , 'last' =>$patient->last)) . "'><ul class='controls'>
                    <li id='check_orders'><`span class='symbols'>L</span><span class='label'>Skip Trace</span></li>

                </ul></a></td>";?>

When the page loads i'm just echoing the _GET to pre populate the first and last input fields on that page..

What I'm looking for is a script that will execute the search with the first and last name fields populated as that page loads automatically. Additionally, when the search is executed it's populating in an iframe. (forgot about that part)~!

I tried using:

<script>document.getElementById('stack').submit();</script>

 <form action='http://xxxx.yyyyyyy./stuffhere' name='es' target="my_iframe" id="stack">

with no avail.

Share edited Dec 28, 2012 at 17:49 Head asked Dec 28, 2012 at 17:45 HeadHead 5687 silver badges27 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Your <script> is running before the <form> exists.

Move the <script> below the <form>.

You are calling the submit function before the form is even loaded on the page.

Place the script tag after the closing form tag or call submit on document ready or window onload.

<form id-"stack">
  ... form fields...
</form>
<script>document.getElementById('stack').submit();</script>

or

<script>$(function(){$('#stack').submit();})</script>

Please imagine this simple example:

<html>
  <head>
    <script type="text/javascript">
        function body_onload() {
            var form = document.getElementById('theform');
            form.submit();
        }
    </script>
  </head>
  <body onload="body_onload()">
    <form id="theform" action="action.php">
        <input type="hidden" name="query" id="query" value="foo" />
    </form>
  </body>
</html>

It will submit the form after the page has loaded.

But are you really searching for a non AJAX solution?

Try this, been using this on a redirect page for a while. This of course needs to be below the from so it is run after the browser process the form.

<script type="text/javascript">
    function send(o)
    {
        var f = document.getElementById("theForm");
        f.submit();
    }
    send();
</script>

If you want to submit the form when the page loads you should change your code to this:

<script>window.onload = function(){ document.getElementById('stack').submit(); }</script>

However this will redirect the user (as if they have clicked a form submit button). To avoid this you will need to use AJAX (I remend using jQuery to do this). See example below:

 $(document).ready(function(e) {

    var form_data = $("#form_id").serialize();
    var form_url = $("#form_id").attr("action");
    var form_method = $("#form_id").attr("method").toUpperCase();

    $.ajax({
        url: form_url, 
        type: form_method,      
        data: form_data,     
        cache: false       
    });    

 });

See this page for more info on using AJAX

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744947391a4602705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信