user1850420
user1850420

Reputation: 53

AJAX POST will not work loading PHP page in DIV

my patience with ajax is gone. no matter cannot get this working. To be honest, I have not tried this outside a DIV because the intention is specifically use AJAX to load and post a form across to a page within the confines of said DIV.

Many Thanks for the advancement of what is to come.

The #contentmainpane is a DIV. I have ...

if (isset($_POST['l'])) {
echo $_POST['l']; }
else { 
echo 'nope'; }

... in the CART page loads fine but does not receive the post from the BOOK page(so I get nope echoed on CART.PHP within #contentmainpane DIV). Here is the offending BOOK page code:

<form id="form1" method="POST">
<input id="l" name="l" size="45" type="text" value="book">
<input id="submit" name="submit" value="save" type="submit">
</form>


<script>
$('#form1').submit(function(event){

event.preventDefault();

var book = $('#book').serialize();
var cart = '/content/pages/cart.php';
var formdata = {"book": book}


$.ajax({

type: "POST",

url : "/content/pages/cart.php"

data: formdata,

success: function(msg){

$("#contentmainpane").load(cart);

}
});
});
</script>

Upvotes: 3

Views: 2040

Answers (4)

user1850420
user1850420

Reputation: 53

The problem has been resolved

Thank you all for your suggestions and efforts. A user on codingforums resolved this ajax post issue.

Here's the thread http://www.codingforums.com/showthread.php?p=1294868#post1294868

Best Regards to all coders

Upvotes: 0

pocesar
pocesar

Reputation: 7050

You can tie PHP with jQuery through AJAX, easily, using phery library. http://phery-php-ajax.net

you didn't mention what your #book is, I guess it's a form right? In that case, using data-related in your form will make it able to join the two forms in one AJAX call.

In your case, it would be like:

<form id="form1" data-remote="function" method="POST" data-related="#book">
  <input id="l" name="l" size="45" type="text" value="book">
  <input id="submit" name="submit" value="save" type="submit">
</form>

In your PHP it will be:

function func($data){
  $r = new PheryResponse;
  // $data['l'] got what you want, also $data['submit'] is available here
  // do whatever you need to do, then update your #contentmainpane
  $r->jquery('#contentmainpane')->html('your cart.php contents');
  return $r;
}

Phery::instance()->set(array(
  'function' => 'func'
))->process();

your logic will be server side now, and not client side anymore. doing this way you can have much more flexibility, since the code that comes from the server is free and can be changed when needed, not the same happens with client side code.

Upvotes: 1

anson
anson

Reputation: 4164

Your data needs to be an object with an existing key of value l.

Since your requesting a $_POST value of 'l', you must pass a data object with a key 'l'. From your code, your variable formdata which you pass in the AJAX call is currently an { 'book' : book}.

Instead it must be either { 'l' : book }

OR, in your PHP code

if(isset($_POST['book'])){...

Upvotes: 2

VIDesignz
VIDesignz

Reputation: 4783

I noticed you are serializing a form with an ID of #Book but your html doesnt show an ID of #book... Also, you need a comma after the url

Try This

$('#form1').submit(function(event) {
    event.preventDefault();
    var book = $('#form').serialize();
    var cart = '/content/pages/cart.php';

    $.ajax({
        type: "POST",
        url: "/content/pages/cart.php",
        data: book,
        success: function() {
            $("#contentmainpane").load(cart);
        }
    });
});

Upvotes: 2

Related Questions