简体   繁体   中英

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. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. 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. 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.

    $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 :

I figured it out using @dirt 's jquery suggestion.

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:

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

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.

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. 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:

$('#<?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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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