Retrieve post array values

后端 未结 6 1042
野的像风
野的像风 2021-01-13 01:23

I have a form that sends all the data with jQuery .serialize() In the form are four arrays qty[], etc it send the form data to a sendMail page and

相关标签:
6条回答
  • 2021-01-13 01:36

    You have [] within your $_POST variable - these aren't required. You should use:

    $qty = $_POST['qty'];

    Your code would then be:

    $qty = $_POST['qty'];
    
    foreach($qty as $value) {
    
       $qtyOut = $value . "<br>";
    
    }
    
    0 讨论(0)
  • 2021-01-13 01:36

    PHP handles nested arrays nicely

    try:

    foreach($_POST['qty'] as $qty){
       echo $qty
    }
    
    0 讨论(0)
  • 2021-01-13 01:39

    My version of PHP 4.4.4 throws an error: Fatal error: Call to undefined function: size()

    I changed size to count and then the routine ran correctly.

    <?php
    $qty = $_POST['qty'];
    
    if (is_array($qty)) {
       for ($i=0;$i<count($qty);$i++)
       {
           print ($qty[$i]);
       }
    }
    ?>
    
    0 讨论(0)
  • 2021-01-13 01:41

    php automatically detects $_POST and $_GET-arrays so you can juse:

    <form method="post">
        <input value="user1"  name="qty[]" type="checkbox">
        <input value="user2"  name="qty[]" type="checkbox">
        <input type="submit">
    </form>
    
    <?php
    $qty = $_POST['qty'];
    

    and $qty will by a php-Array. Now you can access it by:

    if (is_array($qty))
    {
      for ($i=0;$i<size($qty);$i++)
      {
        print ($qty[$i]);
      }
    }
    ?>
    

    if you are not sure about the format of the received data structure you can use:

    print_r($_POST['qty']);
    

    or even

    print_r($_POST);
    

    to see how it is stored.

    0 讨论(0)
  • 2021-01-13 01:51

    try using filter_input() with filters FILTER_SANITIZE_SPECIAL_CHARS and FILTER_REQUIRE_ARRAY as shown

    $qty=filter_input(INPUT_POST, 'qty',FILTER_SANITIZE_SPECIAL_CHARS,FILTER_REQUIRE_ARRAY);
    

    then you can iterate through it nicely as

    foreach($qty as $key=>$value){
        echo $value . "<br>";
       }
    
    0 讨论(0)
  • 2021-01-13 02:03

    I prefer foreach insted of for, because you do not need to heandle the size.

    if( isset( $_POST['qty'] ) )
    {
        $qty = $_POST ['qty'] ;
        if( is_array( $qty ) )
        {
            foreach ( $qty as $key => $value ) 
            {
                print( $value );
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题