我有一些PHP代碼,將創建一個字段,然後一些其他的代碼,你把信息放入文本框,然後在另一個PHP文件,它顯示在文本框中輸入的數據。動態控制數組PHP
我有這段代碼和截圖。 這將創建像名字,年齡等字段。 每次我點擊這裏提交,它會去方法後add.php。我沒有在這裏提供代碼,因爲沒關係。
截圖供的index.php,增加字段:
代碼添加字段
<?php
function renderForm($fieldname, $fieldtype, $fieldgroup,$error)
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>FIELDS</title>
<style>
\t label{
\t float: left;
\t margin-right: 0.5em;
\t }
\t </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>
\t <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 \t
{
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>"; \t
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
\t if(isset($_POST['val[]']))
\t {
\t \t \t $valid = $_POST['val[]'];
\t \t
\t \t \t
\t \t \t for($a = 0; $a<$valid.length; $a++)
\t \t \t {
\t \t \t \t echo $valid[$a];
\t \t \t }
\t }
?>
從你的post val中刪除'''',使它看起來像這樣:'$ _POST ['val']' – Epodax