【问题标题】:Parsing complex forms using AJAX and PHP使用 AJAX 和 PHP 解析复杂的表单
【发布时间】:2018-03-26 17:10:27
【问题描述】:

我正在创建一个销售到多个国家/地区的电子商务网站。

由于运费等原因,我们必须在不同国家为同一产品收取不同的价格,

我们对不同的产品也有不同的属性(如尺寸、颜色等)。

我正在使用PHP 来生成这样的产品定价表单。

我的PHP如下:

    $attribute = $_GET['attribute'];
    $params = [$attribute];
    $sql = "SELECT * FROM attributes WHERE id=?";
    $stmt = DB::run($sql,$params);
    $attributeCount = $stmt->rowCount();
    if ($attributeCount > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $attribute_name = $row["name"];
        }
    }
    $price_form .= "<table class='table'>";
    $price_form .= "<thead>";
    $price_form .= "<td>$attribute_name</td>";
    $sql = "SELECT * FROM countries";
    $stmt = DB::run($sql);
    $countryCount = $stmt->rowCount();
    if ($countryCount > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $country_name = $row["country"];
            $price_form .= "<td>$country_name</td>";
        }
    }
    $price_form .= "</thead>";
    $params = [$attribute];
    $sql = "SELECT * FROM attributes WHERE id=?";
    $attributeResult = DB::run($sql,$params);
    foreach ($attributeResult as $value) {
        for ($i = 1; $i <= 15; $i++) {
            $attributeColumn = "attribute".$i;
            $checkbox = "checkbox".$i;
            $priceValue = "priceValue".$i;
            $priceCurrency = "priceCurrency".$i;
            $priceAttribute = "priceAttribute".$i;
            if($value[$attributeColumn] != ""){
                $price_form .= "<tr><td><input type='checkbox' name='$checkbox' id='$checkbox' value='$checkbox'";
                if($attribute == 1){ $price_form .= " checked";}
                $price_form .= ">$value[$attributeColumn]</td>";
                $sql = "SELECT * FROM countries";
                $countryResult = DB::run($sql);
                while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)){
                    $country_id = $row["id"];
                    $country_currency = $row["currency"];
                    $price_form .= '<td>';
                    $price_form .= '<input type="text" placeholder='.$country_currency.' name='.$priceValue.'_'.$country_id.' size="10">';
                    $price_form .= '<input type="hidden" value='.$country_id.' name='.$priceCurrency.'_'.$country_id.'>';
                    $price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$country_id.'>';
                    $price_form .= '</td>';
                }
            }
            $price_form .= '</tr>';
        }
    }
    $price_form .= "</table>";
    $price_form .= "<br />";
    $price_form .= "<button id='add_prices' name='add_prices' onClick='return false'>Continue</button>";

这取决于产品的属性,在这种情况下,颜色是这样的:

