简体   繁体   English

发布表单值,其中从循环中动态创建字段名称?

[英]Post form values where Field Names are dynamically created from a loop?

I have a page that is created dynamically from the information that a user has submitted on a previous page. 我有一个页面是根据用户在上一页上提交的信息动态创建的。 For example on page 1, the user inputs some department names. 例如在第1页上,用户输入一些部门名称。 They can enter as many as they want. 他们可以输入任意多个。 On page 2, multiple sections are created for each department that was entered on page one. 在第2页上,为在第1页上输入的每个部门创建多个部分。 Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. 在这些部分的每一个内部都将是表单,目前,我已对其进行了设置,以便使用变量$ a创建表单的名称。 I need to know how to post these items once the submit button in each section is clicked. 单击每个部分中的“提交”按钮后,我需要知道如何发布这些项目。 I have tried several different ways and nothing is working. 我尝试了几种不同的方法,但没有任何效果。 I want it so that ONLY the items with the same $a value as the submit button's $a get posted. 我想要这样,以便仅发布与提交按钮的$ a具有相同$ a值的项目。

    $departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id =     '$bid'";
    $departmentResults = mysql_query($departmentSql,$con);

$a = 0;

while ($departmentRow = mysql_fetch_array($departmentResults)) {

$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;

echo "

<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";

$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>

<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area: 
<select name="dep_area<?php echo $a;?>" type="menu">
    <option value=""></option>
    <?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
    <option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><?    php echo $areaRows[       'area_name'] ?></option>
<?php }
?>
</select>
&nbsp;&nbsp;
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />

<?php
echo "</div>"; 
}
?>   

* EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. * 编辑我需要所有内容都采用一种形式,因为页面的重点是将所有稍后将插入各个部分的值相加。 * *

* *EDIT 2 : * *编辑2:

I figured it out using @dirt 's jquery suggestion. 我使用@dirt的jquery建议弄清楚了。

HTML: HTML:

$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);

$a = 0;

while ($departmentRow = mysql_fetch_array($departmentResults)) {

$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;

echo "

<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";

$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>

<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area: 
<select name="dep_area<?php echo $a ?>" type="menu">
    <option value=""></option>
    <?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
    <option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><?    php echo $areaRows[       'area_name'] ?></option>
<?php }
?>
</select>
&nbsp;&nbsp;
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>

<?php
echo "</div></div>"; 
} ?>

jQuery: jQuery的:

<script>
$(document).ready(function() {
    $('#<?php echo $a ?>').submit(function() {
        .post("include/action.php")
    }); 
});
</script>

PHP: PHP:

if(isset($_POST['areaSub'])) {

$areaval = intval($_POST['areaSub']);   



$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]); 

echo $areaval;
echo $area_name;
echo $bdep;

}

EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button. 编辑:如果它是一个具有多个部分的表单,并且您不想在POST之后修剪掉多余的表单数据,那么我认为您必须在发布到服务器之前使用jQuery修剪表单值,因此请参阅我的jQuery部分下面的示例演示了如何只发布所选按钮的数据。

Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. 简短的答案是使用提交按钮名称/值属性,并在发布后对其进行评估。 Multiple buttons with the same name can have different values and the values don't have to be the labels. 具有相同名称的多个按钮可以具有不同的值,并且这些值不必是标签。

Example: 例:

<form id='<?php echo $a; ?>'>
    <input type='text' name='in1'>
    <input type='text' name='in2'>
    <button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
    <input type='text' name='in1'>
    <input type='text' name='in2'>
    <button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
    # _POST:
    if (isset($_POST['submit']) && $_POST['submit'] == '1') {
        echo 'Department 1';
    }
    if (isset($_POST['submit']) && $_POST['submit'] == '2') {
        echo 'Department 2';
    }
?>

You could also use jQuery to get all elements contained in a certain Div ID. 您也可以使用jQuery获取某个Div ID中包含的所有元素。 Something like (this is rough idea): 有点像(这是一个粗略的主意):

<div id='<?php echo $a; ?>'>

    <input type='text' name='in1'>
    <input type='text' name='in2'>

    <input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>

jQuery: jQuery的:

$('#<?php echo $a; ?>').submit(function() {
    do_something_with_form_$a
});

I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? 我不确定我是否能完全理解...,但是您遇到的问题是,当用户点击给定值(给定部门)的“提交”按钮时,它将提交所有数据。这一页? And you only want the input fields for that specific area submitted? 您只希望提交该特定区域的输入字段?

Section off forms with the tag: 用标签截取表格:

 <form> </form>

So, you'll have multiple areas: 因此,您将拥有多个领域:

 while(...) {
    <form method="..." name="form $a">
       <input name="... $a">
       <input name="... $a">
        ...
       <submit>
   </form>
}

(Obviously this is pseudocode, adjust accordingly) (显然这是伪代码,请相应地进行调整)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM