我正在开发一个项目,并尝试创建一个通用部分来将各种表单数据保存到数据库中。我写下了以下代码,将所有数据发送到 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. abc@xyz.com"
$$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);
}
});
})
浮云间