2015-09-29 48 views
0

我是wordpress插件開發新手。我做了一些編碼和足夠了解如何製作菜單 - 子菜單等。在一個子菜單頁面中,我製作了一個表單,用於在數據庫中的一個mytable中更改電子郵件ID。但要停止頁面上的數據插入重新加載/刷新/頁面返回我必須提供一個標題功能,但我無法做到這一點。WP頭重新加載

更簡單的話我只是想停止wordpress插件插入/更新頁面刷新表中的數據。請閱讀我的編碼。

<form method="post" enctype="multipart/form-data"> 
    <label><b>Mention Your Email Here :</b></label> 
    <input type="email" name="email" id="email" placeholder="Enter your email here"/><br/><br/> 
    <input type="submit" name="set" id="set" value="CHANGE"/> 
</form> 

<br/><br/> 

<u>NOTE</u> :<br/> 
1. Please write your email id in the field given above. This email id will be used to get emails of visitors contact query. 
<br/> 
2. Copy the shortcode given below to show the form on the website.<br/> 
&nbsp;&nbsp;&nbsp;&nbsp;<b>[myplugin]</b> 

<?php 

//getting the form email id value 

if (isset($_POST['set']) && !empty($_POST['email'])) { 
    $email = $_POST['email']; 
    global $wpdb; 
    $table_name = $wpdb->prefix . "myemail"; 
    $result = $wpdb->update($table_name, array('id' => '1', 'email' => $email), array('id' => '1')); 

    if ($result > 0) { 
     echo "<script>alert('Successfully Updated');</script>"; 
    } else { 
     exit(var_dump($wpdb->last_query)); 
    } 

    $wpdb->flush(); 
} else { 
    header('location: ------?????--------------'); 
} 

?> 

在頁眉中,我應該寫什麼,以便頁面在重新加載後進入同一頁面。但沒有插入/更新數據。

回答

0

你可以重定向到同一頁面:

header('Location: '.$_SERVER['REQUEST_URI']); 
exit(); 

PHP Manual

'REQUEST_URI' 這是爲了訪問此頁面所需的URI。比如'/index.html'。

對於header()工作中,表單處理腳本將需要運行之前HTML輸出,所以你需要把它勾到合適的WP動作鉤子(如「WP」)。表單將在模板或包含中,並且處理函數通過WordPress add_action()掛鉤。在保存到數據庫之前,您還需要清理數據。