Andrew
Andrew

Reputation: 51

How to submit a form on page load without clicking submit button?

Is there a way to make a form submit "onload" without clicking a submit button with PHP? i saw some ways to make it with Javascript, but i love php and id like to make it with php.

For example:

<form action="https://xxxxxx.com/yyy.php" method="post" id="foo">
<input type="hidden" name="LoginEmail" value="user [at] domain.com">
<input type="hidden" name="LoginPassword" value="mycleartextpassword">
<input type="submit" name="Authenticate" value="Login">
<input type="hidden" name="Action" value="1" >
</form>

I can make it with javascript:

<form id="foo"> .... < /form>

<script type="text/javascript">
    function myfunc () {
        var frm = document.getElementById("foo");
        frm.submit();
    }
    window.onload = myfunc;
</script>

Well, Can we make same thing with PHP? Could you please kindly share your ideas and advices?

Thank you.

Upvotes: 4

Views: 72329

Answers (5)

developer68
developer68

Reputation: 129

Use printf to print your javascript code then when the client browser receives the page it will execute your javascript.

printf("<script type="text/javascript">
    function myfunc () {
        var frm = document.getElementById("foo");
        frm.submit();
    }
    window.onload = myfunc;
</script>");

I do this a lot for the mapping services.

Upvotes: 0

rachael
rachael

Reputation: 21

i had diffculty in dong this as well so thought i would post my solution here it goes...

this curl command posts the file to a url and send back any data it receives. It's posting an xml file which is why the content type is set to application/x-www-form-urlencoded.The api i was posting to requested that the data be encoded which is why I use --data-urlencode. I also assign the filename with a post variable name here so its like posting an input field with a name="whatever".

curl --data-urlencode [post variable name]=@[filename] --header "Content-Type:application/x-www-form-urlencoded" http://[url to send file to]

Assigning the command to a variable is easy...see below '

content=$(put curl command in here);

P.s DO NOT FORGET TO ESCAPE ANY '=' or '&' in your url's, this causes issues if you're trying to retrieve data.

Upvotes: 2

John Parker
John Parker

Reputation: 54445

It isn't possible to carry out a client-side (i.e.: browser based) form submit action using a server side language (i.e.: PHP). It's a bit like trying to get a library to read a book, if that makes sense. (You get the books from the library and return them there, but the library itself can't read books.)

As such, JavaScript is your only solution if you require an automated submission. Even then, you need to ensure there's a "normal" solution for use by people who don't have JavaScript enabled. (The joy of graceful degradation, etc.)

Upvotes: 4

sibiraj
sibiraj

Reputation: 55

it is by using jquery ajax.

$(document).ready(function(){..........your code goes here........});

if you prefer jquery replay me i wll give you the help

Upvotes: 0

marines
marines

Reputation: 2693

If you want to auto-submit form after page load (IMHO it's pointless) I assume you generated its values by yourself (by PHP code) and you want to send them to another page, right? If so redirect page to the another by header() passing values through POST:

$post_data = 'LoginEmail=username [at] domain.com&LoginPassword=...';
$content_length = strlen($post_data);

header('POST /another_page.php HTTP/1.1');
header('Host: fancyhost');
header('Connection: close');
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: ' . $content_length);
header('');
header($post_data);

exit();

Upvotes: 1

Related Questions