我先稍微讲解一下,我有三个PHP的FORM,叫viewMember.php, editMember.php, editMemberAction.php,现在有一写undefined的问题。
请先看完我三个文件,分别是viewMember.php
<html>
<form name="viewMemberForm" method="post">
<center>
<h1>View Member Data</h1><br>
<table border="1" cellspacing="0" cellpadding="5" bordercolor="green">
<tr>
<td>Search by:</td>
<td><select name="searchBy">
<option value="MemberID">Member ID
<option value="FirstName">First Name
<option value="LastName">Last Name
</select>
</td>
<td rowspan="2"><input type="submit" name="search" value="Search">
<input type="submit" name="showAll" value="Show All">
</td>
</tr>
<tr>
<td>Keywords:</td>
<td><input type="text" name="keywords" onfocus="setStyle(this)"></td>
</tr>
</table>
<?php
include("viewMemberAction.php");
?>
</form>
</html>
这个editMember.php
<?php
include ("connect.php");
?>
<html>
<head>
<script type="text/javascript">//allow alphabets only
var alphaOnly = /[A-Za-z]/g; //fname,lname,mname
function restrictCharacters(myfield, e, restrictionType) {
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
// if they pressed esc... remove focus from field...
if (code == 27) { this.blur(); return false; }
// ignore if they are press other keys
// strange because code: 39 is the down key AND ' key...
// and DEL also equals .
if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40)
{
if (character.match(restrictionType))
return true;
else
return false;
}
}
</script>
<SCRIPT TYPE="text/javascript">//allow number only
<!--
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
// decimal point jump
else if (dec && (keychar == "."))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}
//-->
</SCRIPT>
<title>
Edit Member
</title>
</head>
<body>
<h1>Edit Member's Information</h1><br>
<?php
include("connect.php");
echo "<form name='editMemberForm' method='POST' action='editMemberAction.php'>";
$memberID = $_GET["memberID"];
echo "<div align='right' ><input type='submit' name='Cancel' value='Back to View Member'></div>";
echo "<br>";
$query = "SELECT * FROM member_register WHERE memberID = '$memberID'";
$result = $db->query($query);
$row = $result->fetch_assoc();
echo "<tr>";
echo "<td>";
echo "<table border='3' align='left' cellpadding='5' cellspacing='0' bordercolor='black' bgcolor='yellow' width='50%' rules='all'>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Member ID:</td>";
echo "<td><input type='hidden' name='memberID' value='".$memberID."'>".$memberID."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>FirstName:</td>";
echo "<td><input type='text' name='FirstName' onkeypress='return restrictCharacters(this, event, alphaOnly);' value='".$row["firstName"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>LastName:</td>";
echo "<td><input type='text' name='LastName' onkeypress='return restrictCharacters(this, event, alphaOnly);' value='".$row["lastName"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Job Type:</td>";
echo "<td width='200px'><input type='text' maxlength='65' name='JobType' value='".$row["jobType"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Gender:</td>";
echo "<td><select name='Gender'>";
$typeArray = array("Male","Female");
for($i = 0; $i < 2 ; $i++)
{
if($row["gender"] == $typeArray[$i])
{
echo "<option selected>".$typeArray[$i];
}
else
{
echo "<option>".$typeArray[$i];
}
}
echo "</select></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>I.C No.:</td>";
echo "<td><input type='text' name='ic' size='15' onKeyPress='return numbersonly(this, event)' value='".$row["iCNo"]."'>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Address:</td>";
echo "<td width='200px'><input type='text' maxlength='5' size='65' name='Address' value='".$row["address"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Zipcode:</td>";
echo "<td width='200px'><input type='text' maxlength='5' name='ZipCode' onKeyPress='return numbersonly(this, event)' value='".$row["zipcode"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Handphone Number:</td>";
echo "<td width='200px'><input type='text' maxlength='12' name='PhoneNumber' onKeyPress='return numbersonly(this, event)' value='".$row["handphoneNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Telefon Number:</td>";
echo "<td width='200px'><input type='text' maxlength='9' name='TelefonNumber' onKeyPress='return numbersonly(this, event)' value='".$row["telefonNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Fax Number:</td>";
echo "<td width='200px'><input type='text' maxlength='11' name='Fax' onKeyPress='return numbersonly(this, event)' value='".$row["faxNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Email:</td>";
echo "<td width='200px'><input type='text' maxlength='65' name='Email' value='".$row["email"]."'></td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "<td>";
echo "<table border='3' align='right' valign='top' cellpadding='5' cellspacing='0' bordercolor='white' bgcolor='orange' width='50%' rules='all'>";
echo "<tr>";
echo "<td align='right' width='35px'><b>MortPayment:</td>";
echo "<td width='200px'>RM :<input type='text' maxlength='65' name='MortPayment' value='".$row["mortPayment"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Current Redeem Point:</td>";
echo "<td width='200px'>RM :<input type='text' maxlength='65' name='MortPayment' value='".$row["redeemPoint"]."'><br>Add:<input type='number' step='4' min='4'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Borrowed Book Histories:</td>";
echo "<td width='200px'></td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2' align='center'><input type='submit' name='update' value='Confirm'></td>";
echo "</tr>";
?>
</form>
</body>
</html>
以及最后这个editMemberAction.php
<?php
include("connect.php");
$memberID = $_POST["memberID"];
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$sex = $_POST["sex"];
$ic = $_POST["ic"];
$address = $_POST["Address"];
$zipcode = $_POST["ZipCode"];
$phoneNum = $_POST["phoneNumber"];
$telefonNum = $_POST["telefonNumber"];
$fax = $_POST["fax"];
$email = $_POST["email"];
$jobtype = $_POST["jobtype"];
$mortpayment = $_POST["redeempoint"];
$defaultRedeemPoint = $_POST["defaultRedeemPoint"];
if(isset($_POST["update"]) == "Confirm")
{
$query = "UPDATE member_register SET memberID = '$memberID', firstName = '$FirstName', lastName = '$LastName', gender = '$sex', iCNo = '$ic', address = '$address', zipcode = '$zipcode', handphoneNo = '$phoneNum', telefonNo = '$telefonNum', faxNo = '$fax', email = '$email', jobType= '$jobtype', mortPayment = '$mortpayment', redeemPoint='$defaultRedeemPoint' WHERE memberID = '$memberID'";
$result = $db->query($query);
echo "<script language='javascript'>alert('Update success.')</script>";
echo "<script language='javascript'>window.location='viewMember.php'</script>";
}
if(isset($_POST["Cancel"]) == "Back to View Member")
{
echo "<script>alert('Now redirect to View Member Information.')</script>";
echo "<script language='javascript'>window.location='viewMember.php'</script>";
}
$db->close();
?>
当我在editMember.php按 confirm button 之后是能够显示成功提交的,但是有问题出现,如下
Notice:Undefined index : sex in C:\xampp\htdocs\cms\editMemberAction.php on line 7
Notice:Undefined index : phoneNumber in C:\xampp\htdocs\cms\editMemberAction.php on line 11
Notice:Undefined index : telefonNumber in C:\xampp\htdocs\cms\editMemberAction.php on line 12
Notice:Undefined index : fax in C:\xampp\htdocs\cms\editMemberAction.php on line 13
Notice:Undefined index : email in C:\xampp\htdocs\cms\editMemberAction.php on line 14
Notice:Undefined index : jpbtype in C:\xampp\htdocs\cms\editMemberAction.php on line 15
Notice:Undefined index : redeempoint in C:\xampp\htdocs\cms\editMemberAction.php on line 16
Notice:Undefined index : defaultRedeemPoint in C:\xampp\htdocs\cms\editMemberAction.php on line 17
请先看完我三个文件,分别是viewMember.php
<html>
<form name="viewMemberForm" method="post">
<center>
<h1>View Member Data</h1><br>
<table border="1" cellspacing="0" cellpadding="5" bordercolor="green">
<tr>
<td>Search by:</td>
<td><select name="searchBy">
<option value="MemberID">Member ID
<option value="FirstName">First Name
<option value="LastName">Last Name
</select>
</td>
<td rowspan="2"><input type="submit" name="search" value="Search">
<input type="submit" name="showAll" value="Show All">
</td>
</tr>
<tr>
<td>Keywords:</td>
<td><input type="text" name="keywords" onfocus="setStyle(this)"></td>
</tr>
</table>
<?php
include("viewMemberAction.php");
?>
</form>
</html>
这个editMember.php
<?php
include ("connect.php");
?>
<html>
<head>
<script type="text/javascript">//allow alphabets only
var alphaOnly = /[A-Za-z]/g; //fname,lname,mname
function restrictCharacters(myfield, e, restrictionType) {
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
// if they pressed esc... remove focus from field...
if (code == 27) { this.blur(); return false; }
// ignore if they are press other keys
// strange because code: 39 is the down key AND ' key...
// and DEL also equals .
if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40)
{
if (character.match(restrictionType))
return true;
else
return false;
}
}
</script>
<SCRIPT TYPE="text/javascript">//allow number only
<!--
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
// decimal point jump
else if (dec && (keychar == "."))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}
//-->
</SCRIPT>
<title>
Edit Member
</title>
</head>
<body>
<h1>Edit Member's Information</h1><br>
<?php
include("connect.php");
echo "<form name='editMemberForm' method='POST' action='editMemberAction.php'>";
$memberID = $_GET["memberID"];
echo "<div align='right' ><input type='submit' name='Cancel' value='Back to View Member'></div>";
echo "<br>";
$query = "SELECT * FROM member_register WHERE memberID = '$memberID'";
$result = $db->query($query);
$row = $result->fetch_assoc();
echo "<tr>";
echo "<td>";
echo "<table border='3' align='left' cellpadding='5' cellspacing='0' bordercolor='black' bgcolor='yellow' width='50%' rules='all'>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Member ID:</td>";
echo "<td><input type='hidden' name='memberID' value='".$memberID."'>".$memberID."</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>FirstName:</td>";
echo "<td><input type='text' name='FirstName' onkeypress='return restrictCharacters(this, event, alphaOnly);' value='".$row["firstName"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>LastName:</td>";
echo "<td><input type='text' name='LastName' onkeypress='return restrictCharacters(this, event, alphaOnly);' value='".$row["lastName"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Job Type:</td>";
echo "<td width='200px'><input type='text' maxlength='65' name='JobType' value='".$row["jobType"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Gender:</td>";
echo "<td><select name='Gender'>";
$typeArray = array("Male","Female");
for($i = 0; $i < 2 ; $i++)
{
if($row["gender"] == $typeArray[$i])
{
echo "<option selected>".$typeArray[$i];
}
else
{
echo "<option>".$typeArray[$i];
}
}
echo "</select></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>I.C No.:</td>";
echo "<td><input type='text' name='ic' size='15' onKeyPress='return numbersonly(this, event)' value='".$row["iCNo"]."'>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Address:</td>";
echo "<td width='200px'><input type='text' maxlength='5' size='65' name='Address' value='".$row["address"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Zipcode:</td>";
echo "<td width='200px'><input type='text' maxlength='5' name='ZipCode' onKeyPress='return numbersonly(this, event)' value='".$row["zipcode"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Handphone Number:</td>";
echo "<td width='200px'><input type='text' maxlength='12' name='PhoneNumber' onKeyPress='return numbersonly(this, event)' value='".$row["handphoneNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Telefon Number:</td>";
echo "<td width='200px'><input type='text' maxlength='9' name='TelefonNumber' onKeyPress='return numbersonly(this, event)' value='".$row["telefonNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Fax Number:</td>";
echo "<td width='200px'><input type='text' maxlength='11' name='Fax' onKeyPress='return numbersonly(this, event)' value='".$row["faxNo"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Email:</td>";
echo "<td width='200px'><input type='text' maxlength='65' name='Email' value='".$row["email"]."'></td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "<td>";
echo "<table border='3' align='right' valign='top' cellpadding='5' cellspacing='0' bordercolor='white' bgcolor='orange' width='50%' rules='all'>";
echo "<tr>";
echo "<td align='right' width='35px'><b>MortPayment:</td>";
echo "<td width='200px'>RM :<input type='text' maxlength='65' name='MortPayment' value='".$row["mortPayment"]."'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Current Redeem Point:</td>";
echo "<td width='200px'>RM :<input type='text' maxlength='65' name='MortPayment' value='".$row["redeemPoint"]."'><br>Add:<input type='number' step='4' min='4'></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='right' width='35px'><b>Borrowed Book Histories:</td>";
echo "<td width='200px'></td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2' align='center'><input type='submit' name='update' value='Confirm'></td>";
echo "</tr>";
?>
</form>
</body>
</html>
以及最后这个editMemberAction.php
<?php
include("connect.php");
$memberID = $_POST["memberID"];
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$sex = $_POST["sex"];
$ic = $_POST["ic"];
$address = $_POST["Address"];
$zipcode = $_POST["ZipCode"];
$phoneNum = $_POST["phoneNumber"];
$telefonNum = $_POST["telefonNumber"];
$fax = $_POST["fax"];
$email = $_POST["email"];
$jobtype = $_POST["jobtype"];
$mortpayment = $_POST["redeempoint"];
$defaultRedeemPoint = $_POST["defaultRedeemPoint"];
if(isset($_POST["update"]) == "Confirm")
{
$query = "UPDATE member_register SET memberID = '$memberID', firstName = '$FirstName', lastName = '$LastName', gender = '$sex', iCNo = '$ic', address = '$address', zipcode = '$zipcode', handphoneNo = '$phoneNum', telefonNo = '$telefonNum', faxNo = '$fax', email = '$email', jobType= '$jobtype', mortPayment = '$mortpayment', redeemPoint='$defaultRedeemPoint' WHERE memberID = '$memberID'";
$result = $db->query($query);
echo "<script language='javascript'>alert('Update success.')</script>";
echo "<script language='javascript'>window.location='viewMember.php'</script>";
}
if(isset($_POST["Cancel"]) == "Back to View Member")
{
echo "<script>alert('Now redirect to View Member Information.')</script>";
echo "<script language='javascript'>window.location='viewMember.php'</script>";
}
$db->close();
?>
当我在editMember.php按 confirm button 之后是能够显示成功提交的,但是有问题出现,如下
Notice:Undefined index : sex in C:\xampp\htdocs\cms\editMemberAction.php on line 7
Notice:Undefined index : phoneNumber in C:\xampp\htdocs\cms\editMemberAction.php on line 11
Notice:Undefined index : telefonNumber in C:\xampp\htdocs\cms\editMemberAction.php on line 12
Notice:Undefined index : fax in C:\xampp\htdocs\cms\editMemberAction.php on line 13
Notice:Undefined index : email in C:\xampp\htdocs\cms\editMemberAction.php on line 14
Notice:Undefined index : jpbtype in C:\xampp\htdocs\cms\editMemberAction.php on line 15
Notice:Undefined index : redeempoint in C:\xampp\htdocs\cms\editMemberAction.php on line 16
Notice:Undefined index : defaultRedeemPoint in C:\xampp\htdocs\cms\editMemberAction.php on line 17
解决方案 »
- 安装Moodle平台时出现问题
- ecshop 后台菜单问题?
- Warning: include(includelink.php) [function.include]: failed to open stream: No
- 求 PHP 设置管理员帐号session的有效期 代码
- 帮忙改写下一个插件
- 关于 eval 的优化,基于使用eval这个方法实现效率低下的问题
- php 读取Word文档的问题
- !!!!急急急 访问页面报404
- MYSQL 如何把数据插入到空缺的ID中去
- PHP中给AJAX传回的数据类型是JSON的一点困惑
- 求一段rewrite规则
- 关于地图的问题,知道的朋友请进来帮忙,互相帮助
include("connect.php");
$memberID = $_POST["memberID"];
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$sex = $_POST["sex"];
$ic = $_POST["ic"];
$address = $_POST["Address"];
$zipcode = $_POST["ZipCode"];
$phoneNum = $_POST["phoneNumber"];
$telefonNum = $_POST["telefonNumber"];
$fax = $_POST["fax"];
$email = $_POST["email"];
$jobtype = $_POST["jobtype"];
$mortpayment = $_POST["redeempoint"];
$defaultRedeemPoint = $_POST["defaultRedeemPoint"];你的$_POST数组不完备,对应下标的数据没有,所以产生了notice
解决方案是:
$sex = isset($_POST['sex']) ? $_POST['sex'] : false; //false是默认值你也可以自己写个方法进行处理
也试过$phoneNum = isset($_POST["phoneNumber"])?$_POST['phoneNumber']:false;确实没有错误,但是没有返回值了,以上资料只要按CONFIRM之后就会消失变成空(NULL)。
echo "isset:$sex\r\n";我本身用了以上方式作测试,返回值居然是"test is undefined!",这代表返回值是FALSE的,为什么会这样呢?
请调整到一致,不可简单的回避,否则将造成数据损坏比如 sex 在表中和表单中都对应 gender,令 $sex = $_POST["gender"]; 即可