&lt;table class="table"&gt;&lt;thead&gt;&lt;tr&gt;&lt;td&gt;Colour&lt;/td&gt;&lt;td&gt;Ireland&lt;/td&gt;&lt;td&gt;United Kingdom&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox1" id="checkbox1" value="checkbox1"&gt;Black&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue1_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency1_1"&gt;&lt;input type="hidden" value="1" name="priceAttribute1_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue1_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency1_2"&gt;&lt;input type="hidden" value="1" name="priceAttribute1_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox2" id="checkbox2" value="checkbox2"&gt;White&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue2_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency2_1"&gt;&lt;input type="hidden" value="2" name="priceAttribute2_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue2_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency2_2"&gt;&lt;input type="hidden" value="2" name="priceAttribute2_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox3" id="checkbox3" value="checkbox3"&gt;Purple&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue3_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency3_1"&gt;&lt;input type="hidden" value="3" name="priceAttribute3_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue3_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency3_2"&gt;&lt;input type="hidden" value="3" name="priceAttribute3_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox4" id="checkbox4" value="checkbox4"&gt;Yellow&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue4_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency4_1"&gt;&lt;input type="hidden" value="4" name="priceAttribute4_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue4_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency4_2"&gt;&lt;input type="hidden" value="4" name="priceAttribute4_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox5" id="checkbox5" value="checkbox5"&gt;Red&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue5_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency5_1"&gt;&lt;input type="hidden" value="5" name="priceAttribute5_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue5_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency5_2"&gt;&lt;input type="hidden" value="5" name="priceAttribute5_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox6" id="checkbox6" value="checkbox6"&gt;Orange&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue6_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency6_1"&gt;&lt;input type="hidden" value="6" name="priceAttribute6_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue6_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency6_2"&gt;&lt;input type="hidden" value="6" name="priceAttribute6_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox7" id="checkbox7" value="checkbox7"&gt;Blue&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue7_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency7_1"&gt;&lt;input type="hidden" value="7" name="priceAttribute7_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue7_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency7_2"&gt;&lt;input type="hidden" value="7" name="priceAttribute7_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox8" id="checkbox8" value="checkbox8"&gt;Green&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue8_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency8_1"&gt;&lt;input type="hidden" value="8" name="priceAttribute8_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue8_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency8_2"&gt;&lt;input type="hidden" value="8" name="priceAttribute8_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox9" id="checkbox9" value="checkbox9"&gt;Pink&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue9_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency9_1"&gt;&lt;input type="hidden" value="9" name="priceAttribute9_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue9_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency9_2"&gt;&lt;input type="hidden" value="9" name="priceAttribute9_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox10" id="checkbox10" value="checkbox10"&gt;Grey&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue10_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency10_1"&gt;&lt;input type="hidden" value="10" name="priceAttribute10_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue10_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency10_2"&gt;&lt;input type="hidden" value="10" name="priceAttribute10_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox11" id="checkbox11" value="checkbox11"&gt;Brown&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue11_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency11_1"&gt;&lt;input type="hidden" value="11" name="priceAttribute11_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue11_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency11_2"&gt;&lt;input type="hidden" value="11" name="priceAttribute11_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox12" id="checkbox12" value="checkbox12"&gt;Spearmint&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue12_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency12_1"&gt;&lt;input type="hidden" value="12" name="priceAttribute12_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue12_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency12_2"&gt;&lt;input type="hidden" value="12" name="priceAttribute12_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;input type="checkbox" name="checkbox13" id="checkbox13" value="checkbox13"&gt;Lime Green&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="€" name="priceValue13_1" size="10"&gt;&lt;input type="hidden" value="1" name="priceCurrency13_1"&gt;&lt;input type="hidden" value="13" name="priceAttribute13_1"&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" placeholder="£" name="priceValue13_2" size="10"&gt;&lt;input type="hidden" value="2" name="priceCurrency13_2"&gt;&lt;input type="hidden" value="13" name="priceAttribute13_2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
我们有大量颜色尺寸等属性集,但并非所有这些属性集都可能适用于复选框用途的某些产品。

