I have an array in JavaScript that has multiple objects. Inside the object has multiple elements. I am trying to insert those elements in PHP using AJAX and into SQL.
I can pass the array into PHP fine, but when I encode the array and echo the data back, I have a bunch of strings. I just need to break down those elements and insert them into SQL.
How do I get the php similar to my 'desired results' posted at the bottom so that I can insert multiple rows of data into SQL in one query with the data 'SqlArr'
Any help is most appreciated.
HTML
<tr class="rows" id="row3" >
<td class="celltimes4a"id="row3Project"></td>
<td class="celltimes4c"id="row3Name">General</td>
<td class="celltimes4"id="row3Sun" ><input id="num3Sun" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Mon" ><input id="num3Mon" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Tue" ><input id="num3Tue" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Wed" ><input id="num3Wed" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Thu" ><input id="num3Thu" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Fri" ><input id="num3Fri" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4"id="row3Sat" ><input id="num3Sat" class="alignRight" type="text" name="hours" value="" onchange="tott(this)"></input></td>
<td class="celltimes4b"id="total3"></td>
</tr>
Javascript
function tott(element) {
var totwLeg = element.id;
var splitNumero = totwLeg.split(/([A-Za-z]+)([0-9]+)/);
var getNumero = splitNumero[2];
var getDay = splitNumero[3];
EmpId = document.getElementById('num1').value;
WkEnd = document.getElementById('theDate').value;
Day = document.getElementById('row1' + getDay).innerHTML;
Title = getNumero - 2;
Description = getNumero - 2;
Value = document.getElementById('num' + getNumero + getDay).value;
AbbrevJob = Title;
//Empdata = 'EmpData' + cnt + '';
temp = {
EmpId, WkEnd, Day, Title, Description, Value, AbbrevJob
}; // this is where the columsn should match or go inside the columsn in the table... hold one moment
SqlArr.push(temp);
//JSON.stringify(SqlArr);
}
function saveMe() {
$.post('php/GetEmployeeData.php', {
'SqlArr': SqlArr,
datatype: "json"
}, function(SqlArr) {
alert(SqlArr);
})
}
PHP
<?php
include_once 'DbConnectPSI.php';
global $connect;
global $record3;
global $myData3;
$SqlArr =$_POST['SqlArr'];
$SqlArr =json_encode($_POST['SqlArr']);
//$SqlArr =json_decode($SqlArr);
//$SqlArr=implode(',',$SqlArr);
//$SqlArr=explode(',',$SqlArr);
//$SqlArr=$SqlArr[0];
//$myData3="Insert Into EmployeeTimesheets(EmpId, WkEnd, Day, Title, Description, Value,TimeStamp, AbbrevJob) Values('$SqlArr[0][EmpId]','$SqlArr[0][WkEnd]','$SqlArr[0][Day]','$SqlArr[0][Title]','$SqlArr[0][Description]','$SqlArr[0][Value]', getDate(),'$SqlArr[0][AbbrevJob]')"; // this is my sql statment that is supposed to be inserting the data into the table.
//$myData3="Insert Into EmployeeTimesheets Values('$var1','$var2','$var3','$var4','$var5','$var6', getDate())";
//$myData3="Insert Into EmployeeTimesheets Values('$EmpId','$WkEnd','$Day','$Title','$Description','$Value', getDate(),'$AbbrevJob')";
//$record3 = odbc_exec($connect, $myData3);
echo($SqlArr);
odbc_close($connect);
?>
alert of data
Desired results so the values can be inserted into SQL
Array ( [0] => 7 [1] => 09-19-2015 [2] => 09-13-2015 [3] => 1 [4] => 1 [5] => 7 [6]=>1 )
Array ( [0] => 7 [1] => 09-19-2015 [2] => 09-13-2015 [3] => 2 [4] => 2 [5] => 4 [6]=>2 )