请问 关于 form的问题

langkejianxin1999 2016-07-11 12:22:13
请问 , win8, apache php 和 mysql平台

在安装apache的目录下的\Apache24\htdocs 有如下两个文件:mytest.php 和 mylogin.js
\Apache24\htdocs\mytest.php
\Apache24\htdocs\mylogin.js 也就是这两个文件在同一目录下
当在浏览器运行 mytest.php的时候,浏览器显示如图1所示:

图1
当输入 用户名和密码后,点击 “登录”按钮

但是 此时 没有 弹出 alert 提示框的,为什么不弹出呢?
因为mytest.php 代码里边 onSubmit="javascript:return OnSubmit() ,正常情况下当点击 sumit 按钮的时候, 程序就会调用 OnSubmit()函数的。

也就是说没有弹出alert提示框, 程序没有调用 mylogin.js里边的 OnSubmit()函数的。

另外 mytest.php 里边 有 onFocus="javascript:OnPassword(), 这句代码应该怎么理解呢? 是应该 当输入完 用户名后,鼠标点击 密码输入的地方,这时候 密码栏就是 处于 focus状态了,那么是不是 此时 就会触发 mylogin.js里边的OnPassword()呢?或者说 它什么时候 才会调用 mylogin.js里边的OnPassword()呢? (因为整个过程都没有弹出alert提示框,也就是没有调用mylogin.js里边的OnPassword()了)

麻烦知道怎么处理的朋友帮忙回复,谢谢啦
mytest.php 代码如下:

<html>
<head>
<title></title>
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
<tr>
<td valign="middle">
<div class="form">
<form method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();">
<table border="1" align="right" cellpadding="1" cellspacing="1">
<tr>
<td width="68" class="labName">用户名:</td>
<td><input type="text" name="txtName" id="txtName"></td>
</tr>
<tr>
<td class="labPWD">密码:</td>
<td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td>
</tr>
<tr>
<td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td>
</tr>
</table>
</form>
</div>
</div></td>
</tr>
</table>
</body>
</html>


