I have a php page(calendar.php) which is using javascript to load a php calendar(calendar_start.php) and display the calendar in the center of the page.
I am trying to obtain a variable on the calendar_start.php page, however I cannot access variable which I am able to access from the calendar.php page.
below is one of the scripts used on calendar.php to generate canendar_start.php:
<script language="javaScript" type="text/javascript">
function initialCalendar(){
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
showmonth = month;
showyear = year;
var vars = "showmonth="+showmonth+"&showyear="+showyear;
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("showCalendar").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar").innerHTML = "processing...";
}
</script>
and below is part of the page calendar.php where I am generating the calendar:
<body onLoad="initialCalendar(<?php $_GET['page']; ?>);">
I am unable to grab the variable on calendar_start.php and I would really appreciate any help.
Kindest Regards,
Ryan
EDIT::
This may be helpful, this is the error when I am trying to display the variable on calendar_start.php
Notice: Undefined index: page in C:\wamp\www\project\calendar_start.php
Change $_GET['page'] to $_REQUEST['page'].
$_REQUEST combines $_GET and $_POST together (with $_POST taking precedence). That way you won't have to worry about backward comaptibility if you change it in the future.
It is, however, a bad idea to echo ANY of these variable unsanitized to users browser.
EDIT:
Although your main problem is that you forgot the "echo" command in front of the variable.
Related
Can someone tell me why the div called tellme is showing only showing something for half a second.. or less and then just doing nothing? I have tried everything and just can't get the code to work. It is funny because I have the exact same code on my other page and it works with no problems what so ever, I am trying to call an AJAX call and it is just not working and its not making the div called tellme anything, it's just keeping it blank...
<script type="text/javascript">
function fireWorkerFixed(userid)
{
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "http://private.com/fire_worker.php";
var id = userid;
var vars = "userid="+id;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById('tellme').innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById('tellme').innerHTML = "<div class='alert alert-info'>Please wait, Processing request...</div>";
}
</script>
I'm new to PHP, but I'm developing in Visual Studio LightSwitch.
I'd like to pass several javascript variables from a data record to a PHP script during a javascript save event.
What are my options?
Your options are limited to ajax, or an image request.
Option 1. AJAX
function success() {
console.log(this.responseText);
}
var ajax = new XMLHttpRequest();
ajax.onload = success;
// myVars is a string containing the values you need to record. In the format
// of ?var=value&var1=value&var2=value
ajax.open("get", "/record.php?" + myVars, true);
ajax.send();
Option 2. Image
var a = document.getElementById('my-button');
a.addEventListener('click', function(e) {
var target = e.target || e.srcElement;
var img = new Image();
// get your variables values and create a url here.
img.src = 'http://myserver.tld/record.php?' + myVars;
});
I have a php page which is using javascript to produce a calendar on the page. I am trying to pass a variable so that I can reference it on the loaded page.
Below is the script I am using to generate the calendar(along with several others)
<script language="javaScript" type="text/javascript">
function initialCalendar(){
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
showmonth = month;
showyear = year;
var vars = "showmonth="+showmonth+"&showyear="+showyear;
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("showCalendar").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar").innerHTML = "processing...";
}
</script>
and below is the way I am displaying this onto the php page:
<body onLoad="initialCalendar();">
I have tried using '<?php echo $page; ?>' and '<?php $_POST['$page'] ?>' in the brackets of initial calendar()
however I am having no luck, I hope I have explained correctly and in enough detail to make sence, however if you need anything more please just comment
Thanks,
Ryan
I don't know the layout of your $_POST array, but if I understood the question right you should try:
<body onLoad="initialCalendar('<?php echo $_POST['page'];?>');">
and change your function declaration to:
function initialCalendar(page){
Note that I've used single quotes ' around the value which PHP outputs. This to ensure that it didn't break anything if the value is a string.
Since you are passing variable through parameters like
localhost/project/calendar.php?page=something
use $_GET['page'] rather than the $_POST
I have made a simple chat script using jquery and AJAX but the huge hole in it is that i can only chat with myself properly :P
heres the javascript for my main file
<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#status").load('ajaxLoad.php');
$("#userArea").submit(function(){
var hr = new XMLHttpRequest();
var id = '<?php echo $id; ?>';
var name = '<?php echo $name; ?>';
var url = "ajaxPost.php";
var msg = document.getElementById("messages").value;
var vars = "messages="+msg+"&id="+id+"&name="+name;
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;
$("#status").append(return_data);
}
}
hr.send(vars);
return false;});
});
and heres my 2nd file "ajaxLoad.php"
<?php
include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT messages,id FROM chat");
while($row = mysql_fetch_array($sql))
{
echo $row["messages"];
}
?>
Now what i need is to continually refresh "ajaxLoad.php" so that users can chat in realtime without refreshing the page...any way around this?
// reload every second
setInterval(function() {
$("#status").load('ajaxLoad.php');
}, 1000);
Be careful of making too requests to the server. It's going to be rather intensive.
You can use javascript setInterval to call your php script every x seconds.
https://developer.mozilla.org/en/window.setInterval
If you want to get bleeding edge, you should look into WebSockets.
I have the following code generating content on my site:
<div class="content" id="links">
<?php displayTitle("Links"); ?>
<?php displayContent("Links", $isLoggedIn); ?>
</div>
The content has a button that calls a Javascript function 'addLink()' to edit itself. Here is the Javascript with an Ajax call to change the content:
function addLink(){
var ajaxRequest; // The variable that makes Ajax possible!
if(window.XMLHttpRequest){
ajaxRequest = new XMLHttpRequest();
}
else{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
ajaxRequest.onreadystatechange = function(){
alert(ajaxRequest.readyState);
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('links');
ajaxDisplay.innerHTML = "<?php displayTitle('Links'); ?><?php displayContent('Links', $isLoggedIn); ?>"
}
}
var imgURL = document.getElementById('links_img').value;
var linkURL = document.getElementById('links_link').value;
var queryString = "?imgURL=" + imgURL + "&linkURL=" + linkURL;
ajaxRequest.open("GET", "addLink.php" + queryString, true);
ajaxRequest.send(null);
}
'addLink.php' adds things to a table, theoretically allowing the content function 'displayContent()' to show the new entries in the table ('displayContent()' queries a table).
The PHP call works fine, but I have to refresh the page to see the changes.
Is there some problem with how I am doing this? Possibly because there are already PHP calls in the inner HTML when the page is loaded in the first place?
Any help is appreciated, I'm a bit of a beginner with Ajax.
ajaxRequest.onreadystatechange = function(){
alert(ajaxRequest.readyState);
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('links');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
Sorry a little correction. You are attempting to access the pre-ajax php scripts inside ajax callback function. That's not how it works. You want the data that is retrived after-ajax call. Ajax retrives the output from the GET request, and store in ajaxRequest.responseText. Try replacing that and then see what you get.
In your addLink.php you should add the link and then echo out the data that you wish to display as the responseText. What had happened is that you inject the data VIA addLink but you never actually display it on the client side VIA ajax correctly. However, when you refresh the page, the script retrives what has been injected and display it accordingly.