显然不是我的所有代码都在这里,但我相信你会得到这个要点。
<?php
$product_name_1 = $_POST['product_name_1'];
$region_1 = $_POST['region_1'];
$start_date_1 = $_POST['start_date_1'];
$end_date_1 = $_POST['end_date_1'];
$sku_1 = $_POST['sku_1'];
$product_name_2 = $_POST['product_name_2'];
$region_2 = $_POST['region_2'];
$start_date_2 = $_POST['start_date_2'];
$end_date_2 = $_POST['end_date_2'];
$sku_2 = $_POST['sku_2'];
$product_name_3 = $_POST['product_name_3'];
$region_3 = $_POST['region_3'];
$start_date_3 = $_POST['start_date_3'];
$end_date_3 = $_POST['end_date_3'];
$sku_3 = $_POST['sku_3'];
?>
<form action="" method="post" accept-charset="utf-8">
<div id="product_information">
<table id="product_1">
<tr>
<th><label for="product_name">Product Name</label></th>
<th><label for="region">Select A Region</label></th>
<th class="date"><label for="start_date">Start Date</label></th>
<th class="date"><label for="end_date">End Date</label></th>
<th><label for="sku">SKU</label></th>
</tr>
<tr>
<td><input type="text" name="product_name_1" value="" id="product_name_1"></td>
<td><input type="radio" name="region_1" value="upper_north" id="upper_north_1"><label for="upper_north_">Upper North Island</label><br />
<input type="radio" name="region_1" value="lower_north" id="lower_north_1"><label for="lower_north_">Lower North Island</label><br />
<input type="radio" name="region_1" value="south_island" id="south_island_1"><label for="south_island">South Island</label> </td>
<td class="date"><input type="text" class="date" name="start_date_1" value="" id="start_date_1"></td>
<td class="date"><input type="text" class="date" name="end_date_1" value="" id="end_date_1"></td>
<td><input type="text" name="sku_1" value="" id="sku_1"></td>
</tr>
</table>
<span class="product"></span>
<div class="add-product"> </div>
</div>
<script type="text/javascript" charset="utf-8">
var i = 1;
$('.add-product').click(function(){
i++;
$('span.product').replaceWith('<table id="product_'+i+'">'
+'<tr>'
+'<th><label for="product_name">Product Name</label></th>'
+'<th><label for="region">Select A Region</label></th>'
+'<th class="date"><label for="start_date">Start Date</label></th>'
+'<th class="date"><label for="end_date">End Date</label></th>'
+'<th><label for="sku">SKU</label></th>'
+'</tr>'
+'<tr>'
+'<td><input type="text" name="product_name'+i+'" value="" id="product_name'+i+'"></td>'
+'<td><input type="radio" name="region'+i+'" value="upper_north" id="upper_north'+i+'"><label for="upper_north'+i+'">Upper North Island</label><br />'
+'<input type="radio" name="region'+i+'" value="lower_north" id="lower_north'+i+'"><label for="lower_north'+i+'">Lower North Island</label><br />'
+'<input type="radio" name="region'+i+'" value="south_island" id="south_island"><label for="south_island">South Island</label> </td>'
+'<td class="date"><input type="text" class="date" name="start_date'+i+'" value="" id="start_date'+i+'"></td>'
+'<td class="date"><input type="text" class="date" name="end_date'+i+'" value="" id="end_date'+i+'"></td>'
+'<td><input type="text" name="sku'+i+'" value="" id="sku'+i+'"></td>'
+'</tr>'
+'</table>'
+''
+'<span class="product"></span>');
});
</script>
答案 0 :(得分:5)
您可以使用此对象启动。之后定义保存和检索它的方法。也许是适当的构造函数,用于从post params创建不同的产品。
class Product {
private $Name;
private $Region;
private $StartDate;
private $EndDate;
private $Sku;
public function setName( $value )
{
//make some validation or manipulation on data here, if needed
$this->Name = $value;
}
public function getName()
{
return $this->Name;
}
public function setRegion( $value )
{
//make some validation or manipulation on data here, if needed
$this->Region = $value;
}
public function getRegion()
{
return $this->Region;
}
public function setStartDate( $value )
{
//make some validation or manipulation on data here, if needed
$this->StartDate = $value;
}
public function getStartDate()
{
return $this->StartDate;
}
public function setEndDate( $value )
{
//make some validation or manipulation on data here, if needed
$this->EndDate = $value;
}
public function getEndDate()
{
return $this->EndDate;
}
public function setSku( $value )
{
//make some validation or manipulation on data here, if needed
$this->Sku= $value;
}
public function getSku()
{
return $this->Sku;
}
}
希望有所帮助!
答案 1 :(得分:1)
您不一定需要使用对象 - 在处理表单时,关联数组更容易(因为这是HTML传递给PHP的内容)。在您的表单中,使用这样的字段名称会更有用:
product[1][name]
product[1][region]
product[1][start_date]
product[1][end_date]
product[1][sku]
product[2][name]
...等在PHP中接收数据时,它将在$_POST['product']
中,它本身就是每个产品的数组。您的新PHP代码将取决于您要对数据执行的操作,但您可能会像这样循环:
<?php
foreach( $_POST['product'] as $prod )
{
echo $prod['name']; // outputs each product name in turn
}
如果您需要某个对象,则可以使用$prod_obj = (object) $prod
,然后使用$prod_obj->name
,依此类推。
答案 2 :(得分:0)
现在进行实例化和使用新对象。
我可以制作这样的构造吗?
<?
class Product {
private $Name;
private $Region;
private $StartDate;
private $EndDate;
private $Sku;
public $i;
function __construct($Name="product_name_$i" $Region="region_$i" $StartDate="start_date_$i" $EndDate="end_date_$i" $Sku="sku_$i")
{
$i++
}
}
?>
然后我将如何实例化对象并在我的代码中使用它?..我可以用我的JavaScript做到这一点吗?
答案 3 :(得分:0)
/**
* Constructor for the product.
*
* @param string=>string[] An associative array used to create the object.
*/
public function __construct($aData = null) {
if (!is_null($aData)) {
$this->setName ($aData["name"]);
$this->setRegion ($aData["region"]);
$this->setStartDate($aData["start_date"]);
$this->setEndDate ($aData["end_date"]);
$this->setSku ($aData["sku"]);
}
}
在HTML代码中,您可以定义字段,如...
<input type="text" name="product[1][name]" />
<input type="text" name="product[1][sku]" />
...
然后,在PHP中,你会做类似
的事情$aProducts = array();
foreach ($_POST["product"] as $aProduct) {
$aProducts[] = new Product($aProduct);
// NOTE - here you could also make an empty object and fill it.
$oProduct = new Product();
$oProduct->setName($aProduct["name"]);
// etc. - then add it to the array when you're done.
}
// Display the name of product 2 (if it exists).
if (2 <= count($aProducts)) {
echo $aProducts[1]->getName();
}
最后,我不确定“$ i”给你的是什么。我通常只创建像......这样的字段。
<input type="text" name="name[]" />
<input type="text" name="sku[]" />
<!-- This next one is if I need a database ID once it's posted back. -->
<input type="hidden" name="id[]" value="something"/>
...
<input type="text" name="name[]" />
<input type="text" name="sku[]" />
...etc...
然后,在服务器上,我有一堆数组,但它们的键都匹配。所以,我可以做点像......
$aProducts = array();
foreach($_POST["id"] as $iKey => $aValue) {
$oProduct = new Product();
$oProduct->setName($_POST["name"][$iKey]);
$oProduct->setSku ($_POST["sku" ][$iKey]);
$aProducts[] = $oProduct;
}
其中6个,1个,1/2个。我的构造函数通常使用PDORow对象作为参数,所以我在页面帖子上这样做,当我使用关联数组时。