PHP | Get input name through $_POST[]

前端 未结 8 1531
长发绾君心
长发绾君心 2020-12-16 19:10

HTML example:

  
相关标签:
8条回答
  • 2020-12-16 19:43

    Loop your object/array with foreach:

    foreach($_POST as $key => $items) {
        echo $key . "<br />";
    }
    

    Or you can use var_dump or print_r to debug large variables like arrays or objects:

    echo '<pre>' . print_r($_POST, true) . '</pre>';
    

    Or

    echo '<pre>';
    var_dump($_POST);
    echo '</pre>';
    
    0 讨论(0)
  • 2020-12-16 19:43

    Nice neat way to see the form names that are, or will be submitted

    <?php
    $i=1;
    foreach ($_POST as $name => $value) {
    echo "</b><p>".$i." ".$name; 
    echo " = <b>".$value; 
    $i++;
    }
    ?>
    

    Just send your form to this script and it will show you what is being submitted.

    0 讨论(0)
  • 2020-12-16 19:52

    You can process $_POST in foreach loop to get both names and their values, like this:

    foreach ($_POST as $name => $value) {
       echo $name; // email, for example
       echo $value; // the same as echo $_POST['email'], in this case
    }
    

    But you're not able to fetch the name of property from $_POST['email'] value - it's a simple string, and it does not store its "origin".

    0 讨论(0)
  • 2020-12-16 19:56

    If you wanted to do it dynamically though, you could do it like this:

    foreach ($_POST as $key => $value)
    {
        echo 'Name: ', $key, "\nValue: ", $value, "\n"; 
    }
    
    0 讨论(0)
  • 2020-12-16 19:59

    Actually I found something that might work for you, have a look at this-

    http://php.net/manual/en/function.key.php

    This page says that the code below,

    <?php
    $array = array(
        'fruit1' => 'apple',
        'fruit2' => 'orange',
        'fruit3' => 'grape',
        'fruit4' => 'apple',
        'fruit5' => 'apple');
    
    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
        if ($fruit_name == 'apple') {
            echo key($array).'<br />';
        }
        next($array);
    }
    ?>
    

    would give you the output,

    fruit1
    fruit4
    fruit5

    As simple as that.

    0 讨论(0)
  • 2020-12-16 20:01

    current(array_keys($_POST)) should give you what you are looking for. Haven't tested though.

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