doukong9982 2014-10-15 23:10
浏览 24

使用动态网址从javascript发布到php

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条回答 默认 最新

  • douzhuang2016 2014-10-15 23:19
    关注
    $('.postData')
    

    refers to a class.

    Use:

    $('#postData')
    

    for an id.

    Also instead of

    url: ' + url + ',
    

    I would use

    url: $("#postData").attr("href"),
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