简体   繁体   English

使用javascript提交表单,可以在FF中使用,但不能在IE中使用

[英]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 下面的代码在Firefox中有效,但在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 我想做的是,当用户访问页面时,它首先将尝试获取登录数据,在表单中回显它,然后使用javascript自动提交表单。

Update: I forget to mention that the code above is on a Frame, the mainFrame code is like below: 更新:我忘了提到上面的代码在Frame上,mainFrame代码如下:

<?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="login_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. 您将完全使用PHP可用的信息来生成表单,然后将其提交回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 没有理由,您只需在脚本中包含$data['url']指向的任何文件,然后直接为其提供用户名和密码即可

That should work fine, assuming the generated html is not mangled by your lack of escaping the values of the php variables. 假定生成的html没有因缺少转义php变量的值而损坏,那应该可以正常工作。

Your problem might be in your script which receives the post. 您的问题可能出在收到该帖子的脚本中。 Do a print_r($_POST); 做一个print_r($_POST); to help you debug. 帮助您调试。

But, like Michael Mrozek said, you probably don't need to do this at all. 但是,就像Michael Mrozek所说的那样,您可能根本不需要这样做。

You probably need to give IE time to render the form before you make it submit it. 在提交表单之前,您可能需要给IE时间呈现该表单。 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). 您也可以将其挂在文档就绪处理程序或类似的程序上,但是我不知道要使用原始JavaScript(不过jQuery提供了这样的挂钩)。

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. 与执行内联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. 这就提出了内联JavaScript必须与DOM交互的问题,因为没有明显的方法等待浏览器呈现它。 This trick gives the browser an opportunity to get some rendering done before more JavaScript runs. 这个技巧使浏览器有机会在运行更多JavaScript之前完成一些渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM