Reputation: 341
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>
Upvotes: 1
Views: 103
Reputation: 1644
$$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
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;
}
Upvotes: 1