我刚接触PHP和HTML,并且有一个工作表单。现在我正在尝试添加功能。该表单基本上被多次调用以提交照片竞赛的照片。表单要求提供作者的姓名和要上传的文件。在处理完之后,它会再次显示表单,以便上传更多照片。
这就是它到目前为止的样子,似乎有效。
<?php
if (array_key_exists('Submitted',$_POST)) {
$photoCategory = $_POST['Category'];
$authorFirstName = $_POST['FirstName'];
$authorLastName = $_POST['LastName'];
// Process the form data here...
echo "If you have more photos to submit, continue below.<br>";
echo "If you have finished, just close this window.<br>";
}
?>
<!-- Set up the form -->
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="Submitted" value="true">
<label>First Name</label>
<input type="text" name="FirstName" size="20"> required<br>
<label>Last Name</label>
<input type="text" name="LastName" size="20" required><br>
<label>File 1 to upload</label>
<input type="file" name="Photo1"><br>
<label> </label>
<input type="submit" value="Upload"><br>
</form>
所以我的问题是:
这种结构是否是明智之举?
如果是这样,我怎样才能将此人的姓名设为第二个及后续个体的默认值?
答案 0 :(得分:0)
$_SESSION
并在流程中引用该值。同样适用于类别。请注意添加到输入的value
属性:
<?php
if (array_key_exists('Submitted',$_POST)) {
$photoCategory = $_POST['Category'];
$authorFirstName = $_POST['FirstName'];
$authorLastName = $_POST['LastName'];
// Process the form data here...
echo "If you have more photos to submit, continue below.<br>";
echo "If you have finished, just close this window.<br>";
}
?>
<!-- Set up the form -->
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="Submitted" value="true">
<label>First Name</label>
<input type="text" name="FirstName" value="<?php echo $_POST['FirstName'] ?? ''; ?>" size="20"> required<br>
<label>Last Name</label>
<input type="text" name="LastName" value="<?php echo $_POST['LastName'] ?? ''; ?>" size="20" required><br>
<label>File 1 to upload</label>
<input type="file" name="Photo1"><br>
<label> </label>
<input type="submit" value="Upload"><br>
</form>
答案 1 :(得分:0)
我不太明白你的第一个问题 - 语义上。关于“合理的方式”,你可以改写它。
您的页面设计很好。确保您没有在网页的html部分中混合服务器超级全局($ _GET,$ _POST等)。通常,html部分中php标记内的代码应该只显示普通变量和短语句。所以,人们永远不应该看到这样的事情:
<a href="<?php echo '/abc/' . $path1 . '/def=' . (100+0.53) . '&id=' . getId(); ?>">
或者像这样:
<a href="<?php echo $_POST['url']; ?>">
但只是这样:
<a href="<?php echo $url; ?>"
重要的是,您不仅要确保客户端(javascript,jQuery等)的输入值的验证,还要确保服务器端(PHP)的输入值的验证。尽可能多的验证非常好。
否则,尝试不仅为php变量维护“camelCase”表示法,而且还为html属性的值保留“camelCase”表示法。应该像class="my-perfect-notation"
一样注意css类。另请阅读this,this和this。第二个链接特别重要(也易于阅读)。
至于第二个问题,设置默认值的代码是直截了当的:如果设置了发布值,则打印它,否则打印一个空字符串。
祝你好运!<?php
if (isset($_POST['submitButton'])) {
$authorFirstName = isset($_POST['firstName']) ? $_POST['firstName'] : '';
$authorLastName = isset($_POST['lastName']) ? $_POST['lastName'] : '';
$photoCategory = isset($_POST['photoCategory']) ? $_POST['photoCategory'] : '';
// Input data validations.
if (empty($authorFirstName)) {
$errors[] = 'Please provide the first name.';
}
if (empty($authorLastName)) {
$errors[] = 'Please provide the last name.';
}
if (empty($photoCategory)) {
$errors[] = 'Please provide the photo category.';
}
// Input data processing.
if (!isset($errors)) {
// ...
$message = 'If you have more photos to submit, continue below.<br/>';
$message .= 'If you have finished, just close this window.<br/><br/>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Photo upload</title>
<style type="text/css">
.error {
padding: 10px;
margin-bottom: 15px;
width: 400px;
color: #c94e4e;
background-color: #fcc9c9;
}
.message {
margin-bottom: 15px;
}
</style>
</head>
<body>
<?php
// Display errors list.
if (isset($errors)) {
foreach ($errors as $error) {
?>
<div class="error">
<?php echo $error; ?>
</div>
<?php
}
}
?>
<?php
// Display message.
if (isset($message)) {
?>
<div class="message">
<?php echo $message; ?>
</div>
<?php
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="firstName">First Name *</label>
<input type="text" id="firstName" name="firstName" value="<?php echo isset($authorFirstName) ? $authorFirstName : ''; ?>" size="20" required>
<br/><br/>
<label for="lastName">Last Name *</label>
<input type="text" id="lastName" name="lastName" value="<?php echo isset($authorLastName) ? $authorLastName : ''; ?>" size="20" required>
<br/><br/>
<label for="photoCategory">Photo category</label>
<select id="photoCategory" name="photoCategory" value= "<?php echo $photoCategory; ?>" >
<option value="Landscape">Landscape</option>
<option value="Nature">Nature</option>
<option value="SpiritofBushwalking">Spirit of Bushwalking</option>
<option value="HandofMan">Hand of Man</option>
</select>
<br/><br/>
<label for="photo">Photo upload</label>
<input type="file" id="photo" name="photo">
<br/><br/>
<button type="submit" name="submitButton">
Upload
</button>
<br/><br/>
</form>
</body>
</html>