dongshushen4392 2013-06-20 19:18
浏览 45
已采纳

通过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

  • 写回答

2条回答 默认 最新

  • dtnwm4807 2013-06-20 19:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件