【问题标题】:How to redirect to a post using php in wordpress after data insertion into DB数据插入数据库后如何在wordpress中使用php重定向到帖子
【发布时间】:2017-03-09 18:44:14
【问题描述】:

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

我想要的是当数据插入数据库时​​会生成一个“谢谢”帖子页面。

我已经使用了header函数进行重定向,但是它不起作用。请看下面的程序。

我是否错误地放置了 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>

【问题讨论】:

    标签: php wordpress forms validation redirect


    【解决方案1】:

    必须在页面上的任何输出之前调用标题函数。 您应该将 php 插入代码放在 html 标记之前。

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

    【讨论】:

    • 我试过了,但是不行。请告诉解决方案具体意味着我应该写什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2013-10-02
    相关资源
    最近更新 更多