$ .ajax错过了发布到php的一些数据

时间:2022-04-20 01:35:11

I have a problem where some of my data is not getting through to php. I think the problem lies in ajax sending it. I send about 10 attributes, from which some are strings and some are integers. This is just simplified example of what I did. Few of the values given that it misses are integers, I think. And some values are got from cordova.Localstorage with storage.getItem("itemkeyname"); There's no problem with connection, because I get at least error message back saying "missing data" etc, etc.. I've tried PHP's isset() instead of empty(), which didn't change anything. var_dump() returns array of send attributes, but few last attributes are cut-off or missing.

我有一个问题,我的一些数据没有通过PHP。我认为问题在于ajax发送它。我发送了大约10个属性,其中一些是字符串,一些是整数。这只是我所做的简化示例。我认为,它错过的几个值是整数。并且一些值来自cordova.Localstorage with storage.getItem(“itemkeyname”);连接没有问题,因为我至少得到错误消息说“丢失数据”等等。我已经尝试过PHP的isset()而不是empty(),它没有改变任何东西。 var_dump()返回发送属性的数组,但很少有最后一个属性被截断或丢失。

//when submitbtn is pressed
$("#submitbtn").click(function () {

    // First I get data from input elements from page
    $name = $("#name").val();
    $name2 = $("#name2").val();

    //debug to see $name's value
    alert("name: " + $name + ", name2: " + $name2);

    // then I check it's not empty/null
    if ($name && $name2) {
        //then call ajax and send data to server
        $.ajax({
            url: "http://localhost:1234/phpfile.php",
            type: "POST",
            data: {
                name: $name,
                name2: $name2
            },
            dataType: "text",
            success: function (response) {
                alert(response);
            },
            error: function (err) {
                $output = JSON.stringify(err);
                alert($output);
            }
        });
    }
});

On the server side phpfile.php

在服务器端phpfile.php

<?php header('Content-Type: text/html; charset=utf-8');
//store missing data on array
$data_missing = array();

if(empty($_POST['name'])) {
    $data_missing[] = "name";
} else {
    $name = trim($_POST['name']);
}

if(empty($_POST['name2'])) {
    $data_missing[] = "name2";
} else {
    $name2 = trim($_POST['name2']);
}

//check there's no data missing
if(empty($data_missing)) {

    //do stuff

} else {
    echo 'missing data: ';
    foreach($data_missing as $missing) {
        echo '$missing , ';
    } 
}
?>

1 个解决方案

#1


0  

  1. echo '$missing , ' won't work should be echo "$missing , "
  2. echo'$ missing,'将无法正常回应“$ missing”,

  3. In your JS code the dataType is defined as "text" (plain), while PHP defines its response as text/html.
  4. 在您的JS代码中,dataType被定义为“text”(plain),而PHP将其响应定义为text / html。

  5. Try to check the input values as:

    尝试将输入值检查为:

    if( !isset($_POST["name"]) || strlen(trim($_POST["name"])) == 0 ) { $data_missing[] = "name"; }

    if(!isset($ _ POST [“name”])|| strlen(trim($ _ POST [“name”]))== 0){$ data_missing [] =“name”; }

#1


0  

  1. echo '$missing , ' won't work should be echo "$missing , "
  2. echo'$ missing,'将无法正常回应“$ missing”,

  3. In your JS code the dataType is defined as "text" (plain), while PHP defines its response as text/html.
  4. 在您的JS代码中,dataType被定义为“text”(plain),而PHP将其响应定义为text / html。

  5. Try to check the input values as:

    尝试将输入值检查为:

    if( !isset($_POST["name"]) || strlen(trim($_POST["name"])) == 0 ) { $data_missing[] = "name"; }

    if(!isset($ _ POST [“name”])|| strlen(trim($ _ POST [“name”]))== 0){$ data_missing [] =“name”; }