mylogin.js 代码如下:(也就是mylogin.js 只是写了9行代码,除此之外什么都没有了
function OnSubmit(){
alert("hello world");
return true;
}
var abc = false;
function OnPassword(){
alert("false");
abc= false;
}
...全文
143 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
u014567183 2016-07-11
  • 打赏
  • 举报
回复
<script type="text/javascript" src = "mylogin.js"></script> 这么引入。直接include 它怎么知道这段代码是js呢?
langkejianxin1999 2016-07-11
  • 打赏
  • 举报
回复
引用 1 楼 u014567183 的回复:
你没引入mylogin.js 啊,那怎么让它起作用呢?
@ u014567183 谢谢您的回复, 我把mytest.php 修改如下了,但是还是没有探出 alert提示框。麻烦再帮忙回复下,谢谢啦 即在原来的基础上 添加了 <? php include "./mylogin.js"; ?>
langkejianxin1999 2016-07-11
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
你的 mytest.php 中并没有加载 mylogin.js 自然就不会有 mylogin.js 中相应的动作
@ xuzuning 谢谢您的回复 我把mytest.php 修改如下了,但是还是没有探出 alert提示框。 即在原来的基础上 添加了 <? php include "./mylogin.js"; ?> 麻烦再帮忙回复,谢谢啦 修改后mytest.php代码如下: <? php include "./mylogin.js"; ?> <html> <head> <title></title> </head> <body onLoad="document.getElementById('txtName').focus()"> <span style="color: #FFF"></span> <table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;"> <tr> <td valign="middle"> <div class="form"> <form method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();"> <table border="1" align="right" cellpadding="1" cellspacing="1"> <tr> <td width="68" class="labName">用户名:</td> <td><input type="text" name="txtName" id="txtName"></td> </tr> <tr> <td class="labPWD">密码:</td> <td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td> </tr> <tr> <td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td> </tr> </table> </form> </div> </div></td> </tr> </table> </body> </html>
xuzuning 2016-07-11
  • 打赏
  • 举报
回复
你的 mytest.php 中并没有加载 mylogin.js 自然就不会有 mylogin.js 中相应的动作
u014567183 2016-07-11
  • 打赏
  • 举报
回复
你没引入mylogin.js 啊,那怎么让它起作用呢?
wodebaobei12611 2016-07-11
  • 打赏
  • 举报
回复
引用 13 楼 langkejianxin1999 的回复:
[quote=引用 10 楼 u014567183 的回复:] 额,保存的时候编码没注意, 你看5-12行就可以。

<script type="text/javascript">
 <script type="text/javascript">
    <?php 
    if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
@ u014567183 谢谢您的回复 还想请问下,为什么我以下的方法是不行的呢? 因为在php里边获取 用户名信息后,就可以通过这信息判断,做相应的跳转。比如调转到 登录后的界面之类的,等等。 <?php $username = $_POST['txtName']; alert($username); ?> <html> <head> <title></title> <script type="text/javascript" src = "mylogin.js"></script> <script type="text/javascript"> </script> </head> <body onLoad="document.getElementById('txtName').focus()"> <span style="color: #FFF"></span> <table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;"> <tr> <td valign="middle"> <div class="form"> <form action="mytest.php" method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();"> <table border="1" align="right" cellpadding="1" cellspacing="1"> <tr> <td width="68" class="labName">用户名</td> <td><input type="text" name="txtName" id="txtName"></td> </tr> <tr> <td class="labPWD">密码</td> <td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td> </tr> <tr> <td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td> </tr> </table> </form> </div> </div></td> </tr> </table> </body> </html> [/quote]把<?php $username = $_POST['txtName']; alert($username); ?> 改为 <?php if ($_POST) { $username = $_POST['txtName']; echo "<script>alert($username);</script>"; } ?>
u014567183 2016-07-11
  • 打赏
  • 举报
回复
因为你第一次访问的时候$_POST是没有值的,所以你需要做下判断。 当你提交了以后,$_POST才有值,才要alert。 $username = $_POST['txtName']; alert($username); php中是没有alert的,所以你要写成这样 echo "<script>alert('$username');</script>"; 这样浏览器就能解析出这是这段是js,才能alert.
langkejianxin1999 2016-07-11
  • 打赏
  • 举报
回复
引用 10 楼 u014567183 的回复:
额,保存的时候编码没注意, 你看5-12行就可以。

<script type="text/javascript">
 <script type="text/javascript">
    <?php 
    if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
@ u014567183 谢谢您的回复 还想请问下,为什么我以下的方法是不行的呢? 因为在php里边获取 用户名信息后,就可以通过这信息判断,做相应的跳转。比如调转到 登录后的界面之类的,等等。 <?php $username = $_POST['txtName']; alert($username); ?> <html> <head> <title></title> <script type="text/javascript" src = "mylogin.js"></script> <script type="text/javascript"> </script> </head> <body onLoad="document.getElementById('txtName').focus()"> <span style="color: #FFF"></span> <table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;"> <tr> <td valign="middle"> <div class="form"> <form action="mytest.php" method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();"> <table border="1" align="right" cellpadding="1" cellspacing="1"> <tr> <td width="68" class="labName">用户名</td> <td><input type="text" name="txtName" id="txtName"></td> </tr> <tr> <td class="labPWD">密码</td> <td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td> </tr> <tr> <td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td> </tr> </table> </form> </div> </div></td> </tr> </table> </body> </html>
u014567183 2016-07-11
  • 打赏
  • 举报
回复
当然也可以在<head>里写 <?php if($_POST){ $username = $_POST['txtName']; echo "<script>alert('$username');</script>";} ?>
引用 11 楼 u011516112 的回复:
[quote=引用 10 楼 u014567183 的回复:] 额,保存的时候编码没注意, 你看5-12行就可以。

<script type="text/javascript">
 <script type="text/javascript">
    <?php 
    if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
js中写php?[/quote]
wodebaobei12611 2016-07-11
  • 打赏
  • 举报
回复
引用 10 楼 u014567183 的回复:
额,保存的时候编码没注意, 你看5-12行就可以。

<script type="text/javascript">
 <script type="text/javascript">
    <?php 
    if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
js中写php?
u014567183 2016-07-11
  • 打赏
  • 举报
回复
额,保存的时候编码没注意, 你看5-12行就可以。

<script type="text/javascript">
 <script type="text/javascript">
    <?php 
    if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
u014567183 2016-07-11
  • 打赏
  • 举报
回复

<html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script> 
<script type="text/javascript">
	<?php 
	if($_POST){
  $username = $_POST['txtName'];
  echo "alert('$username');";
}
?>
</script> 
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
  <tr>
    <td valign="middle">
        <div class="form">
          <form action="test.php" method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();">
            <table border="1" align="right" cellpadding="1" cellspacing="1">
              <tr>
                <td width="68" class="labName">Óû§Ãû£º</td>
                <td><input type="text" name="txtName" id="txtName"></td>
              </tr>
              <tr>
                <td class="labPWD">ÃÜÂ룺</td>
                <td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td>
              </tr>
              <tr>
                <td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td>
              </tr>
            </table>
          </form>
        </div>
      </div></td>
  </tr>
</table>
</body>
</html> 
langkejianxin1999 2016-07-11
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
<script src="./mylogin.js";></script>
@ xuzuning 谢谢您的回复,麻烦再帮忙回复下,谢谢啦 请问 为什么我在代码里边边 添加了以下的代码, 但是运行后,是会出现错误提示: Undefined index: txtName <?php $username = $_POST['txtName']; alert($username); ?> 也在form里边添加了 <form action="mytest.php" method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();">
langkejianxin1999 2016-07-11
  • 打赏
  • 举报
回复
引用 5 楼 u014567183 的回复:
<script type="text/javascript" src = "mylogin.js"></script> 这么引入。直接include 它怎么知道这段代码是js呢?
@ u014567183 谢谢您的回复 麻烦再帮忙回复下,谢谢啦 如果我想用 _POST[ ]方法获取 用户名的信息,用alert()弹出。 就是在原来的form表格里边添加了: action="mytest.php" 和在<? php 添加了: $username = $_POST['txtName']; alert($username); 但是运行后,是会出现错误提示: Undefined index: txtName mytest .php修改后的代码如下: <?php $username = $_POST['txtName']; alert($username); ?> <html> <head> <title></title> <script type="text/javascript" src = "mylogin.js"></script> </head> <body onLoad="document.getElementById('txtName').focus()"> <span style="color: #FFF"></span> <table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;"> <tr> <td valign="middle"> <div class="form"> <form action="mytest.php" method="post" enctype="application/x-www-form-urlencoded" onSubmit="javascript:return OnSubmit();"> <table border="1" align="right" cellpadding="1" cellspacing="1"> <tr> <td width="68" class="labName">用户名:</td> <td><input type="text" name="txtName" id="txtName"></td> </tr> <tr> <td class="labPWD">密码:</td> <td><input type="password" name="txtPwd" id="txtPwd" onFocus="javascript:OnPassword();"></td> </tr> <tr> <td height="45" colspan="3" align="center" valign="middle"><input type="image" src="image/admin/login/btnsubmit.jpg"></td> </tr> </table> </form> </div> </div></td> </tr> </table> </body> </html>
xuzuning 2016-07-11
  • 打赏
  • 举报
回复
<script src="./mylogin.js";></script>

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