use ajax to call php to write timestamp to file - php

I'm trying to call a function in javascript to write to a text file. This function uses ajax to to run some code in a php file. All my code seems to be working fine, but I havent gotten any output (If the file doesn't exist, I want it to be created when the php code runs). Everything is being run locally and must be run in chrome. I've found some other people with similar questions, but I wasn't able to apply those solutions. I am very new so please be explicit. Also, if there is a better way to do this (get the system timestamp and write/append to a text file) I am more than open. I just need to record the timestamp whenever a particular javascript function is called. In addition, I would like to eventually pass a string from my running javascript to be saved with each timestamp (the string is determined by the running javascript). Note that running in "Data: running" is a dummy variable. Eventually this will be used to pass a string.
im using this script to use ajax (in html script)
script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"
javascript code..... call recordTone().... javascript code
function recordTone() {
$.ajax({
url: "file:///C:/Users/ryan/Desktop/New%20folder/saveTimeStamp.php",
type: "POST",
data: running,
success: function (msg) {
alert(msg);
}
});
}
saveTImeStamp1.php in entirety
<?php
$my_file = 'session1.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '$my_file);
$data = microtime();
fwrite($handle, $data);
fclose($handle);
echo "YES";
?>

You need a web server and then have a URL to it. Easy to install Apache and PHP Like "Ed Heal" said...
or you can use web service to call you procedure/function ...
function recordTone() {
$.ajax({
url: "localhost/yousite/saveTimeStamp.php",
type: "POST",
data: running,
success: function(msg){
alert(msg);
}
});
}
look at 'url', change to your local site...

You need a web server and then have a URL to it. Easy to install Apache and PHP
Not
file:///C:/Users/ryan/Desktop/New%20folder/saveTimeStamp.php
As this will not work

Install XAMPP (http://www.apachefriends.org/en/xampp-windows.html), and then reference your file relatively. If the html file is in C:/Users/ryan/Desktop/New%20folder/, just change the url to "saveTimeStamp.php".
Another option if you don't want to install your own webserver is to purchase some hosting online, from somewhere like http://www.arvixe.com/.

Related

Ajax call to PHP function not calling function (file is 404 not found)

I have a web page that the user can select a team and when they do, I want an image.src to change to reflect the new choice. When the user makes the change, the teamID is saved in a settings file. I can't get the AJAX call to access the PHP file or function.
When it makes the AJAX call it looks like it can't access the PHP file- I get an error
POST: http://192.168.1.104/.....functions.inc.php 404 not found - I tried every path I could think of to get to the functions.inc.php file but it didn't work.
Here is the Ajax code: (the functions.inc.php file is in the same directory as this file)
function updateLogo(){
<?error_log("In update logo "); ?>//test to see if it gets in the function
$.ajax({
url: 'functions.inc.php',
data: {action: 'getLogo'},
type: 'post',
success: function(output) {
alert(output);
}
});
alert (output);
document.getElementById('teamLogo').src = 'output';
}
Any suggestions?
Because the device is running its own PHP server and deamon, the correct url is a unique url to this device.
Thanks for the help

Problem with Cordova Apache Ajax request to PHP

\
I am new at this and I need some help. \
I want to create a Cordova project which is supposed to do this. On the main page there's a "Send"
button, when I click on it an ajax request should be sent to the the php file and it should return
"hello", and in the success function the result is alerted. Instead of that it alerts the whole PHP
file. This only happens when I run Cordova on browser with cmd. \
I tried to execute it like I would execute a php file and it worked, so I don't really understand what's
the problem here. \
Sorry for my bad English, but I hope that by looking at the photos you'll understand my problem.\
Help me Obi-Wan Kenobi, you're my only hope.\
alert.php
<?php echo json_encode("HELLO"); ?>
index.js
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
// Cordova is now initialized. Have fun!
console.log('Running cordova-' + cordova.platformId + '#' + cordova.version);
document.getElementById('deviceready').classList.add('ready');
}
function send(){
$.ajax({
type:"get",
url: "alert.php",
success: function(result){
alert(result);
}});
}
document.getElementById("send").addEventListener("click",send);
https://i.stack.imgur.com/Pwl5m.png
after accessing http://192.168.0.111/Project/www/alert.php
https://i.stack.imgur.com/q7BXK.png
The problem is that http://192.168.0.111/Project/www/alert.php will just return the source code of alert.php that happens because the web server installed on the host 192.168.0.111 is not configured properly to run PHP.
Depending on the server installed on 192.168.0.111 check how to configure PHP with it( your webserver might be Apache, Nginx, etc).
If you configure the webserver properly when you visit http://192.168.0.111/Project/www/alert.php in the browser you should see just HELLO.
Reason why your code works on a localhost is because your file extension ends with .php
In your case http://192.168.0.111/Project/www/alert.php
You can not execute PHP code if file extension ends with .html
That is why you get whole content of a alert.php instend of method output
I would suggest you to use JSON format to return content of a PHP file and then handle that JSON in success() callback. In this case your JS code does not need refactoring but for a future reference please check out https://www.w3schools.com/js/js_json_intro.asp
Your alert.php file should look something like this:
<?php
$txt = "HELLO";
$json = json_encode($txt);
echo $json;
?>

