0
這裏我所做的是通過for循環和mannualy創建輸入字段如果我們想添加一個字段,那麼我們可以通過追加函數添加,但我不明白如何爲每個產品&價格字段提供不同的名稱。我怎樣才能從字段追加提交輸入字段值,,,這裏是代碼..想通過附加按鈕創建字段時提交表單元素
<?php
mysql_connect('localhost','root','');
mysql_select_db('filters');
$select_no="";
?>
<!DOCTYPE html>
<html>
<head>
<title> filters program</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn1").click(function(){
$("ol").append("<li><input type='text' style='width:300px; height:30px; border-radius:10px;' name='item' placeholder='product..' value=''> <input type='text' style='width:100px;height:30px; border-radius:10px;' name='price' placeholder='price..'></li><br>");
});
});
</script>
</head>
<body>
<br>
<?php
//if condition for saving data in database table name filters and table name products....
if(isset($_POST['name_s'])){
session_start();
$x = $_SESSION["number_of_x"];
for ($a=1;$a<=$x;$a++){
$product=$_POST["items".$a];
$price=$_POST["price".$a];
echo "$a.) Product name is $product & ";
echo "its price is Rs. $price was ";
$sql= "INSERT INTO products (product, price) VALUES('$product','$price')";
$res=mysql_query($sql);
if ($res) {
echo "submitted Successfully in database !!<br><hr><br />";
}else{
echo "Not submitted because ".mysql_error();}
}
}else{
?>
<form method="POST" action="">
Select No. OF Fields <input type="number" style="width:100px;height:25px;border-radius:10px;" id="selct_no" name="select_no" value=""> To <input type="submit" name="create" id="create_fields" value="create" style="width:70px;border-radius:10px;"><br><br><br>
</form>
<button id='btn1' style="width:170px;border-radius:10px;">Add Input Field Mannualy</button>
<?php
}
//IF CONDITION FOR CREATIG ITEMS FIELD depend on user need....
if(isset($_POST['create'])){
$select_no=$_POST['select_no'];
// Start the session
session_start();
$_SESSION["number_of_x"] = $_POST['select_no'];
// loop for generating given no. of fields...
?>
<form method="POST" action="">
<ol>
<?php
for ($i=0; $i < $select_no ; $i++) {
global $x;
$x = $i+1;
echo "<div style='width:475px'>";
echo "<li><input type='text' style='width:300px; height:30px; border- radius:10px;' placeholder='Enter Items' id='id_item' name='items".$x."' value=''> <input type='number' style='width:100px;height:30px; border-radius:10px;' placeholder='Price' id='id_price' name='price".$x."' value=''> <br/><br/> ";
}
?>
</ol>
</div>
<div style='width:450px'>
<center>
<input type="submit" name='name_s' style='width:300px;height:35px;border- radius:10px' id='submit_cat'></input>
</center>
</div>
</form>
<?php } ?>
</body>
</html>
關 - 主題,但請縮進您的代碼。這有點難以遵循。 https://en.wikipedia.org/wiki/Indent_style – WillardSolutions
請不要使用[mysql_'數據庫擴展](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions- in-php),它不推薦使用(在PHP7中永遠不會使用)。特別是如果您只是學習PHP,請花些精力學習'PDO'數據庫擴展。 [從這裏開始](http://php.net/manual/en/book.pdo.php)它非常容易 – RiggsFolly
一些明智的代碼縮進將是一個好主意。它可以幫助我們閱讀代碼,更重要的是,它可以幫助您**調試您的代碼** [快速瀏覽編碼標準](http://www.php-fig.org/psr/psr-2/ )爲了您自己的利益。您可能會被要求在幾周/幾個月內修改此代碼 ,最後您會感謝我。 – RiggsFolly