Javascript function argument from HTML/PHP - php

my question is really basic but I really can't figure out how to do it.
Basically I have a javascript function into an HTML document that changes my uploading parameters as follows:
function change(){
upload.url ( 'upload.php?country=COUNTRYISOSTRING' );
}
Now, basing on a selection made by the user through PHP or HTML function (such as IP country detection or simple HTML dropdown menu), I would like to change the COUNTRYISOSTRING passing it to the function instead of writing different functions for each choice, so it would become
function change($countrycode) {
upload.url ( 'upload.php?country=$countrycode' );
}
But it's not working...What am I doing wrong?

I don't really understand what you are exactly trying to do but something like this should definitively work:
function change(country){
upload.url ( 'upload.php?country=' + country);
}

Edit: misunderstood the question. In JavaScript variables don't need the dollar sign, and are not evaluated inside strings; you need to concatenate the parameter like this:
function change(countrycode) {
upload.url ( 'upload.php?country=' + countrycode );
}

Better to pass your php variable as a parameter to the function call:
function change(countryCode) {
upload.url('upload.php?country=' + countrycode);
}
And call this function like:
change('<?php echo $countryCode; ?>')

Related

How to solve this almost easy create_function for PHP?

How do I convert another create_function. The one below
return create_function('$f,$e=null', "return ($parsed_tpl);");
to
$e=null;
return function($f,$e) { return ($parsed_tpl); };
or
return function($f,$e=null;) { return ($parsed_tpl); };
But neither of them are working.
I have tried everything above.
The key part to notice on the original code is this:
"return ($parsed_tpl);"
Note the double-quotes, meaning the variable will be expanded in the string before passing it to create_function. Then the expanded form will form the body of the function.
In other words, the actual code of the created function will be completely different every time - it won't just return the string, it will execute it as PHP code.
To achieve the same effect in current PHP, you need to use the eval function:
return eval("return ($parsed_tpl);");
You also have two mistakes in your attempt:
$e=null isn't an assignment, it's a default value for the parameter; it doesn't need a semicolon
You have to "capture" the $parsed_tpl variable with the use keyword to use it inside the function
The format is this:
$my_anonymous_function =
function($some_param, $next_param='default value', $another='another default')
use ($something_captured, $something_else) {
/* body of function using those 5 variables*/
};

PHP Text to display is interpreted as part of array

I have this code:
return t("Use tokens like: eg. [youtube_video:id]");
Because of the brackets used in my string PHP treat this [youtube_video:id] as an array key and returns notice like: Notice: Use of undefined constant _miscellaneous_filter_tips - assumed '_miscellaneous_filter_tips' w miscellaneous_filter_info_alter()
How can I resolve it?
All the code after a request:
function _miscellaneous_filter_tips() {
return t('Use tokens like: eg. [yamandi:youtube_video:id]');
}
function miscellaneous_filter_info_alter(&$info) {
$info['filter_tokens']['tips callback'] = _miscellaneous_filter_tips;
}
since I don't have the precursor code I can't actually see what the function t() does, however if is seems to think you are calling a variable try using
return t('Use tokens like: eg. [youtube_video:id]');
or if you still want to use variables
return t("Use tokens like: eg. " . '[youtube_video:id]');
Just change the double quote into a single quote:
return t('Use tokens like: eg. [youtube_video:id]');
EDIT
After looking to the updated code, this might be a completely different issue, I think you might wanna try this way of storing function hooks:
function _miscellaneous_filter_tips() {
return t('Use tokens like: eg. [yamandi:youtube_video:id]');
}
function miscellaneous_filter_info_alter(&$info) {
$info['filter_tokens']['tips callback'] = '_miscellaneous_filter_tips';
}
And then when you want to call that function, you can simply use:
$info['filter_tokens']['tips callback']();

$_POST as a function parameter [duplicate]

