简体   繁体   English

在Ajax发送的PHP变量上存储

[英]Store on PHP variables sent by Ajax

I am creating a DDBB Insert trough $.ajax: 我正在创建DDBB插入槽$ .ajax:

$(document).on('click','.submitMessage', function(){
    content=$('textarea').val();
    img=$('#messageImg').val();
    stdMsg=$('.ms_stdMsg').val();
    prefix=$('.prefix').val();
    phone=$('.cuadroTelefono').val();

    $.ajax({
        url: "../actions/newMessage.php",
        type: "POST", 
        data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
        contentType: false, 
        cache: false,  
        processData:false, 
        success: function(data) 
        {   
            alert("Enviado");
        }
    });
});

And this is the way I receive code on newMessage.php : 这就是我在newMessage.php上接收代码的方式:

$ms_content = $_POST['ms_content'];
$ms_img = $_POST['ms_img'];
$ms_prefix = $_POST['ms_prefix'];
$ms_phone = $_POST['ms_phone'];

Console gives an error 控制台出现错误

Notice: Undefined index: ms_content in C:...\\newMessage.php on line 9 注意:未定义的索引:第9行的C:... \\ newMessage.php中的ms_content

one for each variable passed (I have ommited entire URL) 每个传递的变量一个(我省略了整个URL)

As the posted information is an object, I guess I must decode it someway on PHP, but trying: 由于发布的信息是一个对象,我想我必须在PHP上以某种方式对其进行解码,但是尝试:

$ms_content = json_decode($_POST['ms_content']);

...has neither workd ...都没用

You need to specify the data that you are sending with contentType parameter. 您需要使用contentType参数指定要发送的数据。 For more references 有关更多参考

    $(document).on('click','.submitMessage', function(){
        content=$('textarea').val();
        img=$('#messageImg').val();
        stdMsg=$('.ms_stdMsg').val();
        prefix=$('.prefix').val();
        phone=$('.cuadroTelefono').val();

        $.ajax({
            url: "../actions/newMessage.php",
            type: "POST", 
            data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
            cache: false,              
            success: function(data) 
            {   
                alert("Enviado");
            }
        });
    });

Please remove processData:false, contentType: false and then try. 请删除processData:false, contentType: false ,然后尝试。

You can use this $_REQUEST instead of $_POST in your php file. 您可以在PHP文件中使用此$ _REQUEST代替$ _POST。

$ms_content = $_REQUEST['ms_content'];

Instead of 代替

$ms_content = $_POST['ms_content'];

Second Maybe due to url path 第二个可能是由于网址路径

try giving full url path. 尝试提供完整的网址路径。

Third 第三

Provide a contentType. 提供一个contentType。

I think you have to access the $_GET parameters because jquery documentation says: 我认为您必须访问$ _GET参数,因为jquery文档说:

data Type: PlainObject or String or Array Data to be sent to the server. 数据类型:PlainObject或String或Array要发送到服务器的数据。 It is converted to a query string, if not already a string. 如果还不是字符串,则将其转换为查询字符串。 It's appended to the url for GET-requests. 它被附加到GET请求的URL上。 See processData option to prevent this automatic processing. 请参阅processData选项以防止这种自动处理。 Object must be Key/Value pairs. 对象必须是键/值对。 If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below). 如果value是一个Array,则jQuery会根据传统设置(如下所述)的值,使用相同的键序列化多个值。

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

So you will get your data with 这样您将获得您的数据

$ms_img = $_GET['ms_img'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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