通过ajax提交php数组无法正常工作

问题描述:

I have this code for my ajax:

function setList(str){
    var postDatas = decodeURI($('#form'+str+'').serialize());
    alert(postDatas);
    // $('#crm-feedback').html('<img src="images/ajax-loader.gif"/>');
    $.ajax({
        url: 'somewhere/setEmail.php',
        type: 'POST',
        //dataType : "json",
        data: postDatas,
        success: function(data){
            // $('#crm-feedback').html('Saved!').css('color','green');
            alert("test");
        }       
    });

}

postDatas value is "email_id[]=1&email_id[]=2&test=asd"

In my form I have a multiple select that has an array for its name, name="email_id[]".

Now my problem is that I cant seem to get my ajax request to succeed, and I cant identify the lying cause of this problem.

I setup some sessions in setEmail.php so that I can check if the ajax request was submitted.

<?php
require_once("Con/st.php");
session_start();
$_SESSION["thisistessss"] = "hello";
if($_POST){
    $email_success=0;
    $nochanges=1;

    $sql_clr = " DELETE FROM table WHERE grp_id = '".$_POST['gid']."' ";
    $res_clr = mysql_query($sql_clr);
    $_SESSION["testagain5"] = $_POST['email_id'];
    foreach($_POST['email_id'] as $key => $val) {
        $_SESSION["testagain31"] = "testsss".$val;

        $_SESSION["testagain3"] = "testsss".$_POST['dval'];

        if($val!='' && $_POST'dval']!='') {
        $_SESSION["testagain4"] = "testsssss".$_POST'dval'];
            //insert into database
            if(stristr($val,'-sms')==true){
                $val = str_replace("-sms","",$val);
                $sms_on = 1;
            }else{
                $sms_on = 0;
            }
            $sql_ins = " INSERT INTO table (grp_id, email_id, email_sched, sms) VALUES ('".$_POST['gid']."','".$val."','".$_POST'dval']."', '".$sms_on."') ";
            $res_ins = mysql_query($sql_ins);
            $_SESSION["testagain2"] = $sql_ins;
            if($res_ins){
                echo "1";
                $nochanges=2;
            }
        }
    }
    echo "1";

}else{
    echo "1";
}

?>

p.s none of the sessions are being set

Thank you

I got this from a project I am working in:

In the html:

<input type="text" name="add[id_item]" />

In the php:

$_POST["add"]["id_item"]

I think you need to treat the variable as an array. Maybe this gives you some idea where to go. Try $_POST['email_id'][0] and see what you get.

I always do jQuery .ajax requests like this:

$.ajax({
    url:'somewhere/setEmail.php?'+postDatas,
    success: function(result) {
        alert(result);
    }
});

Something like that.

It's possible something is going wrong in setEmail.php, and it's just not giving you a result...