2016-08-29 11 views
-1

我想創建一個用於輸入帳單詳細信息的頁面,所以我創建了表單staffcontentCID.php其中包含我的表單。 該形式具有6個條目:useridcustomeridtransactioniditemcodeitemquantityitemprice並且該值被存儲在使用bill.php數據庫。 它是成功的,但需要更多的東西,當我提交表單時,所有的值都設置爲空,所以我想到了一些過程,在提交表單時,表單條目(userid,customerid,transactionid)將具有最後一個值輸入到它們中,這樣我就不必一次又一次重新輸入所有這些,並且其餘條目(itemcode,itemquantity,itemprice)將變爲空,因爲我可以使用先前輸入的相同ID輸入下一個產品。它會一直持續到最後一次,當我按打印按鈕進行票據打印時(我將在我的代碼中添加),所有值都將再次爲空。進行表格提交後,其中3個條目將會保持原樣,其餘3個將成爲空

形式的代碼是:

<div class="w3-half" style="display:inline;height:400px;"> 
<div class="w3-container"> 

<form action="./database/bill.php" method="post" style="margin-left:100px;margin-top:150px;"> 
<table cellspacing=20px> 

<tr> 
<td> 
<center> 
<b>User ID :</b> 
</center> 
</td> 
<td> 
<input name="uid" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Customer ID :</b> 
</center> 
</td> 
<td> 
<input name="cid" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Transaction ID :</b> 
</center> 
</td> 
<td> 
<input name="tid" type="varchar"> 
</td> 
</tr> 


<tr> 
<td> 
<center> 
<b>Item Code :</b> 
</center> 
</td> 
<td> 
<input name="ic" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Item Quantity :</b> 
</center> 
</td> 
<td> 
<input name="iq" type="number"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Item Price :</b> 
</center> 
</td> 
<td> 
<input name="ip" type="number"> 
</td> 
</tr> 

<tr> 
<td> 
</td> 
<td> 
<input type="submit"> 
</td> 
</tr> 
</table> 
</form> 

</div> 
</div> 

<div class="w3-half" style="display:inline;height:400px;"> 
<div class="w3-container"> 

transaction id : 
</div> 

</div> 

和代碼bill.php是

<?php 
//store.php 

// Create connection 
$conn = new mysqli('localhost','root','','master'); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

//go query 

$r = $_POST['cid']; 
$e = $_POST['uid']; 
$q = $_POST['ic']; 
$j = $_POST['tid']; 
$h = $_POST['ip']; 
$i = $_POST['iq']; 
$x = mysqli_query($conn,"insert into transactionb values('".$j."','".$r."','".$e."','".$q."','".$i."','".$h."')"); 
echo mysqli_error($conn); 
if($x){echo 'success'; 
echo '<script>window.location = "../staffentryCID.php"</script>';} 

$conn->close(); 
?> 

回答

1

您可以在此使用ajaxjQuery實現。

注:如果使用此那麼你的表單提交會在這個意義上充當不同的頁面沒有加載,並帶你到bill.php,而不是它的形式發送到bill.php和形式會插入數據庫而不離開當前頁面。其次,使用這個你必須包含jQuery庫到你的腳本。你可以用下面的代碼替換你的代碼。

<div class="w3-half" style="display:inline;height:400px;"> 
<div class="w3-container"> 

<form id="myForm" style="margin-left:100px;margin-top:150px;"> 
<table cellspacing=20px> 

<tr> 
<td> 
<center> 
<b>User ID :</b> 
</center> 
</td> 
<td> 
<input name="uid" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Customer ID :</b> 
</center> 
</td> 
<td> 
<input name="cid" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Transaction ID :</b> 
</center> 
</td> 
<td> 
<input name="tid" type="varchar"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Item Code :</b> 
</center> 
</td> 
<td> 
<input name="ic" type="varchar" id="ic"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Item Quantity :</b> 
</center> 
</td> 
<td> 
<input name="iq" type="number" id="iq"> 
</td> 
</tr> 

<tr> 
<td> 
<center> 
<b>Item Price :</b> 
</center> 
</td> 
<td> 
<input name="ip" type="number" id="ip"> 
</td> 
</tr> 

<tr> 
<td> 
</td> 
<td> 
<input type="submit"> 
</td> 
</tr> 
</table> 
</form> 

</div> 
</div> 

<div class="w3-half" style="display:inline;height:400px;"> 
<div class="w3-container"> 

transaction id : 
</div> 

</div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script> 
    $('form#myForm').submit(function(e){ 
     e.preventDefault(); // this will prevent the page from going to bill.php 
     $.ajax({ 
      url: "./database/bill.php", 
      type: "POST", 
      data: $(this).serialize(), 
      success: function(){ 
       // This code below will reset the values of the input field you want it be empty after form submission 
       $('input#ic').val(''); 
       $('input#iq').val(''); 
       $('input#ip').val(''); 
      } 
     }) 
    }) 
</script> 

現在你注意到了,我給了form一個id,你也希望它是表單提交後空input領域。然後,我重置他們的價值沒有任何表格提交後成功

+0

謝謝你的建議,我試過這段代碼,但有一個問題,我在執行此代碼後面臨。這樣在表單中輸入的值不會進入數據庫表,只要我點擊提交所有字段刷新而不是最後三個,也不會在表中插入值。還有一個問題出現時,我深深地再次檢查它,它寫在ajax方法「方法:後」但仍然不使用post方法發送條目,而是使用get方法,因爲每當我發送值的值顯示在地址欄 可能是有什麼缺失? –

+0

@Ardern如果你有問題的代碼,那麼我相信它是你的錯。使用'method:「POST」'或'type:「POST」'是一樣的。現在這是你如何解決錯誤。如果您在提交表單時重新加載頁面,那麼您在某處產生了錯誤。所以你檢查你的console.log。爲了獲得更好的結果,只需拿走你的代碼並用我的代碼替換它。並注意。如果你不在線,那麼代碼將不起作用,因爲'jQuery'庫鏈接需要網絡。但是如果你堅持要脫機使用它,那麼下載'jQuery'庫,並用你的下載文件替換腳本src,路徑爲 –

+0

,例如'

相關問題