开发者

PHP form processing with variableform inputs

开发者 https://www.devze.com 2023-03-02 21:51 出处:网络
Got a form that has the optio开发者_如何学Cn to add many inputs for ordering pictures via picture number.

Got a form that has the optio开发者_如何学Cn to add many inputs for ordering pictures via picture number.

In theory a customer could order 1 picture or 100, how would I go about the PHP.

As coding up to 100 $_POST[] for each possible field seems crazy as each of the added fields as it's own unique NAME attr using jQuery.

Anyone got any bright ideas?


Using field names that end in square brackets will cause PHP to create the entries as an array:

<input name="foo[]" value="foo" />
<input name="foo[]" value="bar" />
<input name="foo[]" value="moo" />
<input name="foo[]" value="cow" />

will produce the following: $_REQUEST['foo'] (or $_POST['foo']/$_GET['foo']) is an array like this:

array(
    0 => 'foo',
    1 => 'bar',
    2 => 'moo',
    3 => 'cow'
);


You could try something like

for ($i=0;$i<100;$i++){
    if (isset($_POST['picture'.$i])){
        // Do something
    } else {
        break;
    }
}


You can do something like this

<input type="checkbox" value="picnumber" name="pictures[]" />

<?php
$pics = $_POST['pictures']; // here you will get an array of values of the selected images
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消