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?)
Related
I have some experience in SQL/basic web programming and wanted to branch out. After some help from other users on this site I decided to use JQuery to grab data from a PHP file and echo it to an AJAX request. (Right now I know it seems redundant to use AJAX for this, but my goal is to connect a database to the PHP file once I can hash out this issue).
I can connect to the file no problem, but instead of just loading in the echo I send out from the PHP file, AJAX loads the ENTIRE PHP file, which makes some weird HTML. Any advice on how to fix this? I've tried some tutorials and they all seem to have similar syntax.
PHP File Code:
<?php
echo "<p> HELLO HOW ARE YOU? </p>";
?>
JS/JQuery Code:
function incrementerJavascript() {
$.ajax({
url: "increment-counter.php",
success: function(result)
{
$("#counter").append(result);
}
});
};
Relevant screenshot from debugger:
\
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;
?>
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'm trying to call a function in a .js file from the php page where I'm displaying it!
What I want to do is something like this:
.php file:
...
function testFunc(){
return "2";
}
...
now I want to call this function inside the .js file!
Thaks in advance for your help
What you have tried?
Assuming the testFunc function is enclosed in proper script tags,
your .js file can call it quite simply like this:
testFunc()
Ideally, you include the .js file after testFunc is declared.
PHP runs server side. JavaScript (assuming you are using it in a browser) runs client side. If you want to execute this PHP function from the client side and get the result without reloading the page, you have to do an ajax request, which may be beyond your skills.
Look into jQuery Ajax:
http://api.jquery.com/jQuery.ajax/
I have js file where i need to call the function defined in the php file to calculate the value and then move on to next step. Is this possible to do? I am not able to access the function of the php file from my script. Also is there some way around to include php file in a html page without changing the extention .html
No, you cannot do that. PHP and Javascript do not execute in the same environment, nor at the same time.
PHP executes on YOUR web server (server-side) before the page is served up.
Javascript is executed on the CLIENT's browser after the page has been served up.
To call a PHP function from javascript, you will need to make an AJAX call. This sends a value from the client side to the server side where it can be processed, and the response is then sent back to the client.
Try JQuery Ajax
It´s easy to use.
$.ajax({
type: "POST",
url: "yourphpfile.php",
data: { values: "100"}
}).done(function( msg ) {
console.log("php file called"+msg);
});
replace yourphpfile.php with your php file and your php file gets executed
If you want to access server-side resources (filesystem writing, database), you'd need to use AJAX to switch back and forth.
If you're just more familiair with PHP, you could take a look at http://phpjs.org/, which reimplements many PHP functions in Javascript.
PHP files are files that are executed by the server.
"I have js file where i need to call the function defined in the php file to calculate the value." Browsers cannot run php functions, you need server to do that.
"Also is there some way around to include php file in a html page without changing the extention .html" If you do that your php code will not run, because server use the extensions to know if a file needs execution or not.
To call a PHP function from javascript, you will need AJAX. The value is send from the client to the server where it is processed, and the response is then sent back to the client.