Jump to content

Recommended Posts

Hi All,

I have this problem, I'm not even sure if this can be done.  I have this form where I have a drop down list for a quantity of serial numbers.  When a user selects a numbers from that list I have some JavaScript to dynamically create the number of Text Boxes selected.  In the JavaScript, the name of the text box is serial_num and a number is added at the end of the name ex. serial_num0, serial_num1, etc...  Anyways, what I want to do is to take the serial numbers from the form and create a separate record for each in a MySQL database.

The problem I am having is that since the serial_num fields have numbers at the end of the field names I can't seem to come up with some kind of loop to separate those different serial numbers.  Is there a way I can combine all of those serial number fields into an array to try to get the results that I want?  If that can be done, I'm lost on how to do that coming from a form submission.
Link to comment
https://forums.phpfreaks.com/topic/24849-combining-variables-into-an-array/
Share on other sites

Sure, rather than using javascript to call the fields serial_num0 serial_num1 etc, get it to call all the fields the same thing.  Get it to call them all [color=green]serial_num[][/color].

Then php can access them by using the following:

[code]<?php
foreach ($_REQUEST['serial_num'] as $serial){
  echo "$serial<br>\n";
}
?>[/code]

Regards
Huggie
Create a text-field as "fields_count" and when fields are added/removed using javascript, this field value should be updated accordingly. Then you can run a loop such as this:

[code=php:0]
$fields_count = intval($_POST['fields_count']);
for ($i = 0; $i <= count($fields_count); $i++)
{
      if (empty($_POST['serial_num'.$i])) {
            continue;
      }

      // or here, do anything you like with the data now... it's in $_POST['serial_num' . $i] ....
}
[/code]
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.