我一直在通过 PHP 对其进行解析,但整个添加产品的部分变得非常复杂,而不仅仅是定价。 (正在提交不同的部分,将其保存在 $_SESSION 中,以便在提交其他部分时存储它,然后在每个部分完成时提交所有 $_SESSION 变量。

但是我已经切换到AJAX,所以页面不会一直刷新,我不需要继续存储$_SESSION 变量。

我的PHP如下:

for ($i = 0; $i <= 15; $i++) {
    $checkbox = "checkbox".$i;
    $priceValue = "priceValue".$i;
    $priceCurrency = "priceCurrency".$i;
    $priceAttribute = "priceAttribute".$i;
    for ($j = 1; $j <= $countryCount; $j++) {
        $priceValue1 = $priceValue.'_'.$j;
        $priceCurrency1 = $priceCurrency.'_'.$j;
        $priceAttribute1 = $priceAttribute.'_'.$j;
        if(isset($_SESSION[''.$priceValue1.'']) && isset($_SESSION[''.$priceCurrency1.'']) && isset($_SESSION[''.$priceAttribute1.''])){
            $value = $_SESSION[''.$priceValue1.''];
            $currency = $_SESSION[''.$priceCurrency1.''];
            $attribute = $_SESSION[''.$priceAttribute1.''];
            $params = [$id,$currency,$attribute,$value];
            $sql = "INSERT INTO prices (product, country, attribute, value) VALUES(?,?,?,?)";
            $stmt = DB::run($sql,$params);
        }
    }
}

但是,当我想用​​ AJAX 解析这个时,我不确定如何在不知道来自 PHP$countryCount 变量的情况下循环所有这些以确定存储了多少个国家(我们希望这个保持活力,以便我们可以扩展到更多国家/地区)。

我尝试过在循环中弄乱循环(一个循环用于属性,一个循环用于国家),但我无处可去,必须有一种更好的方法来做到这一点,而不必硬编码以获得价值每个国家/地区的每种价格。

如果有人可以就如何解决这个问题给我任何建议,将不胜感激。

//--------------------------------编辑--------\

到目前为止,我一直在尝试将它们全部推送到这样的多维数组中:

var pricing = [];
for (i = 0; i <15; i++) {
    var checkbox = "checkbox"+i;
    var priceValue = "priceValue"+i;
    var priceCurrency = "priceCurrency"+i;
    var priceAttribute = "priceAttribute"+i;
    if ($("#"+checkbox).is(':checked')) {
        for (j = 1; j <=2; j++) {
            var priceValue1 = priceValue+'_'+j;
            var priceCurrency1 = priceCurrency+'_'+j;
            var priceAttribute1 = priceAttribute+'_'+j;
            var value = $("#"+priceValue1).val();
            var currency = $("#"+priceCurrency1).val();
            var attribute = $("#"+priceAttribute1).val();
            var item = [];
            item.push(value);
            item.push(currency);
            item.push(attribute);
            pricing.push(i);
        }
    }
}

但是,这是错误的并且给了我错误。

它也不够动态,在第二个 for() 循环中,我暂时只输入了“2”代表国家的数量。

【问题讨论】:

  • 注意,上面sn-p中每个有输入的表格单元格也有两个隐藏字段。所以每个单元格有 3 个输入。一个用于引用属性 ID,一个用于国家/地区 ID,以及您看到的价格的实际输入。

标签: php jquery ajax


【解决方案1】:

是否有必要将其作为表单数据或会话发送?我认为最好以合理的方式创建一个包含您想要的内容的 JSON 对象。然后stringify并发送它然后在php端解析它并获取对象。这样,您就不受表单数据要求的限制。

其次,更重要的是,无论你做什么,都不要使用直接出现的值。根据更难伪造的中间传输数据重新生成和重新计算数据是更明智的做法。您应该始终能够检查数据是否有效,否则您迟早会遇到麻烦。

附:您可以像这样从 json 迭代数组:

$arr = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]');

foreach($arr as $item) { //foreach element in $arr
    $uses = $item['var1']; //etc
}

【讨论】:

  • 我认为 $_SESSION 不再需要,它原本就在那里,因此当一个部分提交并刷新页面时,我可以保留所有这些变量,直到所有部分都已提交。我现在想通过 AJAX 发送它们,因此不需要刷新页面。我不介意它是 $_SESSION、表单数据还是 json 对象。我认为我正在努力解决的主要部分是以逻辑方式循环输入,以将所有输入放入变量或 JSON 对象中。
  • 检查附注!
  • 再次感谢您的回答。我不确定我是否在最好地解释自己。我在 PHP 中解析 JSON 对象没有问题,它循环表单输入以创建 JSON 对象,以我遇到的问题开始。我只是对所有隐藏的输入也有困难,并循环通过这些输入以合理的逻辑方式将变量放入数组/json对象中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
  • 2023-03-30
  • 2020-12-08
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多