[英]Echo PHP variable in foreach loop
我的主页设置如下。 它有2个文本框和一个从文本文件填充的复选框列表(我没有使用db作为其简单形式)。 我的问题是我无法发布复选框中的值并回显它们
<!DOCTYPE HTML>
<html>
<body>
<?php
$CN = $Assignee ="";
?>
<form action="GPParser1.php" method="post">
<table>
<tr>
<td>CName:</td>
<td><input type="text" name="CN"></td>
</tr>
<tr>
<td>Assignee:</td>
<td><input type="text" name="Assignee" ></td>
<tr>
<tr>
<td>Select Vendors:</td>
</tr>
<td>
<?php
$gp = file('GP.txt');
foreach ($gp as $line_num => $gp)
{
//echo '<input type="checkbox" name= $line value=$line>($line)."<br />\n";
print "
<br/>
<input type='checkbox' name='" . $gp . "' value='" . $gp . "'>$gp";
};
?>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit">
</td>
<?php
echo $CasinoName;
echo $Assignee;
//foreach ($gp as $ln => $gp) {
// echo $gp;
//};
?>
</body>
<html>
和:
<!DOCTYPE HTML>
<html>
<body>
<?php
$CN = $_POST["CN"];
$Assignee = $_POST["Assignee"];
$GPS = $_POST ["$gp"];
echo $CN;
echo $Assignee;
foreach ($GPS as $GPss => $GPS)
{
echo "$GPS";
}
//foreach ($gp as $ln => $gp) {
// echo $gp;
//};
?>
</body>
<html>
现在,我遇到了一些基于$ GPS = $ _POST [“ $ gp”]的错误;
我该怎么办?
在这两个脚本中,您都将覆盖foreach循环中的变量
foreach ($gp as $line_num => $gp)
// this ^^^
// ^^^ overwrites this
和
foreach ($GPS as $GPss => $GPS)
// this ^^^
// ^^^ overwrites this
因为您在as
两边都使用了相同的变量名
而是使用另一个这样的变量名称
foreach ($gp as $line_num => $gpx)
通常的方法是使用复数名称(例如$items
和单变量witout)来命名包含数组的数组变量,例如
foreach ($gps as $line_num => $gp)
如果我输入错误,我不确定这是否是您正在寻找的解决方案,请让我知道,以便更好地回答。
我的解决方案是您的表单将具有一个复选框列表,该列表稍后将在提交表单时成为值数组。
<!DOCTYPE HTML> <html> <body> <?php $CN = $Assignee =""; ?> <form action="GPParser1.php" method="post"> <table> <tr> <td>CName:</td> <td><input type="text" name="CN"></td> </tr> <tr> <td>Assignee:</td> <td><input type="text" name="Assignee"></td> <tr> <tr> <td>Select Vendors:</td> </tr> <td> <?php $gp = file('GP.txt'); foreach ($gp as $line_num => $gp) { ?> <p><input type="checkbox" name="gp[]" value="<?php echo $gp; ?>"> <?php echo $gp; ?> </p> <?php }; ?> </td> </tr> <tr> <td></td> <td> <input type="submit"> </td> </tr> </body> </html>
<!DOCTYPE HTML> <html> <body> <?php $CN = $_POST["CN"]; $Assignee = $_POST["Assignee"]; $GPS = $_POST["gp"]; echo $CN; echo $Assignee; foreach ($GPS as $GPss => $GPS) { echo "$GPS"; } //foreach ($gp as $ln => $gp) { // echo $gp; //}; ?> </body> <html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.