passing a string variable from php to a JS function - php

I was looking for a way to pass a string(variable saved in a form of $x) from php to Java Script and I found so many codes to solve that, but my question is : does those strings have to be declared global?!
i did declare it as a global variable but still no response ..!
any other suggestions?!
Pass a PHP string to a JavaScript variable (and escape newlines)
I tried most of these codes, none of them worked,

As the others have said, all we can say is that you are doing something wrong. You can place PHP variable values, strings or otherwise, wherever you want in your JavaScript code, since PHP is server-side and can do whatever you like on the client side.

This will help you to solved the issue of passing string variable value by calling the javascript function within php scrpt. :)
<?php
$testStrFileName = "test.jpg";
$file_name = "<script>". $testStrFileName ."</script>";
//<sample tags/>
echo 'Delete';
?>

Related

Deleting part of string in php using str_replace

I have a little issue with deleting a part of string using php.
Let me get you a little bit into problem, I have vlariable "column1text" in which is text string endered into < textarea >, but that does not matter. I want to remove all HTML tags from this string since they are doing problems sometimes. For example: When this string contains < /body > or < /table > it will do a lot of unwanted mess. So I want to get these tags away.
I am using $column1text = str_replace("TEXT TO REMOVE", "", $column1text); and it works, but I want to make function for it (optionaly if you know easier way, just tell me, I'll be glad).
I am using this function:
function remove($removetext)
{
$column1text = str_replace($removetext, "", $column1text);
}
And I am using it like this:
remove("TEXT TO REMOVE");
What am I doing wrong? (I am sure it's something pretty silly, but I cannot find it!)
P.S. I am totally sorry about my English, it must sound stupid, but I had no other idea than asking you.
You can either pass in $column1text as reference, or have your function returns the modified text (which I prefer)
function remove($column1text, $removetext) {
return str_replace($removetext, "", $column1text);
}
$column1text = remove($column1text, '<span>');
You are passing by value, when you actually need to be passing by reference. If you don't know what the difference is I suggest reading up a little on it.
Link
edit:
In a nutshell, whenever you pass a function a parameter, that function makes a copy of whatever it was passed, so when that parameter is manipulated in the function, it manipulates the copy and not the passed object itself.
When a function has a parameter passed by reference, then it actually has access to the object that was passed itself. That means anything you do to manipulate that object also affects the object outside of the function (since its not just a copy of it you are working with).
You need to tell interpreter that you want to use global variable, because now it sees it as local variable (within your function). To do this you just need to use global keyword:
function remove($removetext) {
global $column1text;
$column1text = str_replace($removetext, "", $column1text);
}
you can read more about it here http://php.net/manual/en/language.variables.scope.php

Calling a javascript function on a php variable

Javascript:
function capitalizeFL(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
PHP:
echo "You have chosen a <script>document.write(capitalizeFL(".$race."));</script>";
$race contains a string. What I would like is simply to capitalize the first letter of the php variable $race, using the Javascript function above, and print it on the page.
I could find another way of doing this, but this JS-PHP mixing thing is confusing to me and I'd very much like to figure out WHY this doesn't work.
Look at the generated JavaScript.
document.write(capitalizeFL(value_of_race));
That's an identifier, not a string literal. You need to include quote marks in your generated JS.
Given a string, the json_encode function will output the equivalent JS literal (even if it isn't valid JSON). Use that to convert your PHP variables into JS literals.
$js_race = json_encode($race);
echo "You have chosen a <script>document.write(capitalizeFL($js_race));</script>";
echo "You have chosen a <script>document.write(capitalizeFL('".$race."'));</script>";
You can try above code.
Javascript string must be wrapped by ''.

Variables in PHP Array?

So i have the code below basically when its run it will display a graph. How can i make the variables inside the arrays work the variable works and when echoed will give a number but for some reason it doesn't input number there. $mar1 in [here]
$lineChart = new gLineChart($_GET['width'],$_GET['height']);
[here]$lineChart->addDataSet(array($mar1,315,66,40));[/here]
$lineChart->setLegend(array("first"));
$lineChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
$lineChart->setVisibleAxes(array('x','y'));
$lineChart->setDataRange(30,400);
$lineChart->addAxisLabel(0, array("This", "axis", "has", "labels!"));
$lineChart->addAxisRange(1, 30, 400);
$lineChart->setGridLines(0, 15);
$lineChart->renderImage();
This is a very, very basic question about PHP syntax. Arrays can and frequently are used with variable data.
There isn't anything wrong with the syntax of the code that you posted, so chances are that this is a case of the $mar1 variable not being defined or not containing the data that you're expecting. You probably want to echo or var_dump this variable and see what's in it and work backwards from there.
If $mar1 doesn't contain what you expect, look in the code above that line and see if its value is being set. If this is being passed in the browser's query string like the $_GET['width'] and $_GET['height'] variables are, you would need to access it as $_GET['mar1'] instead of just $mar1.
If this file is being included from another file or includes/requires other files, it could also be defined in the including file(s).
If $mar1 does contain the value you're expecting, then check the documentation for the gLineChart class and make sure that you're passing it all the correct parameters.

Calling a javascript function while passing PHP variables

I am trying use a javascript function while passing php variables in it. For example:
onclick="alert(<?echo $row['username']?>)";
Now this does not work, instead gives an error-ReferenceError: Can't find variable:right_username(here the right_username is the answer i expect in the alert).
However if instead of username i use EmpID:
onclick="alert(<?echo $row['EmpID']?>)";
EmpID being an int in the database works just fine.
Because $row['username'] is a string, you need quote it, or the javascript will think it as a variable.
$row['EmpID'] is a number, so it shows.
onclick="alert('<?echo $row['username']?>')";
You forgot your quotes:
onclick="alert('<?echo $row['username']?>')"

Is it possible to include PHP code that is in memory?

Say I have a variable containing PHP code, can I include its content as if it was a normal PHP file ?
For example, the PHP code could be a class declaration.
You don't have a variable containing php code. You have a string.
You can execute a string as php with the evil eval function, but puppies AND kittens will die!
eval($your_variable);
Be aware about security holes!This is very dangerous and should NOT be based on user's input !
You could use eval to evaluate any code that you have in your string, however it is evil. What exactly are you trying to do?

Categories