简体   繁体   中英

How can get values of dynamically added input values in php loop

I have created a form in contact form 7 and using repeater fields addon. I have two fields.

<input type="text" name="serienummer1__1" value=""  class="wpcf7-form-control wpcf7-text form-control serielnumber" data-orig_name="serienummer1">
<input type="text" name="productcode1__1" value=""  class="wpcf7-form-control wpcf7-text form-control productcode" data-orig_name="postcode1">

It is working fine i am able to get the values in my php file like this..

$serNo = $_GET['serienummer1__1'];
$prodCode = $_GET['productcode1__1'];

The issue is when user clone these fields the repeater plugin changed the name values of inputs then it become like this..

<input type="text" name="serienummer1__2" value=""  class="wpcf7-form-control wpcf7-text form-control serielnumber" data-orig_name="serienummer1">
<input type="text" name="productcode1__2" value=""  class="wpcf7-form-control wpcf7-text form-control productcode" data-orig_name="postcode1">

Here comes the problem.. I can get these values by writing harcoded this in php.

$serNo = $_GET['serienummer1__2'];
$prodCode = $_GET['productcode1__2'];

But this is not handy then i also have to write number of times because user can duplicate this section number of times there is no limit.. My question is how can get these values dynamically in php? I tried to loop and get the values with this:

for($i=1; $i<=10; ++$i) {
    if(isset($_GET['serienummer1__'.$i])) {
     $serNo = $_GET['serienummer1__'.$i];
    }
}

But this give 1 instead of value.

I am not sure i explained my problem correctly.

Can anyone help me with this?

Thanks in advance.

I'm not familiar with Contact Form 7 or its' additional plugins, so if there's a way to send Repeater Fields as an array instead (like @pavel mentioned), that would be ideal. If not, you'll need to grab your information and handle it yourself.

There's a dozen ways to do this, the first one I could think of would involve using array_walk() to walk through your $_GET values and assign them to separate arrays.

Since $_GET is insecure and open to user input, you'll want to check the parameters and values before doing anything with them though.

Assuming your form is posted, and you have a URL like:

https://example.com/?serienummer1__1=a-SN11&productcode1__1=a-PC11&serienummer1__2=b-SN12&…

You will end up with a $_GET array that looks like

$_GET = array(
    'serienummer1__1' => 'a-SN11',
    'productcode1__1' => 'a-PC11',
    'serienummer1__2' => 'b-SN12',
    'productcode1__2' => 'b-PC12',
    'serienummer1__3' => 'c-SN13',
    'productcode1__3' => 'c-PC13',
);

Now, Like I mentioned, $_GET is susceptible to user input, and you don't want users adding their own stuff to it, so you'll want to check the format of it. An easy way is to use preg_match() and check the format:

$regex = '/(productcode1|serienummer1)__\d*/';

Now, before you do anything you'll want some variables to store the validated results in:

$serNo = $prodCode = array();

With everything set up, we can proceed to walk through the array, pass those two empty arrays in by reference, and add the values to them if it passes the formatting test.

// Walk through the $_GET array and assign values
array_walk( $_GET, function($value, $key) use ($regex, &$serNo, &$prodCode){
    // Does this key match our format?
    if( preg_match( $regex, $key ) ){
        // Yes, which array does it go in?
        if( strpos($key, 'serienummer1__') !== false ){
            $serNo[] = $value;
        } else if( strpos($key, 'productcode1__') !== false ){
            $prodCode[] = $value;
        }
    }
});

Now that you've validated the keys, and added the values to the appropriate arrays, you can do what you want with those.

#var_dump( $serNo );    // Contains: array(3){ 'a-SN111', 'b-SN12', 'c-SN13' }
#var_dump( $prodCode ); // Contains: array(3){ 'a-PC111', 'b-PC12', 'c-PC13' }

You can access the individual variables by index now:

echo 'Second Product Code is '. $prodCode[1]; // output: "Second Product Code is b-PC12"

This should be scalable enough to handle whatever number of repreater fields you through at it within reason and max-length of the browser's URI .

It's may be a bit overcomplicated, but in my opinion using $_GET warrants the extra security (you may even want to validate/sanitize the $value of each one). At the very least, this should be enough to get you going. Depending on what you're doing, you can loop through the two arrays at the end to output the data or build an email, or whatever you need:

for( $i = 0, $n = count($serNo); $i < $n; $i++ ){
    printf( "Item #%d: [Series: %s] [Product: %s]", $i, $serNo[$i], $prodCode[$i] );
}

You can see the code compiled together as an example here .

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