我正在为我的班级做一个项目。我们从来没有过数组,我正试图弄清楚如何使用用户输入的关联。我不知道我是否走对了路。我最终需要用键值对数组进行排序。但首先,如果我能得到一些反馈,那就太好了。我一直得到一个错误消息":语法错误,意外的'each' (T_STRING),期待"
我甚至得到我的数组正确的值,并将它们与数组相关联?我对数组很纠结。我也是php新手。
<HTML>
<HEAD>
<TITLE>Student Form</TITLE>
</HEAD>
<BODY>
<FORM METHOD="post" ACTION="final_project.php">
<P>Please enter your name: <INPUT TYPE="text" NAME="txtname" SIZE= 10></P>
<P>Please enter your id: <INPUT TYPE="text" NAME="txtid" SIZE= 10></P>
<P>Please enter your address: <INPUT TYPE="text" NAME="txtaddress" SIZE= 10></P>
<P>Please enter your cell phone number: <INPUT TYPE="text" NAME="txtcell" SIZE= 10></P>
<P>Please enter your Major: <INPUT TYPE="text" NAME="txtmajor" SIZE= 10></P>
<P>Please enter your E-mail address: <INPUT TYPE="text" NAME="txtemail" SIZE= 10></P>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Submit"></P>
</FORM>
<?php
$txtname = $_POST['txtname'];
$txtid = $_POST['txtid'];
$txtaddress = $_POST['txtaddress'];
$txtcell = $_POST['txtcell'];
$array = array(txtname=>$txtname, txtid=>$txtid, txtaddress=>$txtaddress, txtcell=>$txtcell);
for each ($txtid as $key => $array){
echo "Your first name is ".$txtname.", id number is ".$txtid[$key].", your address is ".$txtaddress.", phone number is ".$txtcell.".";
}
?>
</BODY>
</HTML>
谢谢
您的脚本包含语法错误,对于每个应该foreach。数组索引txtname应为'txtname',依此类推。最后,在foreach中,您使用了变量而不是数组。我认为应该是一个数组($array)。修改脚本
与
$array = array(txtname=>$txtname, txtid=>$txtid, txtaddress=>$txtaddress, txtcell=>$txtcell);
for each ($txtid as $key => $array){
echo "Your first name is ".$txtname.", id number is ".$txtid[$key].", your address is ".$txtaddress.", phone number is ".$txtcell.".";
}
$array = array('txtname'=>$txtname, 'txtid'=>$txtid, 'txtaddress'=>$txtaddress, 'txtcell'=>$txtcell);
foreach ($array as $value){
echo $value.'</br>';
}
您应该考虑一些修改。在尝试使用$_POST
值之前,最好检查它们是否存在。对接收到的任何用户输入进行消毒也是一个好主意,以确保保护自己免受可能的恶意内容的侵害。最后,如果使用字段名保存发布的值,则可以轻松引用它们来创建句子,而不必使用for
、foreach
、while
或do-while
循环遍历数组。
<?php
/*
* Array contains the desired fields. You could add txtmajor or txtemail
* for example in the future if you desire. This is an additional safety
* check as well. You should never blindly save all $_POST fields as a
* user with malicious intent or a third party may have modified the
* initial request to contain extra data.
*/
$fields = array("txtname", "txtid", "txtaddress", "txtcell");
// Ensure this was a POST request and not the initial GET request.
if(strtolower($_SERVER['REQUEST_METHOD']) === "post") {
// Create an array to store the posted values
$values = array();
/*
* Loop through the $fields array you created above and make sure
* the post array contains the key. This will ensure that you don't
* leave a required field out or make a typo in your HTML.
*/
foreach($fields as $value) {
/*
* If the key exists and the value was not left blank, strip it
* for any malicious tags, trim off leading and trailing white
* space and save it in your $values array with the field name
* as the key. This makes it easy to reference the value in the
* future.
*
* Note: This is minimal sanitation. You should always take
* check your user's input especially if you plan to
* redisplay it on the screen at a later time or store
* it in a database.
*/
if(array_key_exists($value, $_POST) && !empty($_POST[$value])) {
$values[$value] = trim(strip_tags($_POST[$value]));
} else {
/*
* If the key doesn't exist, there may be a typo in your HTML
* or the returned content may have been manipulated by a third
* party person, proxy or service. Whatever the case, kill the
* script and render the plain HTML.
*/
exit;
}
}
/*
* Finally, since you know the field names and you want to use the
* information in a single sentence, there's no point in looping back
* through the array. Reference the values and echo the sentence...
*/
echo "Your first name is " . $values['txtname'] . ", id number is " . $values['txtid'] . ", your address is " . $values['txtaddress'] . ", phone number is " . $values['txtcell'] . ".";
}
?>