I have a <script> link were I pass vaiables into it however on the network tab in the console its showing %27 why?
Code:
type=calculator&min=50&max=400&amount='".<?php echo $price;?>.
Full Code:
<div class="col-md-4">
<?php if($product->is_on_sale())
{
$price = $product->get_sale_price();
echo 'sale';
}else{
$price = $product->get_price();
echo 'Normal';
}
echo 'The Price is:'. $price;
?>
<script async src="widget-0.1.0.js?type=calculator&min=50&max=400&amount=".<?php echo $price;?>" type="application/javascript"></script>
As Satpal says, encoded value of ' is %27
remove the single quote like:
type=calculator&min=50&max=400&amount=".<?php echo $price;?>
as it is also useless in your query string so you can remove it, and try again.
Edited: Instead of:
type=calculator&min=50&max=400&amount=".<?php echo $price;?>"
try
type=calculator&min=50&max=400&amount=<?php echo $price; ?>"
You can use like this .remove space if it is present .
<?php
$price = 27;
?>
<script src="http://testSite.com/resources/js/animatedcollapse.js?type=calculator&min=50&max=400&amount=<?php echo $price;?>" type="text/javascript"></script>
You have mentioned the single quotes in code block.But there is not single quotes in script attributes.And also you need to remove dot and try like this:
<script async src="widget-0.1.0.js?type=calculator&min=50&max=400&amount="<?=$price?>" type="application/javascript"></script>
Related
My code :
<?php echo ' <script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques".p);? >
The value of p is displayed as 0.
You need to close your php tag properly as well as the <script> tag like so:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" +p);</script>'; ?>
Also, change the . to a + as you are concatenating in javascript not PHP
The correct answer is:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" + p)'; ?>
Strings in single quotes will be escaped, use quotation marks instead.
<?php echo "<script>var p=0;for(var i=0;i<=5;i++){p++;}alert('ques' +p);</script>"; ?>
The mistakes in your code are,
Close your php tag properly - ?> instead of ? >
Close the script tag in the end - </script>
Close the single quote you started with echo
The correct code would be
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){ p++; } alert("ques" + p); </script>'; ?>
I'd like to print this JS script.
<script src="<?php echo base_url('assets/inspinia/js/jquery-2.1.1.js'); ?>"></script>
<script src="<?php echo base_url('assets/jquery-ui/jquery-ui.js'); ?>"></script>
I write like this:
print("<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>");
print("<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\'></script>");
and this:
echo "<script type=\'text/javascript\' src=\'".base_url('assets/inspinia/js/jquery-2.1.1.js')."\'></script>";
echo "<script type=\'text/javascript\' src=\'".base_url('assets/jquery-ui/jquery-ui.js')."\"></script>";
But, both method can't get the external JS file.
Note: The html in in a modal
To simplify, I will first give you an example with the url saved as a variable.
$url = base_url('assets/inspinia/js/jquery-2.1.1.js');
echo "<script src='$url'></script>";
You can keep it in the long form if you prefer, but you would need to leave the quotes and concatenate (similar to what you have been doing)
echo "<script src='" . base_url('assets/inspinia/js/jquery-2.1.1.js') . "'></script>";
For that reason, I always prefer to assign it to a variable first so that I can see the separation of "get value" and then "print string including that value"
Here is an example of using javascript in php echo:
<?php
echo '<script>Hello World!</script>';
?>
I'm a bit sorry to see what you mean, is that so?
echo "<script type='text/javascript'>
function getState(){
var cs = ".$state.";
return cs;
}
</script>";
Do you want to introduce JS files in PHP?
You can wrap yourself,and you can refer to YII2's wording, like this:
/**
* 定义按需加载JS方法
* #param $view View
* #param $jsfile
*/
public static
function addScript($view, $jsfile) {
$AssetManager = new AssetManager();
$jsfile = $AssetManager - > getPublishedUrl('#backend/modules/motorcade/assets').$jsfile;
$view - > registerJsFile($jsfile, [motorcadeUIAsset::className(), 'depends' => 'backend\modules\motorcade\assets\motorcadeUIAsset']);
}
Hope it helps you
Try this,
echo "<script src=" '. base_url('assets/inspinia/js/jquery-2.1.1.js') .' "></script>";
I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.
My variable contains a color name. My example:
<?php
$myvar = blueColour;
?>
I need to add this value to body of a html page using jQuery:
<script type="text/javascript">
jQuery(body).addClass("<?php echo $myvar; ?>");
</script>
Can I use php inside jQuery this way? Thank you.
EDIT: its an wordpress site. I use this in a if to add that class on a specific page template.
<script type="text/javascript">
jQuery("body").css("color", "<?php echo $myvar; ?>");
</script>
yes, as long as your html file is interpreted by php (having .php/.phtml extension)
I did it. Worked.
Correct is:
jQuery('body').addClass("<?php echo $myvar; ?>");
with
jQuery('body')
not
jQuery(body)
if ( is_page_template('nameOfYouTemplate.php') ) {
echo '<body class='. $myvar .'>';
} else {
echo '<body>';
}
I want to be able to echo a PHP link inside a JavaScript string, i.e. <?php echo SITE_URL ?>. How can I do this with the code below?
<script type="text/javascript">
$.backstretch("http://www.example.com/bg.jpg");
</script>
<script type="text/javascript">
$.backstretch('<?php echo str_replace(array("\\", "'"), array("\\\\", "\\'"), SITE_URL); ?>');
</script>
I also made sure to escape ' and \ to avoid any problems.
you can do
<script>
var test= '<?php echo SITE_URL; ?>';// and use it as you please
</script>