设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 330|回复: 1

Automatically submit a form when page is load

[复制链接]
发表于 2012-6-4 14:34:12 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2012-6-5 06:49 编辑

Use javascript to do it. sample as:
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Logwrite.aspx.vb" Inherits="_Default" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Test - </title>
  6. </head>
  7. <body text="#000099" vlink="#660099" onload="javascript: myfunc()">
  8.     <form id="form1" runat="server">
  9.     <div id="Identity">
  10.         Welcome! ---
  11.     </div>
  12.     </form>
  13. <form id="frm" action="http://web2.com/second/answer.php" method=get>
  14. <input type=hiden name="name" value='<asp:Label ID="str_namestring" runat="server" />' >
  15. <input type="Submit" ID="sub1" Value="Submit">
  16. </form>
  17. <script type="text/javascript">
  18. function myfunc () {
  19. //var frm = document.getElementById("name");
  20. frm.submit();
  21. }
  22. window.onload = myfunc;
  23. </script>
  24.     </body>
  25. </html>
复制代码
and on server, a PHP named answer.php to get the submit:
  1. <?php
  2. $myname = $_GET["name"];
  3. $myname = str_replace("<span id="str_namestring">","",$myname);
  4. $myname = str_replace("</span>","",$myname);
  5. echo "<html><head><title>Response</title></head>";
  6. echo "<body>Welcome $myname !";
  7. echo "</body></html>";
  8. ?>
复制代码





 楼主| 发表于 2012-6-4 15:22:06 | 显示全部楼层

Extend .NET text string processiong

本帖最后由 demo 于 2012-6-5 07:23 编辑

As you can see, I have some text processing in PHP to make sure just get the value of the variable "name". This is because of variable created by .NET program and we can hardly remove it.

We can use Javascript to standarize it before submit and that make the program more friendly.

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Logwrite.aspx.vb" Inherits="_Default" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Test - </title>
  6. </head>
  7. <body text="#000099" vlink="#660099" onload="javascript: myfunc()">
  8.     <form id="form1" runat="server">
  9.     <div id="Identity">
  10.         Welcome! ---
  11.     </div>
  12.     </form>
  13. <form id="frm" action="http://test2/jzx/answer.php" method=get>
  14. <input type=hiden name="name" value='<asp:Label ID="str_LogID" runat="server" />' >
  15. <input type="Submit" ID="sub1" Value="Submit">
  16. </form>
  17. <script type="text/javascript">
  18. function myfunc () {
  19.   //var mym = document.getElementById("name");
  20.   //var msg = mym.value.replace("<span id="str_LogID">","").replace("</span>","");
  21.   document.getElementById("name").value = document.getElementById("name").value.replace("<span id="str_LogID">","").replace("</span>","");
  22.   //alert("This is from:" + mym + ". and that is " + msg);
  23.   document.forms["frm"].submit();
  24. //var frm = document.getElementById("name");
  25. //frm.submit();
  26. }
  27. //window.onload = myfunc;
  28. </script>
  29.     </body>
  30. </html>
复制代码

and the PHP code can back to a short one:

  1.     <?php
  2.     $myname = $_GET["name"];
  3.     echo "<html><head><title>Response</title></head>";
  4.     echo "<body>Welcome $myname !";
  5.     echo "</body></html>";
  6.     ?>
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 23:57 , Processed in 0.013513 second(s), 15 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表