1

I am working on a project and trying to create a generic section to save all kinds of form data to the database. I wrote down the following code to send all the data to php field and hence send it to the database. But the issue is, its giving me an error.

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.

}

The above code is getting values from the below 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);
        }
    });
})

And the jquery pulls data from the below 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> 
6
  • $$value['name'] = $value['value'[;, fix the syntax $$value['name'] = $value['value']; Commented Jul 24, 2020 at 2:49
  • Your second "[" should be a "]" here: $value['value'[ Commented Jul 24, 2020 at 2:50
  • What is the meaning of $$? Commented Jul 24, 2020 at 2:52
  • Sorry it was a typo here... I have written it correctly in my code, still its not working. Commented Jul 24, 2020 at 2:52
  • @Nikkorian The $$var (double dollar) is a reference variable that stores the value of the $variable inside it. Commented Jul 24, 2020 at 2:54

1 Answer 1

1

$$value['name'] Will give me $email when the value of $value['name'] will be email

There is no possible way of doing that. You can store it's value, or a reference to that object by doing

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

EDIT

You can do

foreach ($data as $key => $value) {     
    echo $value['name'];
    echo $value['value'];

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

    echo $email;
}

You can't create a Variable Variable from an array, because you would convert an array into a string. You must create a string type variable to help it.

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

    echo $email;
}
Sign up to request clarification or add additional context in comments.

10 Comments

@nagyi, it works in other projects with the same way. I have been working on the same set of code for long. This time there is something that I am missing which leads to the error.
@VikashMishra can you please tell me the difference between $email, and it's value 'email', if not a reference/or a new object? Or can you tell me what $$value['name'] does?
@nagyi I am just trying to create a new variable with the value of the variable. Eg $$value['name'] Will give me $email when the value of $value['name'] will be email when it will be something else the variable name will be something else. I am not hardcoding the name of the variable as it will be generated in runtime.
That's ok, but why do you need that? Why can't you just store the value in a normal variable? xd Why do you benefit from doing that instead of a logical way?
It helps me in multiple aspects, like I do not have to hardcode the name of the variable for each and every form that I submit using this method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.