一切正常。但问题是当我添加新联系人时。页面不会返回到索引,如果我返回到索引中,则会添加新联系人。我用一些检查软件检查了我的代码,它说我的PHP行有一个错误。
<?php
require_once"connection.php";
?>
<!DOCTYPE html>
<html>
<head>
<?php include"includes/head.inc"; ?>
<script>tinymce.init({selector:'textarea'});</script>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<div class="header">
<div class="headerContent"><h1>CONTACT LIST</h1></div>
</div>
<!-- content section -->
<div class="content">
<div><h1>Create New Contact</h1></div>
<hr>
<div class="contact">
<div class="contact_insert">
<form action="insert_contact.php" method="post">
<table style="float:left" width="50%">
<tr>
<td>Name:</td>
<td><input type="text" name="name" placeholder="name" size="40%"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" placeholder="email" size="40%"></td>
</tr>
<tr>
<td>Department:</td>
<td><select name ="department" </td>
</tr>
<tr>
<td>Extension Number:</td>
<td><input type="text" name="extension" placeholder="extension" size="40%"></td>
</tr>
<tr>
<td>Cellphone:</td>
<td><input type="text" name="cellphone" placeholder="cellphone" size="40%"></td>
</tr>
</table>
<div class="clear"></div>
<input class="insert_contact_button" type="submit" name="submit" value="Add Contact">
<a href="index.php"><input class="cancel_contact_button" type="button" value="Cancel"></a>
</form>
</div>
<div class="clear"></div>
</div>
</body>
</html>
<?php
< ---IT SAYS THE ERROR IS HERE
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$department = $_POST['department'];
$extension = $_POST['extension'];
$cellphone = $_POST['cellphone'];
$insert_contact = "insert into contacts (name, email, department, extension, cellphone) values ('$name', '$email', '$department', '$extension', '$cellphone')";
$sql_insert_contact = $conn->query($insert_contact);
$to = "manie@titan-networks.co.za";
$from = "ExtensionList@alpinemotors.co.za";
$subject = "New Staff Added To Extension List";
$message = "New Staff: " . "'n'n" . "Name : " . $name . " " . "'n'n" . "Email: " . $email . " " . "'n'n" . "Department: " . $department . " " . "'n'n" . "Extension: " . $extension . " " . "'n'n" . "Cellphone: " . $
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
if ($sql_insert_contact == true) {
header("Location: index.php");
}
}
?>
尝试添加ob_start();右键单击顶部第一个打开php标记下方的行。在您的情况下,尝试将顶部更改为:
<?php
ob_start();
require_once "connection.php";
?>
您还应该考虑添加ob_end_flush();在文件的最底部
不能在html之后使用Header函数。在渲染任何内容之前,先将代码放到顶部。
在发送任何实际输出之前,必须调用header(),无论是通过文件中的普通HTML标记、空行还是从PHP。使用include或require、函数或其他文件访问函数读取代码,并且在调用header()之前输出空格或空行,这是一个非常常见的错误。
<?php
header("Location: index.php") //This will be succeed
?>
<html>
//some html
</html>
<?php
header("Location: index.php") //This will fail
?>
尝试这个
if (isset($sql_insert_contact)) {
header("Location: index.php");
}
或
if ($sql_insert_contact) {
header("Location: index.php");
}