I am new to php, Please give me a help if anyone know how to execute an external JavaScript function from a function in another PHP file. (or using Ajax).
In my code (see the comment):
In index.php->
<script type="text/javascript">
function OnUnityReady()
{
u.getUnity().SendMessage("test", "SubmitInputValue", "name");
}
</script>
In chatApp.php ->
<?php
class pfcCommand
{
function write($chan, $nick, $cmd, $param)
{
$data .= $nick."\t";
$data .= $cmd."\t";
$data .= $param;
.
.
$this->setMeta("channelid-to-msg", $this->encode($chan), $msgid, $data);
//FROM HERE, I NEED TO EXECUTE OnUnityReady() FUNCTION in index.html
//IN EVERY TIME WHEN EXECUTE THIS write FUNCTION
return $msgid;
}
}
?>
You cannot execute a javascript function from within a php script. Especially since javascript is parsed by your browser, using file_get_contents() or cURL wouldn't work. Find a way to replicate the JavaScript function in PHP :/
Related
<?php
error_reporting(0);
session_start();
function code($no_of_char)
{
$code='';
$possible_char="0123456789";
while($no_of_char>0)
{
$code.=substr($possible_char, rand(0, strlen($possible_char)-1), 1);
$no_of_char--;
}
return $code;
}
function sendSms($msg, $to)
{
$to=trim($to);
$m.=urlencode($msg);
$smsurl="http://bhashsms.com/api/sendmsg.php?user=*****&pass=*****&sender=******&phone=$to&text=$m&priority=ndnd&stype=normal";
$return = exec($smsurl);
return $return;
}
?>
print_r($smsurl) is showing absolutely right command which i want but doesn't executing exec($smsurl)
i m not familiar with exec() function.
Thanks in advance.
exec is used for executing an external program but accessing some webpage/api from a web url is not a valid use case for exec. Try this if you want to get data from an Url.
how to call url of any other website in php
I'm working on a project where I'm keeping all HTML content separate from the rest of the PHP code. Each instance where any HTML needs to be parsed for PHP variables is sent through a function call. Most these deal with dynamic data from the database.
A simple example of a template file:
<div id='{$data['id']}'>{$data['text']}</div>
The variables in the $data array are passed through a function call where the HTML snippet needs to be added to the output buffer:
$output .= $html->load_template('template_id', array('id' => 123, 'text' => 'Testing'));
The html::load_template() function simply locates the correct text file, and is supposed to load the variables and return the string as HTML. This is where I'm having issues:
public static function load($template, $data=array()) {
ob_start();
include ( TEMPLATE . $template .'.tpl' );
ob_flush();
}
I've tried using include() and file_get_contents(), but to no avail - I'm looking for a simple solution where I can use the {$data['var']} syntax, preferably retaining the template HTML as a simple variable, so it can then be added to the output.
I'm trying to avoid using eval().
Can someone give me some guidance?
I've done the same thing in the past, you can modify your below code like this:
public static function load($template, $data=array()) {
ob_start();
include ( TEMPLATE . $template .'.tpl' );
$getData = ob_get_clean();
preg_match_all("|{([^>].*)}|U", $getData, $getDataArr, PREG_SET_ORDER);
if (is_array($getDataArr) && count($getDataArr) > 0) {
foreach ($getDataArr as $php) {
if (strpos($php[1],'$') !== false) {
$getData= str_replace($php[0], (eval('return $'.str_replace('$', '', $php[1]).';')), $getData);
}
}
}
echo $getData;
}
I am using xmlprc server in codeignter for web services . the flow of my application is that i need to pass parameters to the xmlrpc server method which then should invoke another controller class method which would set the parameters in a js function and that js method is invoked concurrently .
The problem i am facing is in calling the controller class method from the xmlrpc server method and getting the response to the server parent method which could then be fetched using xmlhttprequest.
my xmlrpc server method is:
function update_p($request) {
$parameters = $request->output_parameters();
$this->session->set_userdata(array("portfolio" =>$parameters['0']["portfolio"]));
$this->session->set_userdata(array("filter" =>$parameters['0']["filter"]));
$url = base_url("ControllerClass/update_p?".$parameters['0']["portfolio"].'&'.$parameters['0']["filter"]);
header("Location: $url");
$xml_rpc_rows=array("portfolio"=>$parameters['0']["portfolio"],"filter"=>$parameters['0']["filter"]);
$response = array(
$xml_rpc_rows,
'struct');
$this->xmlrpc->send_response($response);
}
Controller Class method:
public function update_p() {
$loginid = $this->session->userdata('loginid');
if(!isset($loginid)){
die;
}
error_reporting(E_ERROR);
if (time()>$this->session->userdata('expire')) { redirect("/dashboard/logout?expired=Y","location",401); die; }
$out='';
$request="USER ".$loginid.($this->session->userdata('isMobile')?"#mobile":"")."\n";
if(isset($_GET["portfolio"])) {
$portfolio=trim($_GET["portfolio"]);
$request.='ECHO "LISTP":'."\nLISTP0 #".$portfolio;
if(isset($_GET["filter"])) {
$filter=trim($_GET["filter"]);
$request.=" -".$filter;
}
if(isset($_GET["sort"])) {
$sort=trim($_GET["sort"]);
if ($sort>=1024) $request.=" -s".($sort&1023);
else $request.=" -S".$sort;
}
$ph = isset($_GET["first"]);
if ($ph) {
$this->load->model('Model');
$resultArray = $this->Model->getData($this->session->userdata('loginid'),$this->session->userdata('isMobile')?'mobile':'default','listp');
$request.=" ".$resultArray[0]['listp'];
}
$request.="\nECHO ,\n";
if(isset($_GET["watch"])) {
$portfolio=trim($_GET["watch"]);
if ($ph)
$resultArray = $this->Model->getData($this->session->userdata('loginid'),$this->session->userdata('isMobile')?'mobile':'default','watch');
$request.='ECHO "watchl":'."\nLISTP1 #".$portfolio." -WL ".($ph?$resultArray[0]['watch']:"")."\n";
$request.='ECHO ,"watchs":'."\nLISTP1 #".$portfolio." -WS\nECHO ,\n";
}
}
$request.="RISk\nECHO ,\nPnL\n";
if ($result=$this->getData($request."BYE\n")) {
if (result!='') $out=$result."\n";
}
ob_start('ob_gzhandler');
echo "{".$out."}";
ob_end_flush();
}
I can not figure out how to get the controller method result in the server method anyone who can shed some light on this would be much appreciated .
Thankyou.
Your controller method is expecting to output the result as an echo statement, which goes to the browser, rather than to return it in a variable. This means your server function is having to try to capture the output of that controller method. That setup is much more awkward and prone to error.
Unless you also need to access your update_p method directly from a browser you should change your Controller to simply return the output which really means this controller is more of a library and should probably go in the libraries folder. You will need to change your controller code a bit so that instead of grabbing the parameters from $_GET you are getting them as arguments, which in CodeIgniter is what you should be doing anyway.
So from the end of update_p just do this instead of your echo:
return "{".$out."}";
Then in your xmlrpc server do this:
$controller = new ControllerClass();
$result = $controller->update_p($parameters['0']["portfolio"], $parameters['0']["filter"]);
Then do whatever you want with your $result.
I am trying to setup an array that pulls the filename and function name to run, but it not fully working.
The code is
$actionArray = array(
'register' => array('Register.php', 'Register'),
);
if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) {
echo '<br><br>index<br><br>';
echo 'test';
exit;
}
require_once($actionArray[$_REQUEST['action']][0]);
return $actionArray[$_REQUEST['action']][1];
Register.php has
function Register()
{
echo 'register';
}
echo '<br>sdfdfsd<br>';
But it does not echo register and just sdfdfsd.
If I change the first lot of code from
return $actionArray[$_REQUEST['action']][1];
to
return Register();
It works, any ideas?
Thanks
Change the last line to:
return call_user_func($actionArray[$_REQUEST['action']][1]);
This uses the call_user_func function for more readable code and better portability. The following also should work (Only tested on PHP 5.4+)
return $actionArray[$_REQUEST['action']][1]();
It's almost the same as your code, but I'm actually invoking the function instead of returning the value of the array. Without the function invocation syntax () you're just asking PHP get to get the value of the variable (in this case, an array) and return it.
You'll find something usefull here:
How to call PHP function from string stored in a Variable
Call a function name stored in a string is what you want...
I need some help with this please
I can't get a handle on it.
The problem is that I want to call a class method, in this case with static methods with an ajax call.
I have put the helper class in the same folder as the script that is called by ajax for easy referencing and try to include it.
Could it be that my refencing is wrong?
If I make a testclass in the file that is called by ajax I can get a response.
class test {
public function testit() {
return "testit";
}
}
$t=new test;
$check= $t->testit();
switch($action) {
case "someaction":
$data = array();
$file='input_helper.php';
include_once $file;
$check= input_helper::ip_address();
header('Content-type: application/json');
$output = array(
"check" => $check,
"user" => $data
);
echo json_encode($output);
exit(0); // Stop script.
break;
//...
EDIT FOR MORE CLARIFICATION
The action is set as a post variable in the ajax function
The ajax url points to a script that takes some action based on the posted variables
thanks, Richard
<?php
include_once 'your/class/path/helper_class.php';
.
.
at the top of your PHP page should do it. it really has nothing to do with AJAX. If your PHP file is in fact being hit on the callback, then that should work properly.
Optionally, to test that your path is correct, if you do:
<?php
require 'your/class/path/helper_class.php';
.
.
If the path is not correct PHP will throw a fatal E_ERROR level error.