简体   繁体   中英

PHP says Undefined even when it is clearly defined in AJAX call?

I'm working on some function which needs to call php using ajax and get some values. How ever I'll post my JS function part below.

JavaScript

$.ajax({
        type: "POST",
        url: 'php/getAccountJobs.php',
        ContentType:"application/json",
        dataType:'json',
        data:{email:$.cookie("email")},
        success : function(arr)
        {
            for(var i=0 ; i<parseInt(response); i++) // number of jobs are the response we use there
            {
                var jobmsg = arr[2*i] ; 
                var imgurl = arr[2*i+1]; 
                $.ajax({
                        type: "POST",
                        url: "php/addDivAccountJobs.php",
                        dataType:'json',
                        data: {imgURL:imgurl,message:jobmsg},
                        }).done(function( html ){ alert(html);});
            }
        }
        });

So I checked the console log and saw this following error.

Notice: Undefined index:imgURL

So what I thought was to use isset function to test whether imgURL value is being set or not.

PHP

<?php
session_start();
if(isset($_POST["imgURL"]) && isset($_POST["message"]))
{
    $username = $_SESSION["firstname"]." ".$_SESSION["lastname"];
    $imgurl = $_POST["imgURL"];
    $msg = $_POST["message"];
    $fullecho = '<div class="col-md-4">
                    <div class="profile-card text-center">
                        <img class="img-responsive"src="'.$imgurl.'">
                        <div class="profile-info">
                          <h2 class="hvr-underline-from-center" id="jobOwnerName">'.$username.'</h2>
                          <div id="jobMessage">'.$msg.'.</div>
                        </div>
                     </div>
                </div>';
    echo json_encode($fullecho);
    die();
}
else
    echo json_encode("error");
?>

As I expected what I received was error message in alert box.

My Problem

I went through every stackexchange question and tried to find the reason behind it and fix it. but none of those solutions gave me a proper answer. Please can someone help me to find the problem?

when your json property has undefined value its completely disapear from json. so it because arr[2*i] or arr[2*i+1] somewhere get undefined value in the loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM