View Full Version : JavaScript to auto click submit button

05-26-2006, 04:57 AM
I'm quite new at JavaScript, anyway I'm trying to use it to write code to auto click the submit button in a web page. Here's my code:

<script LANGUAGE="JavaScript">
var m_alert = "2";
var m_type = "top";
var m_mode = "normal";
var m_xmlpage = "";
var m_method = "get";
var m_ap = "";

setTimeout("document.xmlform.submit()",2000); //After loading the page, wait 2 secs then submit form

function PushMsg() {

xmlform.action = ""; //After hitting submit button, it will go to this url...

m_ap = '<\?xml version="1.0"?>\n'+
'<Push alert= "'+m_alert+'" type="'+m_type+'" mode="'+m_mode+'">\n'+
'<go href= "'+m_xmlpage+'" method = "'+m_method+'">\n'+

document.forms[0].XMLData.value = m_ap; //...and also send these data to the target url


<form name="xmlform" method="POST" onSubmit="return PushMsg()">
<input type="hidden" name="XMLData">
<input type="submit" value="Submit" name="B1">


Well, I use this page to work with some other pages. This page will be called at the specific time, for example at 9am, 12pm and 5pm the other php page will automatically call this page, then hit submit button in order to send the hidden data in a form tag (<input type="hidden" name="XMLData">) to the target url.

The problem is, currently it can automatically submit the form, but it doesn't do the onSubmit="return PushMsg()" part, so the PushMsg() is not called to work. Now, instead of going to the target url ( after submitting the form, it just refreshes the same page every 2 secs. However, the code works if I click the submit button myself.

Is there any way to correct the problem? :confused:

05-26-2006, 05:18 AM
Try this.

if(PushMsg()) document.forms[0].submit();

05-26-2006, 06:35 AM
It doesn't work.

I think if(PushMsg()) document.forms[0].submit(); will submit the form after running the PushMsg() function.

But here's what I want the code to do:

1.) Submit the form and it should call the PushMsg() to run. (at <form name="xmlform" method="POST" onSubmit="return PushMsg()">)

2.) The PushMsg() will assign the target url and the data to XMLData. (at line xmlform.action = ""; and document.forms[0].XMLData.value = m_ap;)

3.) Then the XMLData will be used at the hidden field in the form. (at <input type="hidden" name="XMLData">)

4.) Hit the submit button to send the forn with the data in a hidden field.

Is that possible for JavaScript? :(

05-26-2006, 07:04 AM
Why dont you have the form post to another page with the XMLDATA intact, not process it and then store all the data in fields on that page. From thier you can have it submit to another page with that data.

form ---> submit page ---> process without XMLDATA ---> store data in identical fields with XMLDATA ---> submit page ---> second process form.

05-27-2006, 06:23 AM
It's okay. I've got it solved by inserting this in the body tag:

<body onLoad="PushMsg(); document.xmlform.submit()">

Thanx you guys for your help! :thumbsup: