发新话题
打印

用button来代替submit提交表单

用button来代替submit提交表单

用button来代替submit提交表单,经过测试完全可行。但下面的代码有一个小失误,在点击button时,会报错,修改一下就可以了。
复制内容到剪贴板
代码:
<?php
$a="Submit, Reset  and  Button's Method";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$a;?></title>
<script language="javascript">
<!--
//check s is empty or not
function checks(s)
{
var len=s.length;
for(var i=0;i<len;i++)
  {
  if(s.charAt(i)!="")
    {
    return false;
    }
  }
  return true;
}

//check text.value is right or not
function textcheck()
{
if(checks(document.whb.username.value))
  {
  window.alert("Don't let username empty!");
  return false;
  }
else
  {
  return true;
  }
}

//use button to post form
  function postformbybutton()
  {
  if(checks(document.whb.username.value))
   {
   window.alert("Don't let username empty!");
   }
  else
   {
   document.whb.submit();
   //post form information
   }
  }
   
//use button to Reset  
  function resetformbybutton()
  {
  document.whb.reset();
  //reset form information
  }  
//-->
</script>
</head>
<body >
<div align="center">
  <br>
  <form name="whb" action="http://127.0.0.1/index.php" method="get">
   <table width=400 border=1 cellpadding=0 cellspacing=0 align=center bordercolor=green>
    <tr height=50 >
     <td colspan=2 align=center ><?=$a;?></td>
    </tr>
    <tr height=50 >
      <td align=right >UserName:&nbsp&nbsp </td>
      <td>&nbsp <input type="text" name="username" value="Don't Empty UserName! " size=35 onfocus="document.whb.username.select()" >
      </td>
    </tr>
    <tr height=50 >
     <td align=right >Password:&nbsp </td>
     <td>&nbsp <input type="password" name="pin" size=36 ></td>
    </tr>
    <tr height=120 >
     <td align=right >Your Sign:&nbsp&nbsp </td>
     <td>&nbsp&nbsp
       <textarea name="sign" cols=30 rows=6 onfocus="this.select()" > Leave your step at here~~~~</textarea>
     </td>
    </tr>
    <tr height=50 >
      <td colspan=2 align=center >
       <input type="submit" name="submit" value="submit" onclick="return textcheck()" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
       <input type="reset" name="reset" value="reset">
      </td>
    </tr>
    <tr height=50 >
     <td colspan=2 align=center >
        <input type="button" name="Sure" value="Sure" onclick="postformbybutton()" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
       <input type="button" name="redo" value="Redo" onclick="resetformbybutton()" >
     </td>
    </tr>
   </table>
  </form>
  </div>
<div align=center>
<input type="button" name="back" value="BACK" onclick=window.location="index.php">
</div>
</body>
</html>

TOP

继续努力........
学会怎么调试程序,学会怎么设计模式,学会怎么配置环境,学会怎么带项目,学会怎么分析客户需求

TOP

!

测试了下,
用户登录,
我看不懂

TOP

引用:
原帖由 lxylxy8866 于 2007-10-14 18:00 发表
测试了下,
用户登录,
我看不懂
你这么测试,将上边的http://127.0.0.1/index.php换为"http://www.sohu.com"或是别的网站,点击Subimt会直接进去SOHU网站(或是你换的那个网站),而你点击Sure时,则不行,当然Reset和Redo也是同理,但你改了那个小失误后,就会发现Sure和Redo的功能与Submit和Reset的功能一样了,而你不用与理会表单的内容(UserName和Passwrod,这没用的,只是测试而已,已经预设了值,不用改动).不知我说的明白不?

TOP

为什么不直接用,应该可以的

TOP

发新话题