Unable to add php within js file - javascript

I have a below script, which i need to add in a .js file.
jQuery( ".followlink" ).click(function(event){
event.preventDefault();
var $k = jQuery.noConflict();
$k.dialog({
title: ' ',
backgroundDismiss: true,
content : "<?php echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/list/followup-popup.phtml')->tohtml();?>"
});
$k('.jconfirm-scrollpane .container').addClass('follow-container');
});
I get error marked on this line <?php echo.., can anyone tell how to add this inside the JS file.
I have encloded the entire snippet inside the script tag.

You could do it in one way,
give element property like data-content and then in content label just put $(this).data("content"); so, Code should look like below:
jQuery( ".followlink" ).click(function(event){
event.preventDefault();
var $k = jQuery.noConflict();
$k.dialog({
title: ' ',
backgroundDismiss: true,
content: jQuery( ".followlink" ).data("content"),
});
$k('.jconfirm-scrollpane .container').addClass('follow-container');
});

Normally a javascript file is not interpreted by the PHP engine. You'll need to either modify your server's rules to process that file with PHP, or change the extension to .php. You'll also need to change the resulting content-type before outputting anything:
<?php
header('Content-Type: application/javascript');
?>

You need to serve the JS as PHP OR externalise the PHP:
Do this in your PHP file:
<script>var content = "<?php echo json_encode($this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/list/followup-popup.phtml')->tohtml());?>"</script>
<script src="yourjsfile.js"></script>
and have content: content in the js file

Related

Word Press PHP ACF Snippets in JS functions

I'm currently adding in Advanced Custom Fields for my site but when I try and add an ACF snippet to any of the functions the content doesn't show in the browser and all my js code in the script tags stop working and displaying in the browser. what am I doing wrong?
the js code is in script tags in the front-page.php file
function changeTestimonial1(){
document.getElementById("client-name").innerHTML ="<?php the_field('client_name1'); ?>";
}
I would suggest constructing your html/php/javascript as such:
<?php
$somevar = 1234;
?>
<HTML>
<script language="JavaScript">
// JS will contain value of php variable $somevar, but make sure you escape the value.
var some_js_var = <?php echo $somevar; ?>
</script>
</HTML>

How to include php file using jquery

I already visited this link,
How to include php file using Jquery?
but when I m trying it in my code, its not working. What could be the issue?
index file
<div id="content">Hi</div>
<script type="text/javascript">
var about_me = "<?php include('del.php') ?>"
$('$content').click(function(){
$('#content').load(about_me);
});
</script>
del.php
<?php
echo "hi again";
?>
First, make sure you include the jQuery library.
Second, don't use include as it isn't necessary. You will just load the file via jQuery:
<script type="text/javascript">
$(document).ready(function(){
var about_me = "del.php";
$('#content').click(function(){
$('#content').load(about_me);
});
});
</script>
NOTE: You have a typo in your selector $content should be #content to make it clickable. I have also included a document ready function in the event your script is at the top of the page, instead of the bottom.
.load() accepts url (type string) as first parameter, not a php code.
Try to change it from:
var about_me = "<?php include('del.php') ?>"
To: var about_me = "/del.php"
EDIT: You have a typo in your event listener's selector, should be $('#content').click() instead of $('$content').click()

How can I use javascript code with php?

