hi im trying to access a php variable in an ajax function but apparently it isnt working...i have used an onClick event to activate the ajax function where i pass my local php variable as an argument parameter...
<?php
$name = $_GET['name'];
?>
<html>
<head>
<script language="JavaScript" type="text/javascript">
function ajax_post(x){
var nm = x;
var hr = new XMLHttpRequest();
var url = "my_parse_file.php";
var fn = document.getElementById("first_name").value;
var vars = "todo="+fn+"&name="+nm;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<?php
$display =' Name of list:;
echo <label for="name"></label>
<input type="text" name="name" id="name">
</p>
<p>Name of item:
<input id="first_name" name="first_name" type="text" />
<br /><br />
<input name="myBtn" type="submit" value="Submit Data" onClick="javascript:ajax_post(' . $name . ');">
</p>
<p>Your list has been succesfully created.</p>
<form name="form1" method="post" action="">
<input type="submit" name="AddItem" id="AddItem" value="Add Items">
</form>
<p><br />
<br />
</p>
<div id="status"></div>
</body>
</html>';
?>
<?php
echo $display;
?>
Echo the var into a hidden span or input an snag it from there when you need it.
It looks like you're sending the request via POST and then trying to access it via GET. Try changing it to:
<?php
$name = $_POST['name'];
?>