Ajax serialize; Cant 'read' variables in PHP

I have this code:

var data_string = $('form#frm').serialize();
    $.ajax({
        type:       "POST",
        url:        "/send.php",
        data:       data_string,
        success:    function(data) {
        alert(data);

And in the php file:

$to = mysql_real_escape_string($_POST['email']);    //email
$name = mysql_real_escape_string($_POST['name']);   //name of tipper
$msg = mysql_real_escape_string($_POST['msg']);     //message
echo $name; //EXAMPLE

The above warning in ajax code SHOULD notify the published variable $name. However, a warning message appears, but it is empty.

I think this is due to the serialization part.

echoing 1 or 0 from php is working fine, and 1 or 0 is displayed in the warning field.

Any ideas on something wrong?

thank

EDIT:

It was found that when warning serialized data:

 alert (data_string);

I get "undefined" ...

And here is the form:

<form name='frm' id='frm' action='send.php' method='post' onsubmit='tip_func(); return false;'>

<input type='text' name='name' id='name'>

tip_func () is a function in which all of the above ajax values ​​...

+3
source share
5 answers

data_string? , , !

var data_string = $('#frm').serialize();
alert(data_string);
+1

.serialize() :

. " " . , ​​ . , . .

? data_string?

+2

javascript, json (http://json.org/), , .

Ultimately, you can spend time making serialization work, but your time can be more productive if you think about making changes.

+1
source

It seems to be working now, I replaced:

var data_string = $('form#frm').serialize();

WITH

var form = document.getElementById('frm');
var data_string = form.serialize();

It really bothers me ...

+1
source

Try print_r($_POST)instead to see if your $ _POST rolls will be sent at the end.

0
source

Source: https://habr.com/ru/post/1776271/


All Articles