javascript - Ajax serialize; Cant 'read' variables in PHP - Stack Overflow

I have this code:var data_string = $('form#frm').serialize();$.ajax({type: "POST&q

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 alert in the ajax code SHOULD alert the posted $name variable. However, the alertbox es up, but it is blank.

I think it has to do with the serialize part.

echoing 1 or 0 from php works fine, and the alert box shows the 1 or 0.

Any ideas whats wrong?

Thanks

EDIT:

Found out that when alerting the 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 the function where all the above ajax is...

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 alert in the ajax code SHOULD alert the posted $name variable. However, the alertbox es up, but it is blank.

I think it has to do with the serialize part.

echoing 1 or 0 from php works fine, and the alert box shows the 1 or 0.

Any ideas whats wrong?

Thanks

EDIT:

Found out that when alerting the 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 the function where all the above ajax is...

Share Improve this question edited Nov 22, 2010 at 17:13 asked Nov 22, 2010 at 17:07 user188962user188962 7
  • 1 What does the serialized data look like? – Pekka Commented Nov 22, 2010 at 17:10
  • Can you post some example form HTML? – Richard Marskell - Drackir Commented Nov 22, 2010 at 17:11
  • @Camran - in your form html you need to replace < with &lt; or it doesn't show properly – Richard Marskell - Drackir Commented Nov 22, 2010 at 17:14
  • 1 @Camran - Do you perform mysql_connect before calling mysql_real_escape_string ? – ajreal Commented Nov 22, 2010 at 17:18
  • I don't understand why the alertbox returns undefined. It doesn't find the form. I am sure the name and all is correct. – user188962 Commented Nov 22, 2010 at 17:20
 |  Show 2 more ments

5 Answers 5

Reset to default 2

According to the .serialize() documentation:

Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Data from file select elements is not serialized.

Did you check all of this? Have you tried alerting data_string?

Have you tested the data_string? Try doing an alert on it first to check it has data in it!

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

If you are trying to pass data to the javascript function then why not just use json (http://json/) for this, as it is designed to serialize and there are numerous options for most languages, so you don't have to reinvent the wheel.

Ultimately you can spend time working on how to get serialize to work, but your time may be more productive if you consider making a change.

Seems it works now, I replaced:

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

With:

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

Very confusing to me...

Try print_r($_POST) instead to see if your $_POST vars are posted after all.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744858446a4597547.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信