我有一个简单的表单,当我提交时,重定向回homepic.php(包括)。我在localhost上使用wamp服务器。这与它有什么关系吗?表格信息将通过电子邮件发送给收件人,我尚未编码。这是代码:
<?php
$field_name = $field_email = $field_message = "";
$nameError = $emailError = $messageError = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["cf_name"])) {
$nameError = "Name is required";}
else{$field_name = test_input($_POST['cf_name']);
if (!preg_match("/^[a-zA-Z ]*$/",$field_name)) {
$nameError = "Only letters and white space allowed";
}
}
if (empty($_POST["cf_email"])) {
$emailError = "Email is required";}
else{$field_email = test_input($_POST['cf_email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$field_email)) {
$emailError = "Invalid email format";
}}
if (empty($_POST["cf_message"])) {
$messageError = "Message is required";}
else{$field_message = test_input($_POST['cf_message']);}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
Your name<br />
<input type="text" name="cf_name" value="<?php echo $field_name;?>">
<span class="error">* <?php echo $nameError;?></span><br />
Your e-mail<br />
<input type="text" name="cf_email" value="<?php echo $field_email;?>">
<span class="error">* <?php echo $emailError;?></span><br />
Message<br />
<textarea name="cf_message" value="<?php echo $field_message;?>"></textarea>
<span class="error">* <?php echo $messageError;?></span><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
这是我的索引导航:
<div class="main">
<?php switch ($_GET['page']) {
case 'home':
include ('homepic.php');
break;
case 'about':
include ('about.php');
break;
case 'contact':
include ('contact_form.php');
break;
case 'newwork':
include ('ainewwork.php');
break;
case 'nature':
include ('nature.php');
break;
case 'structures':
include ('structures.php');
break;
case 'other':
include ('other.php');
break;
default:
include ('homepic.php');
break;
}
?>
这是我的导航代码:
<nav>
<ul id="menu">
<li class="left"><a href="index.php?page=home">home</a></li>
<li><a href="index.php?page=about">about</a></li>
<li class="top"><a href="#">portfolios</a>
<ul class="item"><li ><a href="index.php?page=newwork">new work</a></li>
<li ><a href="index.php?page=nature">nature</a></li>
<li ><a href="index.php?page=structures">structures</a></li>
<li ><a href="index.php?page=other">other</a></li>
</ul>
</li>
<li class="right"><a href="index.php?page=contact">contact</a></li>
</ul>
</nav>
答案 0 :(得分:0)
如果您更改开关代码以允许GET / POST变量。您可以根据您正在执行的操作定义该var。
<?php switch ($_REQUEST['page']) {
case 'home':
include ('homepic.php');
break;
case 'about':
include ('about.php');
break;
case 'contact':
include ('contact_form.php');
break;
case 'newwork':
include ('ainewwork.php');
break;
case 'nature':
include ('nature.php');
break;
case 'structures':
include ('structures.php');
break;
case 'other':
include ('other.php');
break;
default:
include ('homepic.php');
break;
}
?>
然后在表单中,您可以像这样维护变量。
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Your name<br />
<input type="text" name="cf_name" value="<?php echo $field_name;?>">
<span class="error">* <?php echo $nameError;?></span><br />
Your e-mail<br />
<input type="text" name="cf_email" value="<?php echo $field_email;?>">
<span class="error">* <?php echo $emailError;?></span><br />
Message<br />
<textarea name="cf_message" value="<?php echo $field_message;?>"></textarea>
<span class="error">* <?php echo $messageError;?></span><br />
<input type="hidden" name="page" value="<?php echo $_GET['page']; ?>" />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>