Hello everybody I'm trying to pass a parameter to my controller.php from javascript, but it doesn't pass and gives me error of undefined URL. kindly help me i shall be thankful to you...Here is my code
function JSfunction(assetid)
{
window.location="controller.php?command=delete&assetid=".assetid;
}
You're mixing PHP and JS, you use + to concatenate strings in JavaScript
Change the code to this and it should work:
function JSfunction(assetid) {
window.location="controller.php?command=delete&assetid=" + assetid;
}
What you are doing now is creating a string and accessing the assetid attribute of that string which is undefined.
you should set window.location to the full URL, not just the relative URL. I.E.
window.location="http://foo.com/controller.php?command=delete&assetid=" + assetid;
BTW, JS uses + to concat, not .
You must include server address, if it is being used locally,it can be denoted by http://localhost/AppName/pages?QueryString.
Concate string with plus sign, dot is used in php script for concatenation.
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 this piece of code
redirect_loc(index.php);
And the definition for this function is somewhere in a functions file.
function redirect_loc($location=NULL)
{
if($location!=NULL)
{
header("Location:$location");
exit;
}
}
When I pass "index.php" (note the quotes around to specify as a string) it works, however passing index.php gives error The requested URL /indexphp was not found on this server.
Why doesn't this work when it's not passed as a string ?
When you write:
redirect_to(index.php);
This is taking index as a constant and php as another constant and concatenating them using the . concatenation operator.
It is looking for a constant named index (and again for one named php) and, not having found a constant with that name, uses the value "index" (or "php") as a string literal.
This is not recommended practice. If you switch on error logging you will see that it is issuing a notice like "No constant defined index, assuming string value instead".
The . acts as concatenation operator . Did you see what the name was of the file it couldn't find? No . ... It joined the two "strings" (index and php) together - it thought that's what you wanted.
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']?>')"
The difference between them is that the PHP's urlencode encodes spaces with + instead of %20?
What are the functions that do the same thing for both the languages?
Use rawurlencode instead of urlencode in PHP.
Follow this link at php's own documention rawurlencode
rawurlencode will do the trick, the link is for reference.
Actually even with JavaScript encodeURIComponent and PHP rawurlencode, they are not exactly the same too, for instance the '(' character, JavaScript encodeURIComponent will not convert it however PHP rawurlencode will convert it to %28. After some experiments and tips from others such as this question another Stackoverflow question.
I found the ultimate solution here.
All you need to do is use the add following code
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
They will be EXACTLY the same now, for example
fixedEncodeURIComponent(yourUrl) (JavaScript) = (PHP) rawurlencode(yourUrl)
no problem with decode, you can use decodeURIComponent() for JavaScript and rawurldecode for PHP
I was having the same problem between rawurlencode() and encodeURIComponent(). The difference for me was that I didn't discover the issue until using encodeURIComponent() in numerous source files, so going back to fix and change them all and then re-test everything was not an option.
Fortunately, JS gives you the ability to "hijack" built-in functions by assigning the same name to a new function. You can thus change the behavior of encodeURIComponent() with a very slight modification to Phantom1412's code, and without having to recode anything.
Just put this script in your page before your code makes any calls to encodeURIComponent():
var encodeURIComponentOld = encodeURIComponent;
encodeURIComponent = function(str) {
return encodeURIComponentOld(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
};
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';
?>