Word Press PHP ACF Snippets in JS functions - javascript

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>

Related

Include js in php file

I have normal alert in my code, instead I need sweet alert as an alternative.
But the problem is my file is pure Php file, and there is no html tag to include the sweetalert.js file, hence it is not working.
<?php
// ...
echo '<script type="text/javascript" src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">';
echo 'swal("Mobile Number not registered.Please login through Email");';
echo '</script>';
?>
The text content of a script element gets ignored, if the script element has a src set. The JS code you are dynamically outputting here via PHP, should not have worked to begin with, even if it was “static” instead.
Something like
echo '<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>';
echo '<script>swal("Mobile Number not registered.Please login through Email");</script>';
would work … but it’s not really nice. You should not output larger parts of static HTML via echo in the first place, but properly “break out” of the PHP code part, see https://www.php.net/manual/en/language.basic-syntax.phpmode.php
A proper way to do this:
<?php
// some PHP stuff here
?>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>swal("Mobile Number not registered.Please login through Email");</script>
<?php
// more PHP stuff here

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>

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>

display php file content in the javascript of another php file

So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.

Is it possible to put a javascript function in a php file?

Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>

Categories

Resources