0

I have the below function call and I am trying to pull the value of the variable data into the data: section of the function. I have gotten this far but no such luck getting it to work. In php I am using $_POST['test'] to try an pull the value.

THanks

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
});
4
  • 2
    Your data variable is only in the scope of the fnc function. Commented Feb 7, 2013 at 20:13
  • data: {test: data.value} Commented Feb 7, 2013 at 20:13
  • @PeterM: You can pass data either an object or a query string. Commented Feb 7, 2013 at 20:14
  • @RocketHazmat Ok, how can i get it in scope? Commented Feb 7, 2013 at 20:14

2 Answers 2

2

Either don't use the 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
});

or move your AJAX request into 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
  });
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks buddy. I needed to keep the function so I used the 2nd option.
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,
});

Comments

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.