如何重定向到一个帖子使用php在wordpress数据插入到数据库后


How to redirect to a post using php in wordpress after data insertion into DB

我是wordpress新手。我有一个程序,其中表单提交和数据插入是在同一个页面。

我想要的是当数据插入到数据库中,一个"谢谢"的帖子页面将生成。

我已经使用了头函数重定向,但它不工作。请看下面的程序。

我把else语句放错了吗?

与此同时,我想知道我将在哪里设置表单验证函数在这个程序?

请帮帮我。

程序如下

<!DOCTYPE html>
<html>
<style>
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
    width: 100%;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
input[type=submit]:hover {
    background-color: #45a049;
}
div {
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}
</style>
<body>
<h3>Form Name</h3>
[insert_php] if (!empty($_POST))

$cname=$_POST['cname'];
$pname=$_POST['pname'];
$webtype=$_POST['webtype'];
$contact=$_POST['contact'];
$email=$_POST['email'];
$address=$_POST['address'];
$pappdate=$_POST['pappdate'];
global $wpdb;
$wpdb->insert( 
    'tab_m_webdesign', 
    array( 
   'cname' => $cname,
   'pname' => $pname,
   'webtype' => $webtype,
   'contact' => $contact,
   'email' => $email,
   'address' => $address,
   'pappdate' => $pappdate
        ), 
    array( 
        '%s', 
        '%s', 
                '%s', 
'%s', 
'%s', 
'%s', 
'%s' 
    ) 
);
header("Location: http://example.com/myOtherpost");

[/insert_php]
<div>
[insert_php] else [/insert_php]
  <form action="" method="post" name="webForm">
    <label for="cname">Company/Institute Name</label>
    <input type="text" id="cname" name="cname">
    <label for="pname">Your Name</label>
    <input type="text" id="pname" name="pname">
    <label for="webtype">Type of Website</label>
    <select id="webtype" name="webtype">
      <option value="Personal(Static Pages)">Personal(Static Pages)</option>
      <option value="Personal(Dynamic Page)">Personal(Dynamic Page)</option>
      <option value="Commercial(Static Pages)">Commercial(Static Pages)</option>
      <option value="Commercial(Dynamic Page)">Commercial(Dynamic Page)</option>
      <option value="Online Shopping Site">Online Shopping Site</option>
      <option value="Educational Site">Educational Site</option>
      <option value="Other">Other</option>
    </select>
    <label for="contact">Contact No.</label>
    <input type="text" id="contact" name="contact">
    <label for="email">Email ID</label>
    <input type="text" id="email" name="email">
    <label for="address">Address</label>
    <input type="text" id="address" name="address">
    <label for="pappdate">Preferred Appointment Date</label>
    <input type="date" name="pappdate">
    <input type="submit" value="Submit">
  </form>
[insert_php] endif; [/insert_php]
</div>
</body>
</html>

Header函数必须在页面上的任何输出之前调用。你应该把php插入代码放在html标签之前。

另一种方法是在页面顶部添加ob_start()。