This question already has answers here:
passing POST array to php function
(3 answers)
Closed 9 years ago.
I've been working on a project for a game (so you'll understand that I can't post the actual script), and I'm cleaning up the code and placing code into functions instead of just after the
if ($_POST['name'] { //code }
Is this the correct method of doing what I'm trying? (I tried, but it broke my system)
if ($_POST['action'] == "actionName") actionName($_POST); //calls function if the $_POST is present
function actionName($_POST) { //code }
Thanks in advance for correct answers!
$_POST is globally accessible. So you don't have to pass to your function:
if ($_POST['action'] == 'actionName') actionName();
function actionName() {
//code using $_POST
}
If you want your function to be able to do what it wants with any array (i.e. other than $_POST), then make the function take a parameter, but don't call it $_POST:
if ($_POST['action'] == 'actionName') actionName($_POST);
function actionName(parameters) {
//code using parameters
}
$_POST is a super-global. All PHP code, no matter the scope, automatically has access to this data structure.
Therefore, the following is fine:
if ($_POST['action'] == "actionName") {
actionName();
}
and then
function actionName() {
print_r($_POST);
// code
}
$_POST is already a superglobal variable so you do not need to pass it to a function. If the function needs the content of $_POST then make a copy and pass it to the function.
However, from your example, it seems to me that you need to pass $_POST['action'] to your function instead of $_POST.
Well in your example you need to have quotes around your string variables:
//calls function if the $_POST is present
if ($_POST['action'] == 'actionName') actionName($_POST);
function actionName($_POST) { /*code*/ }
Although $_POST is super-global, yes of course you can do that and is actually a good idea. This way you decouple your code and maybe you can reuse actionName in another part of the application.
However, don't name the parameter of the function $_POST (this is more confusing than helpful).
To avoid a bunch of if statements for every possible action, have a look at call_user_func [docs].
E.g. you could do:
$allowed_actions = array_fill_keys(array('actionName', ...), true);
if(isset($allowed_actions[$_POST['action']])) {
call_user_func($_POST['action'], $_POST);
}
You should move the function before the code that executes it along with what everyone else here is saying about the $_POST array. Try this if you want
function actionName($input) { // code and return }
if (isset($_POST['action'])) {
$action = $_POST['action'];
if ($action == 'actionName') {
$name = actionName($action);
}
}
$_POST is a super global (as other have mentioned), so you don't need to pass it in.
You can pass it in so that you decouple the function code from the environment in which it's running. For example, should you wish to initiate such an action as a result of something else in the code, you can pass in an array with the same key/values that $_POST would have to do so
If you do pass in $_POST to a function, name the function argument something else, hopefully something that indicates what the data represents
Consider that the function may not need all the key/values that $_POST normally contains, and if you might be better off defining a function that takes only the arguments it needs in order to function
If you are talking about dynamically calling a function, you could achieve it this way:
/*
Example Post
$_POST['functionName'] = 'doSomething';
$_POST['value'] = 'woot!';
*/
function doSomething($message) {
echo $message;
}
// Call the function if it exists
if (function_exists($_POST['functionName']) {
call_user_func($_POST['functionName'], $_POST['value']);
}
// The above would output "Woot!"

PHP: Use function outside current function

can I use a function that is outside the current function?
eg.
function one($test){
return 1;
}
function two($id){
one($id);
}
Seems like i cant, how should I do it then to use the function that are outside? Thanks
The function is in the same file.. /
Is your function inside of a class? In that case you have to use $this->function() instead of function().
That's perfectly valid. Check out the running code here.
Your code looks valid to me : you are declaring two functions, called one and two ; and two is calling one.
Then, you can call any of those functions, to execute it.
For example, if you execute the following portion of code :
function one($test){
var_dump(__FUNCTION__);
return 1;
}
function two($id){
var_dump(__FUNCTION__);
one($id);
}
two('plop');
Note that I called two, in the last line of this example.
You'll get this kind of output :
string 'two' (length=3)
string 'one' (length=3)
Which shows that both functions were executed.
That works fine. However, one ignores its parameter. Then, two ignores the return value from one.
This should work fine
Example:
<?php
function test ($asd)
{
return $asd;
}
function run ()
{
return test('dd');
}
echo run();
?>
Maybe you have an issue elsewhere?

How to assign values into global array inside a function? (php)

I have a function that searches for a string inside a text file. I want to use the same function to assign all lines to an array in case I am going to replace that string. So I will read the input file only once.
I have the search function working but I do not know how to deal with the array thing.
the code is something like that (I made the code sample much simpler,so please ignore the search function that actually isn't below)
function read_ini($config_file_name,$string){
$config_file = file($config_file_name);
foreach($config_file as $line) {
return_string = trim(substr($line,0,15));
$some_global_array = $line'
}
}
echo read_ini('config.ini','database.db')
if ($replaced) {file_put_contents('config.ini', $some_global_array);}
http://php.net/parse_ini_file
I know it doesn't answer the question, but it quite possibly removes the need for even having to ask.
The deal with globals, though, is that they must be defined at the top of the function as globals, or else they're considered part of the function's scope.
function write_global() {
global $foo;
$foo = 'bar';
}
write_global();
echo $foo; // bar

Categories