I have a basic form where I am storing (in the client side) the values in a json format inside a textarea. I then plan to store these values in mysql database. I am using json_decode
to place the json object into a regular php array. But when I check print_r($personArray)
there is nothing. Therefore everytime I submit the form nothing gets stored in mysql database because the php array is empty. How can I store the values in the text area in mysql database? Here is a LIVE DEMO
if(isset($_POST['submit'])) {
$data = $_POST['data'];
echo $data;
$personArray = json_decode($data, true);
print_r($personArray);
foreach($personArray as $key => $value){
$main_role1 = ($value['main1'] == "true") ? 1 : 0;
$main_role2 = ($value['main2'] == "true") ? 1 : 0;
$person_fname = $value['firstName'];
$person_lname = $value['lastName'];
$person_phone = $value['phone'];
$query_init2 = "INSERT INTO person (main_role1, main_role2, first_name, last_name, person_phone) VALUES (:main_role1, :main_role2,:person_fname,:person_lname, :person_phone);";
$query_prep2 = $db_con->prepare($query_init2);
$insert_result2 = $query_prep2->execute(array(
"main_role1" => $main_role1,
"nmain_role2" => $main_role2,
"person_fname" => $person_fname,
"person_lname" => $person_lname,
"person_phone" => $person_phone
));
}
}
HTML
<textarea name="data" rows='5' cols='60'>
JSON Object
[
{
"firstName": "Danny",
"lastName": "LaRusso",
"ciscoID": "123",
"academyID": "1",
"email": "[email protected]",
"phone": "(555) 121-2121",
"fax": "(123) 123-4567",
"contact_role": true,
"netacadContact": true,
"netacadStaff": false,
"netacadSuccess": false,
"instructor_role": false
},
{
"firstName": "Sensei",
"lastName": "Miyagi",
"ciscoID": "456",
"academyID": "1",
"email": "[email protected]",
"phone": "(555) 444-2222",
"fax": "(123) 123-4567",
"contact_role": false,
"netacadContact": false,
"netacadStaff": false,
"netacadSuccess": false,
"instructor_role": true
}
]