我正在開發一個項目,并嘗試創建一個通用部分來將各種表單數據保存到數據庫中。我寫下了以下代碼,將所有數據發送到 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); } });})
嘗試從數組中創建具有輸入字段名稱的變量但不起作用
慕工程0101907
2023-08-19 17:52:03