0

次のような配列を持つphpファイルがあります。

 $selectShiftarray = array();
$shift=$_POST['selectShift'];

if ($shift)
{
    foreach ($shift as $value)
    {
        array_push($selectShiftarray,$value);
    }
}

値を別の php ファイルに渡すには、AJAX 内の $selectShiftarray にアクセスする必要があります。

$.ajax({
    type: 'POST',
    url: '../c/sampleTest.php', //data: {data = <?php $POST['selectShift'] ?> },//"id=78&name=Allen",
    dataType: 'json',

    success: function (json) {
        //alert('successful');
        $.plot($("#placeholder"), json, {
            series: {
                stackpercent: true,
                bars: {
                    show: true,
                    barWidth: 0.6,
                    align: "center"
                }
            },
            xaxis: {
                tickSize: 1
            },
            yaxis: {
                max: 100,
                tickFormatter: function (v, axis) {
                    return v.toFixed(axis.tickDecimals) + '%'
                }
            }
        });
    }
});

AJAXのデータフィールドの配列値をsampleTest.phpに渡して計算してみました。

2 つの php ファイル間で配列値を直接渡す場合、現在の php ファイル内に sampleTest.php を含めたいと思います。私の要件は、php ファイル内に sampleTest.php ファイルを含めないことです。そのため、AJAX の POST メソッドを使用します。しかし、配列を sampleTest.php ファイルに渡すことができません。私はAJAXが初めてなので、この問題を解決できません。誰でもこの問題を解決するのを手伝ってくれますか?

4

2 に答える 2

0

この問題を解決するのに役立ったガイダンスを提供してくれたaxelbrzとBergiに感謝します。

これがコントローラーの私のAJAXコードです:

 <script type="text/javascript">
    $(document).ready(function(){

    var jsonobj = <?php echo json_encode($selectShiftarray); ?>;


    $.ajax({type:'POST',url:'../c/sampleTest.php', data :  { shift : jsonobj } ,
    dataType: 'json',

    success:function(json){
    //alert('successful');
    $.plot($("#placeholder"), json, {
    series: {
        stackpercent: true,
        bars: { show: true, barWidth: 0.6, align: "center" }
    },
        xaxis: {tickSize : 1},
        yaxis:{max:100, tickFormatter: function(v,axis){ return v.toFixed(axis.tickDecimals)+'%'}}
    });
 }
 });
 });

</script>
<div id="placeholder" style="width:600px;height:300px; top: -401px; left: 500px;">
</div>

私のsampleTest.phpコードは、コントローラーから送信された配列を受信して​​処理します。

$selectShiftarray = array();
$shift=$_POST['shift'];

if ($shift)
{
    foreach ($shift as $value)
    {
        array_push($selectShiftarray,$value);
    }
}
于 2012-12-05T03:57:33.563 に答える
0

$ selectShiftarray配列をAJAXコードに出力して、AJAXを介して../c/sampleTest.phpに送信します。

function sendArrayToPHP(phpFile, parameterName, jsArray) {
    $.ajax({
        url: phpFile,
        async: false, // Depending on what you want
        type: "POST",
        data: { parameterName : JSON.serialize(jsArray) }
    }).done(function( data ) {
        // Sent!
    });
}

var arrayExample = $.parseJSON("<?=$selectShiftarray?>"); // This way
// You can modify here the array if you want using JavaScript
sendArrayToPHP("../c/sampleTest.php", "arrayParameter", arrayExample);

../c/sampleTest.phpから配列を受信する(JSONのデコード):

$selectShiftArray = json_decode($_POST['arrayParameter'], true);
于 2012-12-04T06:16:26.867 に答える