$_POST Array from html form

前端 未结 4 625
悲&欢浪女
悲&欢浪女 2020-11-28 12:46

I am trying to send data from multiple checkboxes (id[]) and create an array \"info\" in php to allow me to run a script for each value (however the quantity of values may c

相关标签:
4条回答
  • 2020-11-28 13:13

    You should get the array like in $_POST['id']. So you should be able to do this:

    foreach ($_POST['id'] as $key => $value) {
        echo $value . "<br />";
    }
    

    Input names should be same:

    <input name='id[]' type='checkbox' value='1'>
    <input name='id[]' type='checkbox' value='2'>
    ...
    
    0 讨论(0)
  • 2020-11-28 13:15
    <input name='id[]' type='checkbox' value='".$shopnumb."\'>
    <input name='id[]' type='checkbox' value='".$shopnumb."\'>
    <input name='id[]' type='checkbox' value='".$shopnumb."\'>
    
    
    $id = implode(',',$_POST['id']);
    echo $id
    

    you cannot echo an array because it will just print out Array. If you wanna print out an array use print_r.

    print_r($_POST['id']);
    
    0 讨论(0)
  • 2020-11-28 13:16

    I don't know if I understand your question, but maybe:

    foreach ($_POST as $id=>$value)
        if (strncmp($id,'id[',3) $info[rtrim(ltrim($id,'id['),']')]=$_POST[$id];
    

    would help

    That is if you really want to have a different name (id[key]) on each checkbox of the html form (not very efficient). If not you can just name them all the same, i.e. 'id' and iterate on the (selected) values of the array, like: foreach ($_POST['id'] as $key=>$value)...

    0 讨论(0)
  • 2020-11-28 13:26

    Change

    $info=$_POST['id[]'];
    

    to

    $info=$_POST['id'];
    

    by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.

    0 讨论(0)
提交回复
热议问题