首页 > 解决方案 > 尝试从数组中创建具有输入字段名称的变量但不工作

问题描述

我正在做一个项目并尝试创建一个通用部分以将各种表单数据保存到数据库中。我写下以下代码将所有数据发送到 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);
        }
    });
})

jquery 从下面的 html 中提取数据

<form class="was-validated cat1" method="post" forum='feedback' data-for="save" nonce="{$nonce}">
  <div class="form-group">
    <label for="newPass">Name</label>
    <input type="text" class="form-control" id="name" placeholder="Your Name" name="name" required>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill out this field.</div>
  </div>
  <div class="form-group">
    <label for="newPass">Email</label>
    <input type="email" class="form-control" id="email" placeholder="Your Email Address" name="email" required>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill out this field.</div>
  </div>
  <div class="form-group">
    <label for="newPass">Contact Number</label>
    <input type="number" class="form-control" id="contact" placeholder="Your contact number" name="contact" required>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill out this field.</div>
  </div>
  <div class="form-group">
    <label for="newPass">Message</label>
    <textarea class="form-control" id="message" name="message" required></textarea>
    <div class="valid-feedback">Valid.</div>
    <div class="invalid-feedback">Please fill out this field.</div>
  </div>
  <button type="submit" name="submit" class="btn btn-primary btn-sm">Submit</button>
</form> 

标签: phphtmljquery

解决方案


当 $value['name'] 的值为 email 时,$$value['name'] 会给我 $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;
}

推荐阅读