1

I have a problem regarding on how to get the value of the array variable passed by .post in jquery into my php page

this is my jquery code:

minDate = [];
hoursWork = [];
empId = [];
        $('.minDate').each(function() { 
            minDate.push($(this).val());
        });

        $('.hoursWork').each(function() { 
            hoursWork.push($(this).val());
        });

        $('.empId').each(function() { 
            empId.push($(this).val());
        });

            $.post('rtInsert.php', { minDate: minDate, hoursWork: hoursWork, empId: empId }, function(data) { 
        alert(data);
    });

How can I get the passed data in my rtInsert.php

i tried

$minDate = $_POST['minDate'];
$empId = $_POST['empId'];
$workHours = $_POST['hoursWork'];

now how will i get the individual value of the array because all the 3 variables is an array

All I know is a single array using foreach() but what if there are 3 arrays passed

Is there any idea how I can get it or how can I passed into single array.

Thanks in Advance.

1

1 Answer 1

3

You can use the JSON.stringify(data); to turn them into JSON and prase it on the server side as this; $data = json_decode($json);.

Your code will then become this:

var minDate = [];
var hoursWork = [];
var empId = [];
$('.minDate').each(function() { 
    minDate.push($(this).val());
});

$('.hoursWork').each(function() { 
    hoursWork.push($(this).val());
});

$('.empId').each(function() { 
    empId.push($(this).val());
});

$.post('rtInsert.php', { minDate: JSON.stringify(minDate), hoursWork: JSON.stringify(hoursWork), empId: JSON.stringify(empId) }, function(data) { 
    alert(data);
});

And your server side ccode:

<?php
$mindate = json_decode($_POST['minDate']);
$hourswork = json_decode($_POST['hoursWork']);
$empid = json_decode($_POST['empId']);
foreach($mindate as $k=>$val)
{
    $date = $val;
    $work =$hourswork[$k];
    $id =$empid[$k];
    //...
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.