\
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;
?>
Related
I try to execute a simple PHP file and get this return or echo, it is possible with Angular 8 ?
I well build my Angular project with ng build and put it in Nginx server with PHP configuration
My PHP file don't have a spell mistake error, and I put it inside the make-bulletin inside asset folder.
Actually I made a service sending a POST request in my PHP file locally
I also try with, return this.http.post("assets/make-bulletin/test.php"... but it happen the same thing
My service:
public makeBulletin(ville: string, pollution: string, date: string) {
return this.http.request("POST", "assets/make-bulletin/test.php", {
params: {
Ville: ville,
Pollution: pollution,
Date: date,
},
})
}
My .ts for call my service and subscribe:
public onSubmit() {
this.bs.makeBulletin("Lyon", "Atmospherique", "2019-06-01").subscribe(
resp => {
// How print PDF to the user after get the PDF ?
},
error => {
// get error
}
)
My PHP file:
<?php
$ville = $_POST["Ville"];
echo $ville;
return $ville;
?>
I tried to add:
header("Content-type: text/html") and header("Access-Control-Allow-Origin: *")
But I get a 200 OK so good... but, with a null response in my resp...
How can I get/read a answer from my PHP file ?
Thanks in advance
You seem to be confused about the difference between server-side code and client (in-browser) code.
Your PHP code is intended to be run on a server. It cannot be executed in a web browser.
An Angular application (after being compiled to JavaScript) is run in the user's browser.
So, it makes no sense to embed PHP code in you Angular application, because the browser cannot run it.
Your server-side PHP code and you client-side Angular code need to be two separate applications. That said, they can be deployed side-by-side on the server, with Nginx both fronting your PHP application and serving the Angular application to client's browsers.
A little bit too late but, Angular is expecting to get a JSON from PHP, so in your PHP you need to do an "echo json_encode($ville);"
public onSubmit() {
this.bs.makeBulletin("Lyon", "Atmospherique", "2019-06-01").subscribe({
next: (data: any) => {
console.log(data);
},
error: err => console.error(err)
});
Now you can do what you want with "data" wich contains anything you echoed from 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/.
I have a simple jQuery post function that calls PHP script in order to return value from the database.
In Firebug I see that the PHP file is being called with 200 OK status, however the success function in JS is not being called.
To test the problem I have changed the PHP to only echo a simple string, but it doesn't work as well.
When I view the PHP file directly in the browser I do see the echoed string.
Here is the JS code:
$.post(PATH + "load.php", { id: _id },
function (data) {
console.log("LOADED " + data);
});
And here is the simple PHP code:
<?
echo "bla bla bla"
?>
I don't know what the problem is.
My HTML+JS file is local and it calls an online PHP file. Maybe this is the reason?
Any help will be appreciated.
My HTML+JS file is local and it calls an online PHP file. Maybe this is the reason?
Answer: Yes
Reason: Same origin policy rule
http://en.wikipedia.org/wiki/Same_origin_policy
Grant Thomas suggested a possible workaround: you can delegate the call to a method on same-origin server which calls external resource.
I am making an ajax call to a php file, But I dont want it to return anything.
My php file has HTML content or rather a JS code, which needs to be executed over there itself. Please help me with this. I dont Know why this is happening
This is my ajax code:
$.ajax({
type : "POST",
url : "<?php echo CALLBACKURL; ?>mobile_profile.php",
data : data,
success: function(response){
}
});
I am not giving any console.log but syill in the response its giving the whole of html
code.
I need to execute that js code there itself and I dont want anything as response Or if we cant do this atleast can i run the JS code return nothing
Where do you want your code to execute? In the browser or at the server?
If you do not want it to contact server and run there itself(inside browser), why are you using AJAX? You simply call a function which will do whatever you want.
on success what do you expect it to do? There is nothing inside the braces...
success: function(response){
}
Browsers cannot process php code. they can process only html and javascripts and some third party scripts with the help of add-ons.
Kindly be clear what you are expecting.
url: "<?php echo CALLBACKURL; ?>mobile_profile.php"
replace it with:
url: "mobile_profile.php"
and put those php tags inside that mobile_profile.php, if required.
If you wish, you can pass parameters to that php file by suffixing your? and a querystring so that the called php file understands what to do.
I think this helps.
i have been learning about javascript and using dojo. I am trying to execute a php file with dojo. My code is
dojo.xhrGet({
url: 'helloworld.php',
load: testCallback,
error: testError,
content: {name: dojo.byId('name').value}
});
for the dojo function. The php file is basically a simple script that prints the value of whats being passed in through xhrGet
<?php
header('Content-type: text/plain');
print "Hello {$_GET['name']}\n";
?>
When I call this function, I get the php file displayed as text. My testCallback function is simply
function testCallback(data, ioArgs)
{
alert("in testCallback");
alert(data);
}
I cant think why this wont work as it was pulled from the dojo tutorial itself. I tested php with a file with phpinfo() in it, and it was working. Does php have to be configured to 'work' with certain ports?
If you get your php file back as text your webserver is not setup to call php to handle the file. It is as simple as that.
Have you named it .php or something else (judging from the post it looks like it is called helloworld.php and in that case I wonder how your phpinfo() call could work, was it the same server?)