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']?>')"
Related
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 ''.
I have a very simple question. But is really making me crazy.
I have a statement say:
example and example with one php variable like $loggedin_user_name
First of all, I want to store the above sentence in MySQL database and then take it back whenever I want to print the above statement. It seems that their is no issue.
But when I tried to print data after extracting from database it is printing the same statement. But i guess, it has to print the logged in user name instead of $loggedin_user_name in the above statement.
So, is it possible to print the variable within the variable? If yes, please suggest a way.
use sprintf()
$str = "example and example with one php variable like %s";
Then load it from database and fill
$out = sprintf($str, $loggedin_user_name);
If it is always the same variable name, I would suggest using
echo str_replace($fromDb, '$variableToReplace', $variableToReplace);
You can use preg_match to find you variable name in string and then replace it with str_replace.
$name = "ABC";
$bla = "$name";
echo $bla; //ABC
Will always be "ABC", because PHP is evaluating your variable when asigning to $bla.
You can use single-quotes to avoid that behaviour (like $bla='$name'; //$name) or you quote the $-sign (like $bla="\$name"; //$name). Then you can store your string like you wanted into your database.
But you can not (only when using eval(), wich you MUST NOT DO in good PHP-Code) build this behaviour, that php has, when printing fulltext.
Like Mentioned in another answer, you should use printf or sprintf and replace the $loggedin_user_name with %s (for "string).
Best would be to concatinate a string:
$exampleWithUsername = 'example' . $loggedin_user_name;
echo $exampleWithUsername;
'example' is a hardcoded string, but you can give it a variable containing string $example, or directly concatinate $username into $example.
You can use eval function, it can be used like your example:
$loggedin_user_name = 'bilal';
$str = "example and example with one php variable like $loggedin_user_name";
eval("\$str = \"$str\";");
echo $str;
Cons:
If your str variable or string/code which you give to eval as a parameter is filled by users, this usage creates a vulnerability.
In case of a fatal error in the evaluated code, the whole script exits.
I need to include two variables inside the brackets below, whats the correct way to do this please?
//Needs two variables stated
listDebug();
The proper syntax for passing 2 variables to a function in PHP looks like this:
//Needs two variables stated
listDebug($var1, $var2);
Edit:
Judging by the comments, it looks like listDebug() is a library function that only accepts one argument and that you cannot edit. It logs information to a database and its single argument represents the string to be logged. In this case, you want to log the value of 2 separate values. The most straight forward approach would be simply concatenate them together like so:
listDebug($var1 . ' ' . $var2);
Is this what you are looking for?
listDebug($variable1,$variable2);
Will need more information if you are looking for more.
Seems like listDebug function implementation hasn't been written to accept more than one argument.
So you just cannot pass 2 arguments there. Call the function twice... or rewrite the function to respect second argument.
To get this to work instead I did listDebug($var1.$var2); Thanks for your replies and advice.
{$row['info']}
How do I use stripslashes() php function on this?
I've tried :
stripslashes({$row['info']}), doesnt work and this: {stripslashes($row['info'])}
Neither work.
Do I have to use a $var first??
Thanks
stripslashes returns the modified string, leaving its argument unchanged. You have to assign the result to a variable:
$var = stripslashes($row['info']);
That said, why are you doing this? You almost certainly shouldn't be. There is no reason to strip slashes on data coming from the database, unless you've double-escaped the slashes when the data was inserted.
Your question is somewhat confusing.
stripslashes() takes parameter and converts backslashed symbols to normal ones. more over, it does not affect the parameter. it returns stripped version.
so $result = stripslashes($source) or $row["info"] in your case.
$var = stripslashes($row['info']);
is more correct. Or in string, use it like this
echo "something".stripslashes($row['info'])." some more thingy";
It almost seems, that you are using heredoc syntax because of your {}. Question, is why? Are you seriously displaying your results like this?:
echo <<<my_results
Info: {$row['info']}
my_results;
Well, since that is cool way to do so then here is your fix:
$row_info = stripslashes($row['info']);
echo <<<my_results
Info: {$row_info}
my_results;
However, I do not recommend that approach. Rather do it like this:
echo 'Info:' . stripslashes($row['info']);
Because {stripslashes($row['info'])} doesn't work indeed and stripslashes({$row['info']}) is an anecdote!
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';
?>