2016-03-02 112 views
0

我在製作管理面板。當用戶將數據輸入到文本字段時,我想將文本字段中的數據複製到另一個文本字段。 我的問題是代碼的作品,直到我添加到數據庫功能的插入到同一個按鈕。 我如何獲得執行這兩個功能的按鈕。有沒有更好的方法來做onclick函數。由於我使用兩個文本字段,但我真的希望一個是產品的標題。像下面的圖片中:Onclick將輸入文字複製到另一個位置

enter image description here

代碼是

<?php 
    $add_product_errors = array(); 

    if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
     // price validate - must be decimal(float) 
     if (empty($_POST['price']) || !filter_var($_POST['price'], FILTER_VALIDATE_FLOAT) || ($_POST['price'] <= 0)) { 
      $add_product_errors['price'] = "Please enter a product price"; 
     } 
     // item name validate 
     if (empty($_POST['item_name'])) { 
      $add_product_errors['item_name'] = "Please enter a name"; 
     } 
     // item name description 
     if (empty($_POST['desc'])) { 
      $add_product_errors['desc'] = "Please enter a product description"; 
     } 

     //add to database 
     //if (empty($add_product_errors)) { 
     $q = 'INSERT INTO Product (Product_Name,Product_Desc,Product_Price) VALUES (?,?,?)'; 
     $stmt = mysqli_prepare($dbc, $q); 

     //debugging 
     $stmt = mysqli_prepare($dbc, $q) or die(mysqli_error($dbc)); 

     mysqli_stmt_bind_param($stmt, 'sss', $item_name, $desc, $price); 
     $item_name = strip_tags($_POST['item_name']); 
     $desc = strip_tags($_POST['desc']); 
     //100 - changes the way the decimal displays in database 
     $price = strip_tags($_POST['price'] * 100); 

     //execute the query 
     mysqli_stmt_execute($stmt); 
     printf("%d Item Added.\ ", mysqli_stmt_affected_rows($stmt) === 1); { 
      mysqli_stmt_close($stmt); 
     } 
    } 
    ?> 

HTML

<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" > 
      <input type="textfield" id="title" name="title" value=""> 
      <input type="textfield" id="item_name" name="item_name" placeholder="item name" value="" <?php $add_product_errors ?>/><br> 
      <textarea id="desc" name="desc" value="" placeholder="Item description" rows="3" maxlength="200" required ><?php $add_product_errors ?></textarea><br> 
      <input type="textfield" id="price" name="price" value="" placeholder="£" maxlength="30" required <?php $add_product_errors ?>/><br> 
      <input type="submit" name="add" value="add" value="add" class="btn" onclick="myFunction()"> 

      <!-- copy item_name to page title --> 
      <script> 
       function myFunction() { 
        document.getElementById("title").value = document.getElementById("item_name").value; 
       }  
      </script> 
+0

你的問題是什麼?你說它不起作用,但是預期的結果是什麼,你得到了什麼? –

+0

@ J.Bush我期待提交按鈕中的數據顯示在另一個文本字段中,並且輸入的數據也會插入到數據庫中。這兩個功能都是單獨工作,但不是在一起。 – jerneva

+0

它完全打破了嗎?或者是插入到數據庫中的數據,但是文本沒有顯示在其他字段中? –

回答

1

考慮將提交輸入更改爲常規按鈕,以便可以完全控制控制流。它也可能使控制流程更清晰,供您理解。

<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" onsubmit="myFunction()"> 
... your HTML here ... 
<input type="button" name="add" value="add" value="add" class="btn" onclick="myFunction()"> 
</form> 
<script> 
    function myFunction() { 
    // Update your field value 
    document.getElementById("title").value = document.getElementById("item_name").value; 

    // Submit your form. 
    document.forms["product_form"].submit(); 
      }  
</script> 
+0

嗨,謝謝你。我厭倦了這一點,但因爲我添加了本地存儲,我不得不使用輸入類型=提交 – jerneva

+1

得到它的工作。 – jerneva

+0

@ jerneva如果我的答案適合您,您能否請我的答案標記爲「答案」? –

0

您可以將文本複製綁定在onsubmit事件是這樣的:

<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" onsubmit="myFunction()"> 
+0

嗨,謝謝。但我只想要item_name重複提交 – jerneva

相關問題