Problems with quotes when writing javascript code in PHP - php

I wonder what's the best method for writing javascript code into a PHP variable?
Some times it might be quite long javascript code... Is there a way without escaping all quotes?
<?php
echo '
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>'
?>

Use heredoc. For example:
$var = EOF<<<
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
EOF;
The EOF can be any arbitrary string you want, it just has to come directly after the <<< delimiter and match on both sides of the string you want to create.

The answer is simple don't mix PHP code and HTML/JavaScript and if you need to do so, end the PHP block with ?> and open it again with <?php after your HTML/JS block.
If you need it inside a variable, you could either use output buffering which is kind of messy though or use Heredoc/Nowdoc strings:
<?php
$foo = <<<FOOBAR
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
FOOBAR;
?>

Just do:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
a_div.innerHTML = '<iframe style="width:100%;height:300px;" id="iframe_upload" src="index.php">';
</script>
And in case you need php in there somewhere:
a_div.innerHTML = '<iframe style="width:<?php echo $width; ?>;height:300px;" id="iframe_upload" src="index.php">';

Related

How to use the jquery variable in script inside the PHP tag echo?

I am getting the issue
Warning: Use of undefined constant otherInput - assumed 'otherInput'
(this will throw an Error in a future version of PHP) Warning: A
non-numeric value encountered
I know there is some issue with the quote.
What I am doing is, I have to click on the anchor tag called Click me one and sending the data-id in the script. I am getting the id value in the script but I am getting the error on $(".showme'+otherInput+'").show();
I am using WordPress and I have to use the below code in the function.php so I have to use my script inside the PHP tag.
This is the screenshot of the code
Here is the code
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.showme{display: none;}
</style>
</head>
<body>
<div class="clickme" data-id="1">Click me one</div>
<div class="showme showme1">this is example</div>
<?php
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(".clickme").click(function(){
var otherInput=$(this).data("id");
$(".showme'+otherInput+'").show();
});.
</script>';
?>
</body>
</html>
It looks like everything in your php block is all JQuery/Javascript.
Q: What do you even need the PHP block and the "echo" for?
Current:
<?php
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(".clickme").click(function(){
var otherInput=$(this).data("id");
$(".showme'+otherInput+'").show();
});.
</script>';
?>
Suggested change:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(".clickme").click(function(){
var otherInput=$(this).data("id");
$(".showme"+otherInput).show();
});.
</script>'
Just follow the regular js standard, and if you willing to implement your script inside the php code:
Replace your code with these changes:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.showme{display: none;}
</style>
</head>
<body>
<div class="clickme" data-id="1">Click me one</div>
<div class="showme showme1">this is example</div>
<?php
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(".clickme").click(function(){
var otherInput=$(this).data("id");
$(".showme"+otherInput).show();
//here otherInput js variable is just used as regular js variable inside the script, so no need to extra commas to call that js variable, just use it like you do in js files, and will work fine.
});
</script>';
?>
</body>
</html>

How to put HTML header into a PHP variable

I would like to get my whole site's header into a variable.
So, for example, I would like to put these three lines into a single variable:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
And I'm writing a function for it:
function set_header(){
//This is where i would like to set the variable for the 3 lines I mentioned earlier
}
function set_header(){
echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}
set_header();
OR
function set_header(){
return '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
}
echo set_header();
function set_header(){
$header = '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
return $header;
}
echo set_header();

Declare variable outside of javascript file and use it in file

so i am declaring a variable inside of a .php file
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
But i want to use the variable templatePath inside of jscript_pages.js but when i do it like this my console says: Uncaught ReferenceError...
I hope someone can help :)
Thanks
The content of a script element is alternative content to use if the browser doesn't support src, it isn't a script to run before running the external script.
Use two script elements.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>
Set the variable before you reference it in script.
<script>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js">
</script>
As #Quentin said, it's either/or in a single tag.
In concept, you can do what you're looking for by having two script tags
<script type='text/javascript'>
var templatePath = "<?php bloginfo('template_directory'); ?>";
</script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/script/jscript_pages.js"></script>
But, in practice what's usually better (for testing, portability, etc) is for your script file to just define functions and then have your on-page scripts (if you have them) call those functions and apply the variables.
In this setup, your function bloginfo('template_directory'); should return a $GLOBALS variable.

JQmodal pop up can not appear

I've a problem about jqmodal. That pop up can not appear. My scenario is any data in variable warning, pop up will appear and will appear the all warning data.
This the code. Please help me. Thanks
<link type="text/css" rel="stylesheet" href="<? echo base_url().'css/jqmodal/css/prettify.css'; ?>" />
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/prettify.js"> </script>
<link type="text/css" rel="stylesheet" href="<? echo base_url().'css/jqmodal/';?>css/jquery.modaldialog.css" />
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/jquery.js"> </script>
<script type="text/javascript" language="JavaScript" src="<? echo base_url().'css/jqmodal/';?>js/jquery.modaldialog.js"> </script>
<script type="text/javascript" language="JavaScript">
$().ready(function(){
prettyPrint();
});
</script>
if(#$warning != NULL)
{
?>
<div id="jqModal" class="jqmWindow" style="display: none;"></div>
<script type="text/javascript">
jQuery().ready(function($){
$('#jqModal').jqm({onShow:setText});
$('#jqModal').jqmShow();
function setText(){
$('#jqModal').text(<?php echo #$warning;?>);
}
});
</script>
<?php
Hey Ayu I built a small demo for you Demo http://jsfiddle.net/BG42j/
Now if you will click on the view... in above demo you will get the text I am setting in my setText function. Rest you can play around with the demo. And please don't forget to accept the answer if this helps. :) Your accept ration is very low :)
Helpful link: http://dev.iceburg.net/jquery/jqModal/
So I reckon issue was the way setText is declared is not correct /* callback executed when a trigger click. Show notice */ onShow (callback): Called when a dialog is to be shown. Be sure to show (set visible) the dialog.
Hope this helps and have a nice one! cheers
Jquery Code
$(document).ready(function() {
$('#dialog').jqm({onShow:setText});
// $('#dialog').jqm().jqmShow({overlay: 70});
});
var setText = function(h) {
/* callback executed when a trigger click. Show notice */
h.w.text('Warning! foooooo');
h.w.css('opacity',0.92).slideDown();
}
​

regex to find a specific tag by content

I've 2 (or more) script tag in my html page, and i want to remove (replace with ' ') them, which one has myfunc in its content (only script tags).
an example:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function myfunc(){
...
...
...
}
</script>
<script type="text/javascript">
alert("Hello World!");
</script>
<script type="text/javascript">
alert(myfunc());
</script>
</head>
</html>
I'm looking for a RegEx to do that, for php preg_replace.
Thank you, very much.
<script type=\"text\/javascript\">(.*myfunc\(\).*?)<\/script>
replace it with
\1

Categories