Jump to content

Identifying dynamic form fields


tpclarke2

Recommended Posts

Here's the situation:

 

User goes to page which is a form that asks for some basic info regarding returning products for credit/exchange, and also asks for how many products are going to be returned. Upon submitting, a return authorization form is presented to them with the specified number of product fields. I now want to take the input from this form and email it to myself, as well as, display a confirmation page to the user. The problem I have is a can't say, for instance, echo "$field_name"; because field_name will get created dynamically after the user specifies how many products to return. For example, say the user says 5 products, this will generate a form with field names like:

 

return_code0 inv_num0 qty0 product0 cre_exch0 stockout0

return_code1 inv_num1 qty1 product1 cre_exch1 stockout1

return_code2 inv_num2 qty2 product2 cre_exch2 stockout2 and so on....

 

These get created with a for() loop. Once this form is submitted, how can I address the field names?

 

Here's my code so far, any help would be greatly appreciated!

 

if (!$num_prods){

<form action='$PHP_SELF' method='POST'>

        <table border='1' cellspacing='0' cellpadding='2'>

<tr><td align='right' nowrap>Your Name: </td>

        <td><input type='text' name='rep' size='30' maxlength='30'></td>

<td align='right' nowrap>Salon Name: </td>

        <td><input type='text' name='sname' size='30' maxlength='30'></td></tr>

<tr><td align='right' nowrap>Contact: </td>

        <td><input type='text' name='contact' size='30' maxlength='30'></td>

<td align='right' nowrap>Order Date (mm-dd-yy): </td>

        <td><input type='text' name='date' size='10' maxlength='8'></td></tr>

<tr><td align='right' nowrap>Order #: </td>

        <td><input type='text' name='ord_num' size='10' maxlength='10'></td>

<td align='right' nowrap>Account #: </td>

        <td><input type='text' name='acct_num' size='10' maxlength='6'></td></tr>

<tr><td align='right' nowrap>Number of Products: </td>

        <td><input type='text' name='num_prods' size='3' maxlength='2'></td>

<td colspan='2'><input type='submit' name='submit' value='submit'></td></tr>

</table></div>";

exit;

}

 

if ($num_prods >= '1' && !$complete){

<form action='$PHP_SELF' method='POST'>

<input type='hidden' name='complete' value='true'>

<input type='hidden' name='num_prods' value='$num_prods'>

<input type='hidden' name='rep' value='$rep'>

<input type='hidden' name='sname' value='$sname'>

<input type='hidden' name='contact' value='$contact'>

<input type='hidden' name='date' value='$date'>

<input type='hidden' name='ord_num' value='$ord_num'>

<input type='hidden' name='acct_num' value='$acct_num'>

 

<table border='1' cellspacing='0' cellpadding='2' width='100%'>

<tr>

<td nowrap align='center'>Return Code</td><td nowrap align='center'>Invoice #</td>

<td nowrap align='center'>Qty</td><td nowrap align='center'>Product Description (include size)</td>

<td nowrap align='center'>Credit/Exchange</td><td nowrap align='center'>Stockout?</td></tr>";

for ($i = 0; $i < $num_prods; $i++){

echo "<tr>

<td align='center'><select name='return_code$i'>

<option value=''>Select...</option>

<option value='1'>Shipping Error</option>

<option value='2'>DSC Error</option>

<option value='3'>CSR Error</option>

<option value='4'>Customer Return</option>

<option value='5'>Overstock</option>

<option value='6'>Damaged</option></select></td>

<td align='center'><input type='text' name='inv_num$i' size='10' maxlength='10'></td>

<td align='center'><input type='text' name='qty$i' size='3' maxlength='3'></td>

<td align='center'><input type='text' name='product$i' size='50' maxlength='30'></td>

<td align='center'><select name='cred_exch$i'>

<option value=''>Select...</option>

<option value='C'>Credit</option>

<option value='E'>Exchange</option></select></td>

<td align='center'><select name='stockout$i'>

<option value=''>Select...</option>

<option value='Y'>Yes</option>

<option value='N'>No</option></select></td></tr>";

}

echo "</table><br/><input type='submit' name='submit' value='submit'></form>";

do_html_footer();

exit;

}

