我正在开发一个项目,并尝试创建一个通用部分来将各种表单数据保存到数据库中。我写下了以下代码,将所有数据发送到 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); } });})
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;
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消