duanshan1856 2015-12-08 10:20
浏览 60
已采纳

PHP $ _POST和extract()除了可能是一个casted int之外什么也没显示

I have a simple form using post

<form id="quote_form" name="n_quote_form" method="post" action="quote">

(the action here is an url parameter (sort of) which a page-loading script uses to direct the user back to this page in_quote.php)

switch ($urlParam) {
        case 'quote':
            $incPage = "in_quote.php";
            break;
        case 'contact':
            $incPage = "in_contact.php";
            break;
        case 'links':
            $incPage = "in_links.php";
            break;
        default:
            $incPage = "in_home.php";
            break;
    }

so when I fill in the form with this data

enter image description here

this data is passed to this script:

function submit_quote_form(){

    if (validate_form($_POST)){

which in turn calls on this script: (I have not implemented validation until I can extract the form data from $_POST using extract() as I don't want to manually type in the keys every time the form changes or if I want to reuse my validate script (with less modification) elsewhere)

this script dumps the $form_data var correctly ($_POST in essence so I think we can assume that it's not a passing parameters issue) but then the extract variable $var seems to be converted to an int of size 11???

function validate_form($form_data) {
    var_dump($form_data);
    $var = extract( $form_data, EXTR_OVERWRITE, "form_" );
    var_dump($var);
    echo "THIS IS MY POST VAR ==>";
    var_dump($_POST); // included for comparison
}

the results are strange when I var_dump $form_data (compared $form_data to $_POST to make sure it's not a parameter passing issue)

output:

array(11) { ["n_name"]=> string(3) "bob" ["n_email"]=> string(12) "bob@bobs.com" ["n_email2"]=> string(12) "bob@bobs.com" ["n_day_from"]=> string(1) "8" ["n_month_from"]=> string(8) "December" ["n_year_from"]=> string(4) "2015" ["n_day_to"]=> string(1) "9" ["n_month_to"]=> string(8) "December" ["n_year_to"]=> string(4) "2015" ["n_bike_reqs"]=> string(14) "5 bikes please" ["submit_but"]=> string(13) "get my quotes" } int(11) THIS IS MY POST VAR ==>array(11) { ["n_name"]=> string(3) "bob" ["n_email"]=> string(12) "bob@bobs.com" ["n_email2"]=> string(12) "bob@bobs.com" ["n_day_from"]=> string(1) "8" ["n_month_from"]=> string(8) "December" ["n_year_from"]=> string(4) "2015" ["n_day_to"]=> string(1) "9" ["n_month_to"]=> string(8) "December" ["n_year_to"]=> string(4) "2015" ["n_bike_reqs"]=> string(14) "5 bikes please" ["submit_but"]=> string(13) "get my quotes" }

I did try to cast the extraction to an array using:

$var = (array) extract( $_POST, EXTR_OVERWRITE, "form_" );

but that gave me this:

array(1) { [0]=> int(11) }

in var_dump($var);

so only casted the int into an array of ints of length 1! I COULD use the individual keys as other posts here have suggested but I'd rather not particularly when other posts here had accepted answers using the extract method - it's just NOT WORKING for me!

Q. Why is extract() not working as I/WE think it should???

Help is appreciated!

  • 写回答

1条回答 默认 最新

  • dongnuan7956 2015-12-08 10:34
    关注

    extract would add array keys and values to the current symbol table, so after using extract on an array you can use array keys as variable name. it will return array values.

    $size = "large";
    $var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
    $var=extract($var_array, EXTR_PREFIX_SAME, "wddx");
    
    echo "$color, $size, $shape, $wddx_size
    ";
    echo $var;
    

    the result I got was:

    blue, large, sphere, medium 3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?