Dynamic jquery pages

I didn't really know what to call this so I couldn't find anything by searching. Pretty much I have the following:
$(document).on('click', 'a[data-ajax]', function(e) {
var box = $('#ajaxdata');
e.preventDefault();
var r = $(this).attr('href');
$.ajax({
type: 'post',
url: '/betasite/' + r,
success: function(data) {
box.html(data);
}
});
});
Pretty much it loads the contents of a file (being some html) into a div so the page doesn't need to reload data as often. The problem is having is that I want to have some php inside that loaded file so I can access server side MySQL data and display it. Whenever I try to do this however, it comments the php out like <--php (stuff) -->. Does anyone know how I can work around that (like having the php generate the page before its loaded)?
Your php code will be rendered by server(WAMP/MAMP) automatically if you are on a local environment. If you are not running a server(WAMP/MAMP) you will have to select one depending on the O/S you use.
a good practice is to test the operation of the service in this case "php", before trying the operation in the front-end (jquery). If you have an Apache server up should see the result of their service by accessing the service route.

PHP SCript Not Accepting AJAX Data

This is really bizarre. I am trying to submit data to the server and have a PHP script parse the data and then send back a response. A correctly formed URL is being sent:
http://localhost/ajax/test.html?row=rec_no_1
, but the server does not seem to respond with content from the PHP script despite a return code of 200. In fact, Developer Tools (in Google Chrome) doesn't say anything about the PHP file. The AJAX "data" statement must be formatted properly otherwise I wouldn't get the correct URL. POST (instead of GET) doesn't help.
If I change the AJAX data to a string, then it works fine. This implies there is something wrong with the AJAX data. But I can't understand what given that the URL is correctly formed and does change depending on which row I select.
Any ideas?
Here is the Javascript:
$(document).ready(function() {
$(".submit").click(function() {
$.ajax({
type: 'GET',
url: 'getTable.php',
dataType: 'html',
data: {row: $('input[type='checkbox']:checked').val()},
//data: {row: 'rec_no_2'},
success: function($result) {
$('.tableHolder').text($result);
}
});
return false;
});
});
Here is the PHP code:
<?php
if (isset($_GET['row'])) {
$tableRow = $_GET['row'];
echo $tableRow;
}
else
echo 'TEST';
?>
Your syntax is incorrect:
$('input[type='checkbox']:checked').val()
You should use double quotes around checkbox:
$('input[type="checkbox"]:checked').val()
Anyway do a console.log( $('input[type="checkbox"]:checked').val() ) before the ajax call just to find out which value is being sent.
Try moving your javascript file to server. Ajax don't work on cross domain. You are running the javascript file on localhost and trying to fetch information from live server. Keep both php and javascript file on same server, either live or localhost. It will work fine.
One more thing, try changing the url parameter of your ajax request. try complete url or try putting or not putting a slash / before your filename.

How to avoid retrieving literal content of PHP script when debugging?

I am developing an HTML/Javascript/JQuery page retrieving a JSON returned by a PHP script:
$.ajax({
url: "data.php",
dataType: 'json',
success: function(rate) {
var retr = JSON.stringify(rate, null, 2);
$("#data").text(retr);
},
error: function(request,error) {
alert(request.statusText);
}
});
When I view the HTML page from my local PC, the Ajax returns the PHP script code rather than the JSON the PHP code should generate. Somehow, it makes sense since it does not go through a PHP engine.
I have tried replacing url: "data.php" with url: "http://www.mysite.com/data.php" or url: "www.mysite.com/data.php", but it does not work.
I would like to avoid uploading my page on a server to test it. I would like to be able to invoke the PHP on my PHP server directly from my local PC. Is this possible?
you will have to upload the page on the server, otherwise you will be restricted by CORS
add the following line in your php script
<?php
header('Access-Control-Allow-Origin: *');
echo json_encode("hello");
?>
I think this post would help you solve your issue just go through the steps mentioned here.
http://php.about.com/od/troubleshooting/qt/shows_code.htm
Take care if the php file has properly set php tag

Categories