php的form表单使用js验证,贴出代码请高手帮我看看围合无法调用js验证
<?phperror_reporting('0');if($_POST["submit"]=="上传"){include("do_sellbook.inc.php");e...
<?php
error_reporting('0');
if($_POST["submit"] == "上传") {
include("do_sellbook.inc.php");
exit;
}
session_start();
if($_GET["id"]!="" && $_SESSION["userid"]==1 && is_numeric($_GET["id"])) {
$id = $_GET["id"];
}else {
$id = $_SESSION["userid"];
}
$sql = "select * from users where id={$id}";
mysql_query("set names gbk");
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
extract($row_1);
?>
<script language="javascript">
function check_form() {
bookname = document.getElementById("edit-name").value;
writer = document.getElementById("edit-writer").value;
price = document.getElementById("edit-price").value;
cond = document.getElementById("edit-cond").value;
emsg = "";
if(bookname == "") emsg += "用户名没有填写. \n";
if(writer == "") emsg += "作者没有填写. \n";
if(price == "") emsg += "价钱没有填写. \n";
if(cond == "") emsg += "几成新没有填写. \n";
if(emsg != "" ) {
alert(emsg);
return false;
}else {
return true;
}
}
</script>
<!-- begin content -->
<form action="sellbook.php" method="post" id="book_upload">
<div><div class="form-item">
<label for="edit-name">书名: </label>
<input type="text" maxlength="60" name="edit[bookname]" id="edit-name" size="30" value="" class="form-text required" />
</div>
<div class="form-item">
<label for="edit-writer">作者: </label>
<input type="text" maxlength="60" name="edit[writer]" id="edit-writer" size="30" value="" class="form-text required" />
</div>
<div class="form-item">
<label for="edit-price">价格:</label>
<input type="text" maxlength="60" name="edit[price]" id="edit-price" size="30" value="" class="form-text required"/>
</div>
<div class="form-item">
<label for="edit-cond">几成新: </label>
1 <input type="radio" name="edit[cond]" value="1" checked=checked/>
2 <input type="radio" name="edit[cond]" value="2" />
3 <input type="radio" name="edit[cond]" value="3" />
</div>
<input type="submit" name="submit" value="上传" class="form-submit" onclick="return check_form();"/>
<br /><br />
</div></form>
<!-- end content -->
在这个页面中即使所有项目都没填写也能提交,请高手帮我看看 展开
error_reporting('0');
if($_POST["submit"] == "上传") {
include("do_sellbook.inc.php");
exit;
}
session_start();
if($_GET["id"]!="" && $_SESSION["userid"]==1 && is_numeric($_GET["id"])) {
$id = $_GET["id"];
}else {
$id = $_SESSION["userid"];
}
$sql = "select * from users where id={$id}";
mysql_query("set names gbk");
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
extract($row_1);
?>
<script language="javascript">
function check_form() {
bookname = document.getElementById("edit-name").value;
writer = document.getElementById("edit-writer").value;
price = document.getElementById("edit-price").value;
cond = document.getElementById("edit-cond").value;
emsg = "";
if(bookname == "") emsg += "用户名没有填写. \n";
if(writer == "") emsg += "作者没有填写. \n";
if(price == "") emsg += "价钱没有填写. \n";
if(cond == "") emsg += "几成新没有填写. \n";
if(emsg != "" ) {
alert(emsg);
return false;
}else {
return true;
}
}
</script>
<!-- begin content -->
<form action="sellbook.php" method="post" id="book_upload">
<div><div class="form-item">
<label for="edit-name">书名: </label>
<input type="text" maxlength="60" name="edit[bookname]" id="edit-name" size="30" value="" class="form-text required" />
</div>
<div class="form-item">
<label for="edit-writer">作者: </label>
<input type="text" maxlength="60" name="edit[writer]" id="edit-writer" size="30" value="" class="form-text required" />
</div>
<div class="form-item">
<label for="edit-price">价格:</label>
<input type="text" maxlength="60" name="edit[price]" id="edit-price" size="30" value="" class="form-text required"/>
</div>
<div class="form-item">
<label for="edit-cond">几成新: </label>
1 <input type="radio" name="edit[cond]" value="1" checked=checked/>
2 <input type="radio" name="edit[cond]" value="2" />
3 <input type="radio" name="edit[cond]" value="3" />
</div>
<input type="submit" name="submit" value="上传" class="form-submit" onclick="return check_form();"/>
<br /><br />
</div></form>
<!-- end content -->
在这个页面中即使所有项目都没填写也能提交,请高手帮我看看 展开
2个回答
展开全部
楼上说错了,这样提交进行阻止是可以的。
问题出在单选框值的获取上。
var obj = document.getElementsByTagName("edit[cond]");
for(var i=0; i<obj.length; i ++){
if(obj[i].checked){
alert(obj[i].value);
}
}
}
单选框值的获取很麻烦的。
我觉得直接注释掉cond有关的两行就可以了。
<script language="javascript">
function check_form() {
bookname = document.getElementById("edit-name").value;
writer = document.getElementById("edit-writer").value;
price = document.getElementById("edit-price").value;
//cond = document.getElementById("edit-cond").value;
emsg = "";
if(bookname == "") emsg += "用户名没有填写. \n";
if(writer == "") emsg += "作者没有填写. \n";
if(price == "") emsg += "价钱没有填写. \n";
//if(cond == "") emsg += "几成新没有填写. \n";
if(emsg != "" ) {
alert(emsg);
return false;
}else {
return true;
}
}
</script>
这样
追问
您说的对!谢谢您,这件事困扰了我好几天了。。。
2014-12-08
展开全部
把check_form()的调用放到<form />的onsubmit方法中,或者把你那个提交按钮改成普通按钮,等验证通过后再用js提交,你将验证放到submit按钮的click事件中是不行的。
追问
我之前有试过您说的onsubmit那个方法,也是不行的啊。改成普通按钮的方法请问您能给我一下吗?感谢
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询