0

I am having trouble getting a basic ajax POST to work. I switched to an onclick after I was having trouble getting using a jquery .click, among other things. Just wondering if I am making some blatant mistake or what. If no obvious mistake, it may be something with apache? Not too much experienced here so any help would be appreciated.

Here is a link to a function:

<a href="markerpages.php" onclick="postData()">click this for php page</a>

Here is the function:

function postData() {
            console.log("outside ajax is working");

            $.ajax({
                type: "POST",
                url: "/markerpages.php",
                data: {
                    source1: "some text",
                    source2: "some text 2"},
                success: function (data) {
                    console.log("inside ajax is working");
                },
                error:  function () {
                    console.log("ajax post failed")
                }
            });

here is what I have on my php webpage:

<?php
 if (isset($_POST['source1'])) 
        $src1 = $_POST['source1'];
    else $src1 = "post data not obtained";

    echo $src1;

            echo "<pre>" . print_r($_REQUEST, 1) . "</pre>";

    print_r($_POST);
    var_dump($_POST);
    var_dump($_POST);die;
 ?>

I am not returning errors in firebug, and I am getting the log statements I placed inside ajax and outside, just not getting empty arrays on the PHP page. Sincere thanks for any help.

ambe5950
  • 75
  • 2
  • 11

3 Answers3

2

You are sending the user to the page using the anchor tag. If you do this, all post data is lost. You need to replace the url in the anchor tag with # to make sure the user stays on the page:

<a href="#" onclick="postData()">click this for php page</a>
Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • That might be the answer i have been looking for! I am sending them to a dynamically generated page but I suppose I shouldn't try to make the happen at the same time I POST the data. – ambe5950 Oct 17 '14 at 08:09
  • so what if I want to send them to a dynamically generated PHP page, but I also want to POST data to that same page(I know my simplified example doesn't do this, but this is actually what I am doing)? – ambe5950 Oct 17 '14 at 08:12
  • You could wrap it all in a html form, put the post data in hidden fields and use the anchor tag as a submit button. It all depends on what exactly you want to do. – Jerodev Oct 17 '14 at 08:14
  • Thanks, I'll give this a twirl tomorrow. – ambe5950 Oct 17 '14 at 08:22
  • Got it working via a jquery redirect http://stackoverflow.com/questions/8389646/send-post-data-on-redirect-with-javascript-jquery – ambe5950 Oct 17 '14 at 20:43
1

First of all you should put a return false; in the js function, doing so there will not be any redirects,

Check in Developer Network your request, if the url is correct and if you are retrieving any errors during the ajax post.

Alex Doe
  • 934
  • 6
  • 12
0

When clicking the link, postData() is being called, but then the browser is loading the URL specified in your <a href>. You can do one of three possible things:

  1. Add the following line at the top of your postData() function: event.preventDefault(). That will stop the original event clicking action, and is the preferred method.

  2. Change your onclick attribute and add return false to prevent the URL clicking behavior. Ex: onclick="postData(); return false;

  3. Change the <a href="/markerpages.php"> to <a href="#">. But this is not a good approach since the page's URL will change to mypage.htm#.

Another helpful debugging tip: you can output the data variable (the returned value from your PHP script) like so: console.log(data);.

And since you seem to be hand building a form which will eventually pass values from the server back to the front-end, I suggest looking at this article: Pass a PHP Array to Javascript as JSON using AJAX and json_encode()

hargobind
  • 582
  • 2
  • 20