2016-06-18 15 views
0

我需要'確認表單重新提交'信息(查看查看源代碼時進入Chrome)的幫助重定向與發佈數據是可能的?

如果我刪除評論,它只顯示用戶名1,無論我選擇哪個ID,並且評論如下代碼,與重新提交信息的作品...

<?php 

    $username = get_username_from_db(1); 

    if (isset($_POST['id'])) { 
     $id = $_POST['id']; 
     $username = get_username_from_db($id); 

     /* 
     // if this comment stay as comment, username will update with 'confirm form resubmission' message 
     // else 'confirm form resubmission' message not shows but username not updated (keep as default [1]) 

     header('Location: ' . $_SERVER['HTTP_HOST']); 
     exit(); 
     */ 
    } 

?> 

<body> 
    <form method="POST"> 
     <fieldset> 
      <legend>Choose ID</legend> 
      <select name="id" onchange="this.form.submit()"> 
       <option value="1">1st</option> 
       <option value="2">2nd</option> 
       <option value="3">3rd</option> 
      </select> 
      <br> 
      <label>Username:</label> 
      <input type="text" name="username" value="<?php echo $username; ?>"> 
     </fieldset> 
    </form> 
</body> 

有什麼想法嗎?

回答

0

試試這個:

帶$ _GET的代碼;

<?php 

    $username = get_username_from_db(1); 

    if (isset($_POST['id'])) { 
     $id = $_POST['id']; 
     $username = get_username_from_db($id); 

     /* 
     // if this comment stay as comment, username will update with 'confirm form resubmission' message 
     // else 'confirm form resubmission' message not shows but username not updated (keep as default [1]) 
     */ 
     $url = $_SERVER['HTTP_HOST'] . '?name='.$username; 
     header('Location: ' . $url); 
     exit(); 

    } 
    else if(isset($_GET['name']) && !empty($_GET['name'])){ 
     $username = $_GET['name']; 
    } 
?> 

<body> 
    <form method="POST"> 
     <fieldset> 
      <legend>Choose ID</legend> 
      <select name="id" onchange="this.form.submit()"> 
       <option value="1">1st</option> 
       <option value="2">2nd</option> 
       <option value="3">3rd</option> 
      </select> 
      <br> 
      <label>Username:</label> 
      <input type="text" name="username" value="<?php echo $username; ?>"> 
     </fieldset> 
    </form> 
</body> 

帶$ _SESSION的代碼;

<?php 

session_start(); 
$user_id = 1; 

if(isset($_SESSION['user_id'])){ 
    $user_id = intval($_SESSION['user_id']); 
} 
// prevent 0 and negative; 
if($user_id <= 0){ 
    $user_id = 1; 
} 
$username = get_username_from_db($user_id); 

if(isset($_POST['id'])){ 
    $id = intval($_POST['id']); 
    // prevent 0 and negative; 
    if($id <= 0){ 
     $id = 1; 
    } 
    $_SESSION['user_id'] = $id; 
    $url = $_SERVER['HTTP_HOST']; 
    header('Location: ' . $url); 
    exit(); 
} 
?> 

<body> 
    <form method="POST"> 
     <fieldset> 
      <legend>Choose ID</legend> 
      <select name="id" onchange="this.form.submit()"> 
       <option value="1">1st</option> 
       <option value="2">2nd</option> 
       <option value="3">3rd</option> 
      </select> 
      <br> 
      <label>Username:</label> 
      <input type="text" name="username" value="<?php echo $username; ?>"> 
     </fieldset> 
    </form> 
</body> 
+0

還有沒有使用GET的選項? –

+0

是的,你可以用php session()來做到這一點()只有你會更多的代碼待辦事項 – Paules

+0

用會話代碼編輯我的答案 – Paules

相關問題