user123
user123

Reputation: 5407

pass data while loading to the page loaded by ajax

I am loading page url.php and want to pass $x to url.php.

var myid=mydata.id;
   $.ajax({
                         url:'url.php'
                         ,async:     true
                         ,cache:     false
                         , data : 'post_field=' + myid
                         ,dataType:  'html'
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }
                       }
                             );

at url.php

<?php
$myid=$_POST[myid];
echo "myID is : <br>";
echo myid;
?>

What's wrong here?

Gives errror:

<br />
<b>Notice</b>:  Use of undefined constant myid - assumed 'myid' in <b>/opt/lampp/htdocs/FB/ec2/url.php</b> on line <b>2</b><br />
<br />
<b>Notice</b>:  Undefined index: myid in <b>/opt/lampp/htdocs/FB/ec2/url.php</b> on line <b>2</b><br />
myID is : <br><br />
<b>Notice</b>:  Use of undefined constant myid - assumed 'myid' in <b>/opt/lampp/htdocs/FB/ec2/url.php</b> on line <b>4</b><br />
myid<html>

Upvotes: 1

Views: 226

Answers (2)

Ajith S
Ajith S

Reputation: 2917

Try this:

var formData = $('#formId').serialize();
$.ajax({
    type: 'POST',
    url: 'url.php',
    async: true,
    data: formData,
    cache: false,
    success: function(data){
        $('body').html(data);
        FB.XFBML.parse();
    }
});

Upvotes: 1

cedric
cedric

Reputation: 11

for passing post value... use

$.ajax({
                         url:'url.php'
                         ,async:     true
                         ,cache:     false
                         ,dataType:  'html'
                         , data : 'post_field=' + $x  
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }
                       }
                             );

             }

if you are using jquery and want to post form input , you can do

var data = $('#my_form').serialize();

$.ajax({
                         url:'url.php'
                         ,async:     true
                         ,cache:     false
                         ,dataType:  'html'
                         , data : data  
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }
                       }
                             );

                 }

Upvotes: 1

Related Questions