How to run external php script using phonegap? - javascript

I am trying to connect to a remote server and basically run my application off files on that server. I am able to connect to the server but I don't know how to execute the php files on that server. Here's my javascript function on my local directory which connects to the server:
function test2(){
$.getJSON("http://sampleserver.com/check.php?var=test&callback=?", {
success:function(data){
alert("Working"); },
error: function() {
alert("Error");
}
});
}
This alerts "Working". Here's my check.php on the remote server:
<?php
echo "hello";
echo $_GET['jsoncallback'];
?>
Why is hello not being printed to the screen? How would I actually execute this script once I am connected?

Here is just one example:
var url = '/m.profile_msg/0/send/';
var dataString = 'msg_body='+ msg_body;
$.ajax({
type: "POST",
url: url,
data: dataString,
success: function(data) {
try {
//var data = jQuery.parseJSON(data);
alert(data);
} catch(e) {
alert(data);
}
}
});
And on the PHP side:
<?php
$xxarray = array('key' => 'value');
echo json_encode($xxarray);
?>

Your script IS already executing, but you're doing an AJAX call and simply alerting with your own message, you're not doing anything with the response, which would contain your hello text.

To run remote scripts - try doing your ajax call to a PHP script and have the PHP script shell execute something. But I wouldn't wait for the reply - rather return a status code back to the mobile device saying that the script is running.

Related

Call Python-script or PHP-script with JavaScript by Button-Click (RasperryPi)

I have a web interface on my raspberry pi. Threfore I'm trying to call a PHP script by clicking a button in JS. The function for the button is executed, but nothing (no success and no error function) is executed.
My function in index.html:
function performPhpScript() {
$.ajax({
type: "POST",
async: false,
url: "http://192.168.178.99/open.php",
success: function () {
alert("Test1");
},
error: function (requestObject, error, errorThrown) {
alert("Test2");
}
});
}
My open.php:
<?php
echo shell_exec("python /home/pi/Smoker/Autostart.py");
?>
The php script works fine, I tested it with following Python script:
import subprocess
proc = subprocess.Popen("php open.php", shell = True, stdout = subprocess.PIPE)
script_response = proc.stdout.read()
Any ideas why nothing happens?
My initial aim was to execute a certain python script.. On the internet I found the possibility to perform it with an php script..

Ajax Php get file from server, how to detect the file is open

There is this case.
I have many files(data) in the server. I use Ajax and php for Client browser to get the data from the server.
When a person opens a website and accesses a file from the server, at the same time, if another person(user) open this website, I want to find a way to detect that file is open, if so, I can load a different file or just block this user.
How can I do this, any suggestion is appreciated, thanks for your time!
Ajax get the file
load: function (url) {
var _this = this;
$.ajax({
type: "GET",
url: url,
dataType: "text",
success: function (json) {
if (typeof json == 'string' || json instanceof String) {
json = JSON.parse(json);
_this.parse(json);
}
}
});
},
You can use global variables:
<?php
if($_GLOBAL["locked"]) {
header("HTTP/1.0 503 Service Unavailable");
exit(0);
} else {
$_GLOBAL["locked"]=true;
//send file
$_GLOBAL["locked"]=false;
exit(0);
}
?>
The variable 'locked' will persist throughout all of the php files.

Getting JSON data from a PHP script using AJAX

My PHP script, 'getNews.php', (which works when I run it in the terminal and returns the correct data) is as follows:
<?php
header('Content-Type: application/json');
$db = new PDO('mysql:host=hostname;dbname=table', 'name', 'password');
$sql = "SELECT * from `news` ORDER BY date";
$result = $db->query($sql);
echo json_encode($result->fetchAll(PDO::FETCH_ASSOC));
?>
In my Javascript (I have JQuery loaded), I am attempting to pull this data in the following manner:
$(document).ready(function() {
$.getJSON("getNews.php", function(data) {
console.log(data);
});
});
which does nothing. When I change it to:
$(document).ready(function() {
$.get("getNews.php", function(data) {
console.log(data);
});
});
It writes the entire text of the php script to the console. Basically, it doesn't seem to be executing the php script or retrieving the json object at all. Thoughts?
Do you have a web server (like apache or nginx) installed? "It writes the entire text of the php script to the console." = You aren't invoking a PHP processor. Are you loading it like file:///something, or via http://something?
If you are using HTTP, make sure your web server knows to process PHP files. This usually involves editing a .ini or .cfg file; I can't tell which, because you don't mention what web server you are using.
It writes the entire text of the php script to the console
It could be that your server doesnt have php installed/configured. If thats not the case then see if this works for you
function prsJSN() {
$.ajax({
type: "get",
url: "getNews.php",
success: function(json) {
var dataArray = jQuery.parseJSON(json);
},
error: function(request, status, error) {
alert(request.responseText);
}
});
}

How to make Request.JSON work cross-domain (Mootools)?

I am executing an index.html file from the server test1.com. Mootools library file is included in this index.html file.
Following is a script that calls a PHP page:
<script>
var request = new Request.JSON({
url: 'http://test1.com/ajaxtest.php',
onSuccess: function(data) {
// the request was completed.
alert(JSON.stringify(data));
}
}).send();
</script>
ajaxtest.php
<?php
$arr['age'] = 30;
$arr['place'] = 'London';
echo json_encode($arr); exit;
?>
While executing index.html, I'm getting the correct output"
{"age":30,"place":"London"}
Now, ajaxtest.php is residing on another server, say test2.com. How to change the above script to make it work as earlier?
Not sure if this will be helpful to you now.
You need to use the Request.JSONP Class object to make cross site request:
new Request.JSONP({
url: "http://search.twitter.com/search.json",
data: {
q: "Arsenal"
},
onComplete: function(tweets) {
// Log the result to console for inspection
console.info("Twitter returned: ",tweets);
}
}).send();

Ajax in Codeigniter doesn't work for a specific controller in remote host

My web application does Ajax request to a Codeigniter-php code in a remote server. It works in localhost but not with a specific controller in remote host. It is strange because works in localhost for both controllers.
The request:
$.ajax({
async:true,
type: "POST",
dataType: "json",
url:"/CI/site/index.php/contact/submitContact",
data: "", //data example
success:arrived,
error:problems });
function arrived(data){
var dataJson = eval(data);
}
function problems(){
$("#result").text('Problems.');
}
I check the arrived with log_message. With the next function works fine:
function submitContact(){
log_message('error', 'submitContact. ');
//If data are received
if($_POST){
log_message('error', 'data. [application/controllers/contact.php]');
}
}
However, If I change the request to url:"/CI/site/index.php/control/controlHome", there isn't any log message and the output is the next:
POST http://www.page.com/CI/site/index.php/control/controlHome 500 (Internal Server Error)
The function /application/controllers/control.php is the next:
function controlHome(){
log_message('error', 'controlHome. [application/controllers/control.php]');
//If data are received
if($_POST){
log_message('error', 'data. [application/controllers/control.php]');
}
}
Also I've tried with complete url in the ajax code but the result is the same. Any setting is required?
Check this AJAX csrf protection with codeigniter 2. This solve my same problem
http://aymsystems.com/ajax-csrf-protection-codeigniter-20
UPDATE:
I checked your control.php file on my test server.
if($_POST) { /* i only commented gangway library functions */
} else { /* only replace the load->view with an print_r $data; and its work */ }
And put to comment the gangway library on construct. And control/controlHome works normaly without any error. Check your gangway library THAT's cause error 500.

Categories

Resources