2013-04-01 86 views
0

我正在創建一個網頁,其中有兩個div(帳單詳細信息和送貨詳細信息)。加載頁面後,系統會自動顯示結算明細,並且運送詳情爲空。我已經包含了兩個單選按鈕,允許用戶選擇運送詳情是否與結算明細相同。如果用戶從單選按鈕中選擇「是」,則應在運送詳情中顯示相同的細節。 注:詳細信息都存儲在數據庫中正在使用PHP來獲得數據顯示單擊單選按鈕時如何填充文本字段?

目前,我只用

<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?> 

第一名稱字段只是爲了看它是否正在嘗試,但我似乎沒有工作。

<div id="leftprofile"> 
    <form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php"> 
    <fieldset class="billing"> 
     <legend>Billing Details</legend><br /> 
     <label for="fname" class="reglabel">First Name:</label> 
     <input required name="fname" type="text" id="fname" value="<?php echo $fname ?>"/><br /> 
     <label for="lname" class="reglabel">Last Name:</label> 
     <input required name="lname" type="text" id="lname" value="<?php echo $lname ?>"/><br /> 
     <label for="address" class="reglabel">Address:</label> 
     <input required name="address" id="address" type="text" value="<?php echo $address ?>"/><br /> 
     <label for="town" class="reglabel">Town:</label> 
     <input required name="town" id="town" type="text" value="<?php echo $town ?>"/><br /> 
     <label for="postcode" class="reglabel">Post Code:</label> 
     <input required name="postcode" id="postcode" type="text" value="<?php echo $postcode ?>"/><br /> 
     <label for="phone" class="reglabel">Phone:</label> 
     <input required name="phone" id="phone" type="text" value="<?php echo $phone ?>"/><br /> 
     <label for="email" id="EmailLabel" class="reglabel">E-mail:</label> 
     <input required name="email" type="email" id="email" value="<?php echo $email ?>"/><br /> 
    </fieldset> 
    </form> 
</div> 

<div id="rightprofile"> 
    <form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php"> 
    <fieldset class="billing"> 
     <legend>Shipping Details</legend><br /> 
     <form> 
      Same as billing address? 
      <input type="radio" name="shipping" id="yes" value="yes">Yes 
      <input type="radio" name="shipping" id="no" value="no">No<br/> 
     </form> 
     <label for="fname" class="reglabel">First Name:</label> 
     <input required name="fname" type="text" id="fname" value="<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>"/><br /> 
     <label for="lname" class="reglabel">Last Name:</label> 
     <input required name="lname" type="text" id="lname" /><br /> 
     <label for="address" class="reglabel">Address:</label> 
     <input required name="address" id="address" type="text" /><br /> 
     <label for="town" class="reglabel">Town:</label> 
     <input required name="town" id="town" type="text" /><br /> 
     <label for="postcode" class="reglabel">Post Code:</label> 
     <input required name="postcode" id="postcode" type="text" /><br /> 
     <label for="phone" class="reglabel">Phone:</label> 
     <input required name="phone" id="phone" type="text" /><br /> 
     <label for="email" id="EmailLabel" class="reglabel">E-mail:</label> 
     <input required name="email" type="email" id="email" /><br /> 
    </fieldset> 
    </form> 
</div> 
+1

那麼,輸入的名稱既可以是結算和運輸方式,也可以是相同的,所以當您最終獲得提交的表單時,您會遇到問題。此外,這需要在Javascript中完成,因爲它需要在表單提交之前處理。 (也應該注意,一次只能提交一個表單,現在你有兩個單獨的表單提交到同一個位置。) – Jon

+0

他們是不同的div,但文本字段是相同的 – littledevils326

+0

準確地說我的觀點。如果所有提交到相同的「行動」,您需要區分開票和運輸字段名稱。 – Jon

回答

1

我寫過一些簡單的示例代碼。該網站包含一個表單,其中包含兩個輸入字段(結算和運輸)以及一個用於檢查運送信息是否與結算信息相同的複選框。如果複選框被選中,代碼將簡單地忽略輸入「shipping」的任何內容。

這將實現你所要求的,至少從PHP的角度來看。如果您在瀏覽器中實時查找「在這些輸入字段中複製數據到這些輸入字段」,那麼這是Javascript的任務,而不是PHP。

<?php 
/* Check if anything was submitted */ 
if(isset($_POST)) 
{ 
    /* Retrieve billing information */ 
    $billing_name = $_POST['billing_name']; 
    $billing_addr = $_POST['billing_addr']; 

    /* Check if shipping same as billing */ 
    if(isset($_POST['same'])) 
    { 
     /* Shipping is the same as billing */ 
     $shipping_name = $billing_name; 
     $shipping_addr = $billing_addr; 
    } 
    /* If not, set shipping to the posted value */ 
    else 
    { 
     $shipping_name = $_POST['shipping_name']; 
     $shipping_addr = $_POST['shipping_addr']; 
    } 

    $insert = mysql_query(...); 
} 
?> 

<form method="post" action="#" /> 

Billing information 
<label for="billing_name">Name</label> 
<input type="text" id="billing_name" name="billing_name" /> 
<label for="billing_addr">Addr</label> 
<input type="text" id="billing_addr" name="billing_addr" /> 

<label for="same" />Is the shipping information the same as billing information?</label> 
<input type="checkbox" id="same" name="same" /> 

Shipping information 
<label for="shipping_name">Name</label> 
<input type="text" id="shipping_name" name="shipping_name" /> 
<label for="shipping_addr">Addr</label> 
<input type="text" id="shipping_addr" name="shipping_addr" /> 

<input type="submit" value="Register" /> 

</form> 
相關問題