开发者

Submit form using javascript, work in FF but not in IE

I have this code.

The code below is working in Firefox, but it is not in IE

<?php // file: login_dfr.php ?>
<body>
<?php
$data = getLoginData($_SESSION['whoyouare']);
?>
<form name="frm_redirect_dfr" action="<?php echo $data['url']; ?>" method="POST"     id="frm_redirect_dfr" style="display: none;">
    <input name="DFRNet_User" value="<?php echo $data['username']; ?>" type="hidden" />
    <input name="DFRNet_Pass" value="<?php echo $data['password']; ?>" type="hidden" />
    <input name="tbllogin" value="login" type="hidden" />
    <input type="submit" value="submit" />
    </form>
    <script language="javascript" type="text/javascript">
    document.forms["frm_redirect_dfr"].submit();
    </script>
</body>

What I want to do is, when user access the page, it first will try to get login data, echo it in the form, and submit the form automatically using javascript

Update: I forget to mention that the code above is on a Frame, the mainFrame code is like below:

<?php session_start(); ?>
<?php // file: login_frame.php ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Wavinet</title>
</head>
<frameset rows="1,*" frameborder="no" border="0" framespacing="0">
<frame src="topFrame.php" name="topFrame" scrolling="No" noresize="noresize" id="topFrame" title="topFrame" />
<frame src="lo开发者_如何学Gogin_dfr.php" name="mainFrame" id="mainFrame" title="mainFrame" />
</frameset>
<noframes>
<body>This page contain frame</body>
</noframes>
</html>


Your code doesn't really make sense, unless I'm misunderstanding it. You're generating a form entirely with information already available to PHP, and submitting it back to PHP. There's no reason, you can just include whatever file $data['url'] points to in your script and give it the username and password directly


You probably need to give IE time to render the form before you make it submit it. So this with

<script language="javascript" type="text/javascript">
setTimeout(function() {
    document.forms["frm_redirect_dfr"].submit();
    }, 0) /* You may need a higher number here. If you do, start with 100 and go up. */
</script>

You could also hang it off a document ready handler or something similar, but I don't know the raw JavaScript to do it (jQuery provides just such a hook, though).

The reason this works is that it's a type of 'yield'. Browsers tend to give lower priority to rending the page elements than executing inline JavaScript. This presents a problem with inline JavaScript has to interact with the DOM as there is no obvious way to wait for the browser to render it. This trick gives the browser an opportunity to get some rendering done before more JavaScript runs.


That should work fine, assuming the generated html is not mangled by your lack of escaping the values of the php variables.

Your problem might be in your script which receives the post. Do a print_r($_POST); to help you debug.

But, like Michael Mrozek said, you probably don't need to do this at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