What am I doing ?
I have a dynamically created html division containing the score of a game played by user which user can share on social media.
For sharing I have meta tag e.g. facebook
<meta property="og:image" content= "<?php echo $img_url; ?>"/>
Where $img_url is the link of the screenshot of dynamically created html division.
test.php -- where my above mentioned meta tag is placed and I want to take screenshot of html div if $img_url is not set.
<?php
session_start();
$img_url = $_POST['img_url'];
if(!isset($url)) {
/* <script>
function capture() {
html2canvas will give var img.
Which will be POSTED to testsave.php to save it in the server.
}
</script> */
}
?>
<html>
<!--dynamically created html division -->
</html>
testsave.php -- which will take the posted value from test.php and save the screenshot image to server and will send back the $img_url to test.php
<?php
session_start();
/*Get the base-64 string from data
Decode the string
Save the image
function predefined for random string*/
$img_url = $random_str.".png";
file_put_contents($img_url, $unencodedData);
$_SESSION['img_url'] = $img_url ;
?>
What my problem is ?
When facebook scrapes a paricular page , it doesn't take session values or any values from other page. So I can't send img_val from testsave.php
to another page because scraper won't read POSTED values.
So, what am I doing is when scraper will scrape test.php then if $img_val is not set I want my code to take the screenshot and post js variable var img to testsave.php to save the image, which will then post back the value of $img_val to test.php.
I know there may be many mistakes in above code like I can't put js inside php. But I tried it many way but couldn't figure it out.
Can you please suggest what can I do to make the code work or can you suggest any other way to do it ?
You have to echo out HTML from PHP
<?php
session_start();
$img_url = $_POST['img_url'];
if(!isset($url)) {
echo "<script>
function capture() {
//your js code here
}
</script>";
}
?>
<html>
<!--dynamically created html division -->
</html>
One of the solutions is to use here-document to assign the whole HTML page to PHP variable and later echo/print it where it matches your needs, and you can include the Javascript codes there as well
Example "this example doesn't include the image thing you mentioned but you can use any Javascript codes"
<?php
$variable=<<<_END
<html>
<p>Some HTML line...</p>
<script>
//you can write the Javascript codes you need here
console.log('Find this sentence at console !')
</script>
</html>
_END;
echo $variable;
?>
In order to deliver it in browser, we have to construct JS code as a string inside the PHP code
eg. Test.phtml - where i am calling a js function under some condition.
<body>
<?php if($this->emptyData){ ?>
<div id="someTestDialog">
<!-- call js function below -->
<?php
echo '<script type="text/javascript">testDialog();</script>';
?>
</div>
<?php } ?>
</body>
<script>
function testDialog() {
return $("<div class='dialog' title='Alert'><p>Hello World</p></div>")
.dialog({
resizable: false,
height:90,
width:90,
modal: true,
dialogClass: 'no-close success-dialog',
buttons: {
"OK": function() {
$( this ).dialog( "close" );
}
}
});}
$(document).ready(function() {
var text = "some code here" }); </script>

Use <a href> to call a formatted XML from within a php page. (?)

A table I extract from a DB, have a column dedicated to the path of XML file.
I need to open these xml from within the php.
First the column is shown:
<td>$row[2]</td>
Then a function submitdata(event) is called:
<script type='text/javascript'>
function submitdata(event){
var el=typeof( event.target )!='undefined' ? event.target : event.srcElement;
var data=document.getElementById('data');
data.value=el.dataset.value;
data.parentNode.submit();
}
</script>
And in the php:
if (function_exists('submitdata')){
$data = addslashes(strip_tags(#$_POST["data"]));
$myfile = fopen("$data", "r") or die("<h1>Unable to open file!</h1>");
$xml = fread($myfile,filesize("$data"));
$dom = new DOMDocument;
$dom->preserveWhiteSpace = TRUE;
$dom->loadXML($xml);
$dom->formatOutput = TRUE;
$xml_out = $dom->saveXML();
echo "<pre class=\"brush: xml\">";
echo htmlspecialchars($xml_out);
echo "</pre>";
fclose($myfile);
?>
<script type="text/javascript">
SyntaxHighlighter.all()
</script>
<?php
}
This code was working in a old server but I was not using the function_exists() because the xml was shown in another php page. This page's code was exactly the same code wrote above.
The request:
How do I open a XML file, through a link?
Or, are there better alternatives?
EDIT:
I'm trying with Ajax, no luck so far.
I really appreciate any help in building the Ajax/jquery that can get the name of the file from the <href> (or any other) and open the corresponding xml file in the same bootstrap's column.

How to use a php inside js

When i use the php and js separately it is working correctly
<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>
But when I use the php inside the js function it is not working correctly
<script> var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>
How can make work my second code?
The second code produces wrong HTML/JS:
<script> var test = 'asdasdasd'; <script>document.write(test)</script> </script>
- see for yourself in the generated page.
Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.
I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.
You can insert any value from php to absolutely everywhere in your file. Literally everywhere.
While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.
Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:
<script>
<script>
...
</script>

Categories

Resources