在PHP循环中生成表单元素 [英] Generating form elements within PHP loop
问题描述
您好,
我已经让Ajax处理简单的表单,但是当我尝试在循环中生成多个表单元素时,我无法解压缩所有这些数据并使用它来更新我的数据库。根据我的代码,任何人都有关于如何实现这一目标的任何意见?基本上,每个订单项都有可能更新其价格。当用户单击提交按钮时,它会调用Ajax函数,该函数应将所有表单数据传递给price-update.php。在那里,它解压每个$ _POST并相应地更新数据库。
注意:此代码是为了视觉上的简易而减少的。使用了一些变量,但没有给出初始化方法。
执行print_r($ _ POST)测试会在URL中产生以下结果:
?submit = Submit + Changes& price%5B%5D%3B +%3F> = 5612& id%5B%5D = 6& p rice%5B%5D%3B +%3F> ; = 2 5和ID%5B%5D = 5&安培;价格%5 B%5D%3B +%3F> = 52和ID%5 B%5D = 3及价格%5B%5D%3 B +%3F> ; =& id%5B%5D = 2& p rice%5B%5D%3B +%3F> =& id%5B%5D = 8
Hello,
I've gotten Ajax to work on simple forms, but when I try to generate multiple form elements in a loop I have trouble unpacking all that data and using it to update my database. Anyone have any input on how I could achieve this based on my code below? Basically, each line item has the potential to have its price updated. When the user clicks the submit button, it calls a function to Ajax, which should pass all the form data to price-update.php. There, it unpacks every $_POST and update the database accordingly.
Note: This code is reduced for visual ease. Some variables are used, but methods for initialization are not given.
Doing a print_r($_POST) test results in the following in the URL:
?submit=Submit+Changes&price%5B%5D%3B+%3F>=5612&id%5B%5D=6&price%5B%5D%3B+%3F>=25&id%5B%5D=5&price%5B%5D%3B+%3F>=52&id%5B%5D=3&price%5B%5D%3B+%3F>=&id%5B%5D=2&price%5B%5D%3B+%3F>=&id%5B%5D=8
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '/price-update.php',
data: $('form').serialize(),
success: function () {
alert('Data was submitted');
}
});
});
});
</script>
//Multiple form elements generated depending on how many prices exist in database
<form>
<input name="submit" type="submit" value="Submit Changes" />
<table>
<tbody>
<?php
$n = 0;
for ($z = 0; $z < count($names); $z++) { ?>
<tr id = "row-<?php echo $z; ?>">
<td><input type="number" name ="price[]; ?>" class="price-update" value="" min="1" max="999999999" /></td>
<td><input type="hidden" name="id[]" class="price-id" value="<?php echo $id_array[$n]; ?>" /></td>
<?php $n++;?>
</tr>
<?php
}?>
</tbody>
</table>
</form>
//price-update.php
<?php
if($_POST) {
//Uses SSL connection with mysqli
require_once('connection.php');
$IDs = array();
$Prices = array();
foreach($_POST as $key => $value) {
if (strstr($key, 'id'))
{
$IDs[] = $value;
}
if (strstr($key, 'price'))
{
$Prices[] = $value;
}
}
for($i = 0; $i < count($IDs); $i++) {
$price_update = "UPDATE prices SET price='".$Prices[$i]."' WHERE id='".$IDs[$i]."'";
$send_update = $instance->query($price_update);
}
}
?>
我的尝试:
许多变化。简单的表单更新数据库就好了 - 但是从PHP循环生成表单元素似乎会导致问题。我认为我的问题是$ _POST元素的流控制。
What I have tried:
Many variations. Simple forms update database just fine - but generating form elements form a PHP loop seems to cause problems. I think my issue is with the flow control for the $_POST elements.
推荐答案
_POST并相应地更新数据库。
注意:此代码因视觉简便而减少。使用了一些变量,但没有给出初始化方法。
执行print_r(
_POST and update the database accordingly.
Note: This code is reduced for visual ease. Some variables are used, but methods for initialization are not given.
Doing a print_r(
_POST)测试结果在URL中跟随:
?submit =提交+更改&价格%5B%5D%3B +%3F> = 5612& id%5B%5D = 6& p大米%5B%5D%3B +%3F> = 2 5和ID%5B%5D = 5&安培;价格%5 B%5D%3B +%3F> = 52和ID%5 B%5D = 3及价格%5B%5D%3 B +%3F> =& id%5B%5D = 2& p rice%5B%5D%3B +%3F>& id%5B%5D = 8
_POST) test results in the following in the URL:
?submit=Submit+Changes&price%5B%5D%3B+%3F>=5612&id%5B%5D=6&price%5B%5D%3B+%3F>=25&id%5B%5D=5&price%5B%5D%3B+%3F>=52&id%5B%5D=3&price%5B%5D%3B+%3F>=&id%5B%5D=2&price%5B%5D%3B+%3F>=&id%5B%5D=8
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
(function(){
(function () {
这篇关于在PHP循环中生成表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!