У меня есть код php, который создаст поле, а затем другой код, в который вы помещаете информацию в текстовое поле, а затем в другой 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>
\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 }
?>
Удалите '[]' из вашего сообщения val, сделайте это так: '$ _POST ['val']' – Epodax