我有一些php代码会创建一个字段,然后是其他一些代码,你将信息放入文本框,然后在另一个php文件中显示文本框中输入的数据。
我有这个代码和截图。 这将创建名字,年龄等字段。 每次我点击这里提交,它都会转到方法post add.php。我不是在这里提供代码,因为它没关系。
添加字段的index.php的屏幕截图:
添加字段的代码
<?php
function renderForm($fieldname, $fieldtype, $fieldgroup,$error)
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>FIELDS</title>
<style>
label{
float: left;
margin-right: 0.5em;
}
</style>
</head>
<body>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<h1>ADD FIELDS</h1>
<form action="" method="post">
<label for = "fieldname"> Field Name: <input type="text" name="fieldname"><br>
<br></label>
<label for = "fieldtype"> Field Type: <select name="fieldtype">
<option value="Text">Text</option>
<option value="Date/Time">Date/Time Picker</option>
<option value="Number">Number</option>
</select>
</label>
<label for = "fieldgroup"> Field Group: <select name="fieldgroup">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
</label>
<input type="submit" name="submit" value="Submit">
</form>
<h1>VIEW</h1>
<?php
include('connect.php');
$result = mysql_query("SELECT * FROM fields")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Field Name</th> <th>Field Type</th><th>Field Group</th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['fieldname'] . '</td>';
echo '<td>' . $row['fieldtype'] . '</td>';
echo '<td>' . $row['fieldgroup'] . '</td>';
echo '<td><a href="edit.php?id=' . $row['ID'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['ID'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
<?php
}
include('connect.php');
if (isset($_POST['submit']))
{
$fieldname = mysql_real_escape_string(htmlspecialchars($_POST['fieldname']));
$fieldtype = mysql_real_escape_string(htmlspecialchars($_POST['fieldtype']));
$fieldgroup = mysql_real_escape_string(htmlspecialchars($_POST['fieldgroup']));
if ($fieldname == '' || $fieldtype == '' || $fieldgroup == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($fieldname, $fieldtype, $fieldgroup, $error);
}
else
{
mysql_query("INSERT INTO fields (fieldname,fieldtype,fieldgroup) VALUES('$fieldname','$fieldtype', '$fieldgroup')") or die (mysql_error());
header("Location: index.php");
}
}
else
{
renderForm('','','','','');
}
?>
view.php的截图:
此代码用于创建显示之前字段的表单。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
include('connect.php');
$result = mysql_query("SELECT * FROM fields")
or die(mysql_error());
echo "<form action = 'test.php' method='POST'>
<table border='1' cellpadding='10'>";
echo "<tr> <th>Field Name</th> <th>Value</th></tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td> <name= "lbl[]" value ='. $row['ID'] . '"\>'. $row['fieldname'] . '</td>';
echo "<td> <input type ='text' name= 'val[]'> </td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
echo "<input type ='submit' name='Submit' value='Submit'>";
echo "</form>"?>
</body>
</html>
点击提交后,转到test.php,这是代码。这是我遇到问题的地方。它不显示view.php中插入的值
<?php
if(isset($_POST['val[]']))
{
$valid = $_POST['val[]'];
for($a = 0; $a<$valid.length; $a++)
{
echo $valid[$a];
}
}
?>
答案 0 :(得分:0)
从你的帖子中删除[],使它看起来像这样:$ _POST ['val']
试试这个
<?php
if(isset($_POST['val']))
{
$valid = $_POST['val'];
for($a = 0; $a<$valid.length; $a++)
{
echo $valid[$a];
}
}
&GT;