将变量拉入AJAX后期函数的数据中

时间:2013-02-07 20:11:17

标签: php javascript ajax post

我有以下函数调用,我试图将变量数据的值拉入函数的data:部分。我已经做到这一点,但没有这样的运气让它工作。 在php中我使用$ _POST ['test']来尝试拉取值。

感谢

function fnc()
 {
   var oButton = document.getElementById("addData");
   var data = document.getElementById("dataInput");
   var display = document.getElementById("display");


     display.innerHTML += "<a id='dispchk'>"+ data.value +"</a>" +"<br />" ;
}
$.ajax({
    type: "POST",
    url: "chkinpost.php?eventid=<?php echo $_GET['eventid']; ?>&eventname=<?php echo $_GET['eventname']; ?>",
    data: "test="+ data.value
});

2 个答案:

答案 0 :(得分:2)

要么不使用function fnc()

var oButton = document.getElementById("addData");
var data = document.getElementById("dataInput");
var display = document.getElementById("display");
display.innerHTML += "<a id='dispchk'>"+ data.value +"</a>" +"<br />" ;
$.ajax({
    type: "POST",
    url: "chkinpost.php?eventid=<?php echo $_GET['eventid']; ?>&eventname=<?php echo $_GET['eventname']; ?>",
    data: "test="+ data.value
});

将您的AJAX请求移至function fnc()

function fnc()
{
  var oButton = document.getElementById("addData");
  var data = document.getElementById("dataInput");
  var display = document.getElementById("display");
  display.innerHTML += "<a id='dispchk'>"+ data.value +"</a>" +"<br />" ;
  $.ajax({
    type: "POST",
    url: "chkinpost.php?eventid=<?php echo $_GET['eventid']; ?>&eventname=<?php echo $_GET['eventname']; ?>",
    data: "test="+ data.value
  });
}

答案 1 :(得分:0)

Either pass a string or a plain object as data parameter

var url = "chkinpost.php?eventid=<?php echo $_GET['eventid']; ?>&eventname=<?php echo $_GET['eventname']; ?>"

//var data = { value: "some value"}
//or
var data = 'this is a chicken';

$.ajax({
  type: "POST",
  url: url,
  data: data,
});