0

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

6
  • Does alert(postDatas); work? Commented Jun 20, 2013 at 19:20
  • @Sergio yes, that's how I was able to get the value of postDatas Commented Jun 20, 2013 at 19:20
  • Is echo the same as return? If it isn't you just might not be sending anything back to alert success. Commented Jun 20, 2013 at 19:27
  • @AndrewPeacock it worked for me before, this one though has been giving me problems. maybe cause i use serialized with array Commented Jun 20, 2013 at 19:28
  • @Sergio it is in array, also i still cant get the sessions to be set up, this $_SESSION["thisistessss"] = "hello"; never gives me anything. I have a different page to check the sessions Commented Jun 20, 2013 at 19:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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...

1 Comment

i doubt it since i have setup a session variable before any conditions

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.