if ($num_prods && $complete){

$recipient = "somebody@something.com";

$subject = "RA Form for order add-on";

$mailheaders = "From: $rep\n";

$msg = "--------------------------------------------------------------------\n";

$msg .= "$rep needs to add credits/exchanges to an existing order. Here's the info:\n";

$msg .= "-------------------------------------------------------------------\n";

$msg .= "Order #: $ord_num\n";

$msg .= "Account: $acct_num\n";

$msg .= "Salon:  $sname\n";

$msg .= "Contact: $contact\n";

$msg .= "Date:    $date\n";

$msg .= "-------------------------------------------------------------------\n";

for ($i = 0; $i < $num_prods; $i++){

 

HERE'S WHERE THE CODE WOULD GO THAT LOOPS THROUGH THE SUBMITTED FORM AND EMAILS ME THE VALUES

 

        }

$mailheaders .= "Reply-To: email@email.com\n\n";

mail($recipient, $subject, $msg, $mailheaders);

 

echo "<div align = 'center'>Your RA Form has been submitted. Here's your copy:<p>

<table border='1' cellspacing='0' cellpadding='2'>

<tr><td align='center' nowrap>Reason</td>

        <td align='center' nowrap>Invoice #</td>

        <td align='center' nowrap>Qty</td>

<td align='center' nowrap>Product</td>

        <td align='center' nowrap>Cred/Exch</td>

        <td align='center' nowrap>Stockout?</td></tr>";

for ($i = 0; $i < $num_prods; $i++){

 

HERE IS WHERE THE CODE WOULD GO TO LOOP THE SUBMITTED FORM AND DISPLAY A CONF PAGE FOR THE USER

 

}

echo "</table></p></div>";

do_html_footer();

exit;

}

?>

Link to comment
Share on other sites

make a hidden field that tells you how many fields that there were, and then loop through them using the $_POST['return_code' + $i] sort of thing.  its not that hard, if i understand your code directly.

 

<?php
for ($i = 0; $i <= $_POST['num_of_fields']; $i++) {
  msg .= "return code:  " . $_POST['return_code' . $i] . "\r\n";
  //and the rest...
}
?>

Link to comment
Share on other sites

Pass the fields as arrays by appending [] to the field names

 

<?php
/**
* PROCESS THE FORM DATA
*/
if (isset($_POST['sub']))
{
    foreach ($_POST['return_code'] as $k=>$return_code)
    {
        $inv_num = $_POST['inv_num'][$k];         // get other fields for same input set
        $qty = $_POST['qty'][$k];
        $product = $_POST['product'][$k];
        $cre_exch = $_POST['cre_exch'][$k];
        $stockout = $_POST['stockout'][$k];
        
        // process this input record
        echo "$return_code,  $inv_num, $qty, $product, $cre_exch, $stockout <br />"; 
    }
}
/**
* THE FORM
*/
$howMany = 5;   // requested by user

echo '<form method="post">';

echo '<table>';
echo '<tr>
    <th>Return code</th>
    <th>Inv num</th>
    <th>Qty</th>
    <th>Product</th>
    <th>CRE exch</th>
    <th>Stockout</th>
    </tr>';
        
for ($i=0; $i<$howMany; $i++)
{
    echo '<tr>
        <th><input type="text" name="return_code[]" size="5"></th> 
        <th><input type="text" name="inv_num[]" size="5"></th>
        <th><input type="text" name="qty[]" size="5"></th>
        <th><input type="text" name="product[]" size="5"></th>
        <th><input type="text" name="cre_exch[]" size="5"></th>
        <th><input type="text" name="stockout[]" size="5"></th>
        </tr>';
}
echo '</table>';
echo '<input type="submit" name="sub" value="Submit">';
echo '</form>'
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.