0

I am creating a DDBB Insert trough $.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:

$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

one for each variable passed (I have ommited entire URL)

As the posted information is an object, I guess I must decode it someway on PHP, but trying:

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

...has neither workd

Biomehanika
  • 1,530
  • 1
  • 17
  • 45

4 Answers4

1

You need to specify the data that you are sending with contentType parameter. For more references

Community
  • 1
  • 1
SpongePablo
  • 870
  • 10
  • 24
0
    $(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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mahesh Shukla
  • 186
  • 10
-1

You can use this $_REQUEST instead of $_POST in your php file.

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

-2

I think you have to access the $_GET parameters because jquery documentation says:

data Type: PlainObject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. 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).

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

So you will get your data with

$ms_img = $_GET['ms_img'];
Rene Vorndran
  • 693
  • 1
  • 6
  • 13
  • Hi Rene.- No, there's a POST way to get this info. – Biomehanika May 04 '15 at 12:19
  • On the page you linked check out **method** and you'll see you can set AJAX to POST, which he did. **type** is the older way of doing it – Machavity May 04 '15 at 12:21
  • Okay i think i missinterpreted the description to only append it to the GET-request, but if i read it again, one could interpret it that only for GET-requests its appended to the url – Rene Vorndran May 04 '15 at 12:23
  • The comment you're referring to is there to let you know how it will behave in `GET` situations. Regardless, it's not an answer to the OP – Machavity May 04 '15 at 12:30