0
ここで私は入力フィールドをforループで作成し、mannualyフィールドを追加して追加することができますが、商品ごとに異なる名前を提供する&価格フィールド。あなたは個々の値を取得します。このitem[]
ような配列として名を渡しますかどうかはどのように追加ボタンでフィールドを作成しているときにフォーム要素を送信したい
<?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