2016-10-04 62 views
0

我有一個表單捕獲輸入值作爲會話參數。其中一個輸入是具有5種不同選項(手機運營商)的選擇。我希望表單提交id(carrier_id),但在確認頁上回顯相關值(carrier_name)。回聲關聯的行值

事情是這樣的:

<form action="confirm_form.php" role="form"> 
<div class="form-group"> 
<label for="carrier>Select Carrier</label> 
<select type="text" class="form-control" name="carrier" value=""> 
    <?php 
     $q = "SELECT * FROM carriers"; 
     $carriers = mysqli_query($con, $q); 
    while($row = mysqli_fetch_assoc($carriers)) { 
$carrier_id = $row['car_id']; 
$carrier_name = $row['car_name']; 

echo "<option value='$carrier_id'>$carrier_name</option>"; 
} 
?> 
</select> 
</div> 
<button type="submit" value="submit" name="submit">Add customer</button> 
</form> 


$carrier = mysqli_real_escape_string($con, $_SESSION['carrier']); 

的confirm_form.php頁面應該回聲出運營商的名稱,但是當我嘗試了兩種輸出CARRIER_ID還是空白。請幫忙。我也假設其他因素在我沒有輸出的快速概述中。 (我已經有在session_start();調用時,一個)

+0

設置此線while循環中' echo「」;' – AHJeebon

+0

是的。謝謝你指出這個錯誤。但這不是它不能正確輸出的原因。我更新了我的代碼。 –

+0

爲什麼你想使用會話?您可以從$ _POST獲得價值。 – AHJeebon

回答

0

主要形式和Set方法交

<form action="confirm_form.php" role="form" method="POST"> 
    <div class="form-group"> 
    <label for="carrier">Select Carrier</label> 
    <select class="form-control" id="carrier" name="carrier"> 
     <?php 
      $q = "SELECT * FROM carriers"; 
      $carriers = mysqli_query($con, $q); 
      while($row = mysqli_fetch_assoc($carriers)){ 
      $carrier_id = $row['car_id']; 
      $carrier_name = $row['car_name']; 
      echo "<option value='$carrier_id'>$carrier_name</option>"; 
      } 
     ?> 
    </select> 
    </div> 
    <button type="submit" value="submit" name="submit">Add customer</button> 
</form> 

confirm_form.php

<?php 
    session_start(); 
    if(isset($_POST['carrier'])){ 
    $_SESSION['carrier']=$_POST['carrier']; 
    $carrier = mysqli_real_escape_string($con, $_SESSION['carrier']); 

    //Then query here......... 

    echo $_SESSION['carrier']; 
    } 

?> 
+0

已編輯立即檢查並通知我是否工作。 – AHJeebon