0

I am trying to get POST from javascript to a php webpage and am using ajax to do so. I am a noob and am therefore having difficulty with the syntax.

all within the same function,

my url is generated by:

var id = markers[i].getAttribute("id");
var name = markers[i].getAttribute("name");
var url = "markerpages.php?name=" + name + "&id=" + id;

then I use this in linking to the webpage:

<a href="'+ url +'" id="postData">link to PHP webpage</a> 

then my jquery is run using that id:

$(document).ready(function() {
            $('.postData').click(function() {
                console.log("outside ajax is working");
                console.log(url);

                  $.ajax({
                    type: "POST",
                    url: ' + url + ',
                    data: {
                    source1: "some text",
                    source2: "some text 2"},
                    success: function (data) {
                        console.log(data);
                        console.log(url);
                        console.log("inside ajax is working");
                    }
                  });
            });
  });

I then have my on my php page:

 if (isset($_POST['source1'])) {
        $src1 = $_POST['source1'];
        echo $src1;
    }

    var_dump($_POST);

I don't fully understand the query syntax necessary here, but even when I simplified it to just the ajax and an on click, I was having difficulty posting to the proper URL, and getting an empty var_dump($_POST). I had been trying to just post to markerpages.php without any id or name attached to it.

initially I had run the ajax as a function outside of this function that was triggered by an onclick() of the link, but I was having too much difficulty passing throughthe dynamic URL, so I settled on this. I am a inexperienced, to say the least.

Sincere thanks for any and all help.

1 Answer 1

1
$('.postData')

refers to a class.

Use:

$('#postData')

for an id.

Also instead of

url: ' + url + ',

I would use

url: $("#postData").attr("href"),
Sign up to request clarification or add additional context in comments.

8 Comments

how does this answer the OP question?
Well the form won't post if the click function is not attached to the correct element.
Based on the original post, it might make sense to leave it as a postData class, but to make the anchor tags have the postData class instead of the postData ID. The OP is pulling the values from an array, so there might be many links which should behave similarly.
Just tried changing it to a class. The jquery is simply not firing as a class or id :/
Do you have many hrefs with the class postData on the page? If so use $(".postData").each(function() { $(this).click(function() { //code here }); });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.