2

I'm storing different values in the sessionStorage during a longer process. At the end i want to read all the data and send it to my php backend using ajax. The problem is that php seems to take every value including quotes. Here is what i got:

// Reading data from storage
var imgb = sessionStorage.getItem("img_b_id");
....
// perpare data
var oData = {
            imgb: imgb,
            ...
};

// Sending data
$.post( "../php/direct/create.php", oData).done(function(data) {
    if(data==true) {
        window.open("step5.php", "_self");
    } else {
        alert("Error: " + data.toString()); 
    }
});

This is working perfectly fine. However, php will read the value of imbg with quotes. Example:

echo $_POST['imgb'];
/* Returns "asd" instead of asd

The image below shows that my data is stored without the quotes (chrome sessionstorage screenshot) enter image description here

Do you have any suggestions on how to solve this. I currently replace the quotes, but I'd love to fix this problem at it's root...

4

1 回答 1

1

我已经尝试过您的代码(使用 Firefox 和 Chrome)并且

echo $_POST['imgb'];

打印不带引号的字符串。考虑一下,即使您在会话存储中设置了一个数字,例如

sessionStorage.setItem("img_b_id", 100);

PHP 在服务器上接收该值作为字符串,因此您可以使用 PHP 函数 intval()、floatval() 等对其进行转换。

http://php.net/manual/en/function.intval.php

于 2015-04-25T13:48:35.977 回答