亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試從數組中創建具有輸入字段名稱的變量但不起作用

嘗試從數組中創建具有輸入字段名稱的變量但不起作用

PHP
慕工程0101907 2023-08-19 17:52:03
我正在開發一個項目,并嘗試創建一個通用部分來將各種表單數據保存到數據庫中。我寫下了以下代碼,將所有數據發送到 php 字段,從而將其發送到數據庫。但問題是,它給了我一個錯誤。if(isset($_POST['data_for']) && $_POST['data_for']=='save') {    $data = $_POST['formdata'];    print_r($data); // This is showing proper array as an output    foreach ($data as $key => $value) {             echo $value['name']; //This gives the key (index value) of the form "Eg. email"        echo $value['value']; //This gives the value of the user input "eg. [email protected]"                $$value['name'] = $value['value']; //This line gives error as "Array to string conversion"    }    echo $email; //This is just a test to print a variable created in runtime    //The insertion to database code goes here.}上面的代碼是從下面的jquery獲取值$(document).on('submit','form.cat1', function(e){    e.preventDefault();    var forum = $(this).attr('forum');    var method = $(this).attr('method');    var nonce = $(this).attr('nonce');    var data_for = $(this).attr('data-for');    var formdata = $(this).serializeArray();    //alert(formdata);    $.ajax({        url:'formSubmitPoint.php',        method:method,        data:{formdata:formdata, nonce:nonce, forum:forum, data_for:data_for},        //processData: false,        //contentType: false,         success:function(data){            console.log(data);            if (data['result']=='success') {                if (data['action']=='redirect') {                    window.location.href=data['location'];                }                if (data['action']=='show') {                    $(data['location']).html(data['message']);                }            }            if (data['result']=='error') {                if (data['action']=='show') {                    $(data['location']).html(data['message']);                }            }        },        error:function(data){            console.log(data);        }    });})
查看完整描述

1 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

$$value['name'] 當 $value['name'] 的值為 email 時會給我 $email


沒有辦法做到這一點。您可以通過執行以下操作來存儲它的值或對該對象的引用


$email = $value['value'];  //this is a copied object

$email = &$value['value']; //this is a reference

編輯

你可以做


foreach ($data as $key => $value) {     

    echo $value['name'];

    echo $value['value'];


    $text  = $value['name'];

    $$text = $value['value'];


    echo $email;

}

您無法從數組創建變量,因為您會將數組轉換為字符串。您必須創建一個字符串類型變量來幫助它。


foreach ($data as $key => $value) {     

    $text  = $key;

    $$text = $value;


    echo $email;

}


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號