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()
Related
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>
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
The Jquery will not run. After the button is clicked, I'd like it add text, "Hello Everyone" after the Say: between the tags. The jquery.js file is uploaded to my server and is in the same place as the testpage.php file.
Should my button be inside <html></html> or is it something else?
my testpage.php code:
<?php
echo '<button id="some-btn">Test</button>
<script src="jquery-3.2.0.min.js"></script>
<p>Say: </p>
<script>
$(document).ready(function () {
$("#some-btn").click(fucntion (){
$("p").append("Hello Everyone");
});
});
</script>';
?>
You have mistakes in your code. E.g. fucntion is misspelt, should be function
<?php
echo '<button id="some-btn">Test</button>
<script src="jquery-3.2.0.min.js"></script>
<p>Say: </p>
<script>
$(document).ready(function () {
$("#some-btn").click(function (){
$("p").append("Hello Everyone");
});
});
</script>';
Like Qirel mentionned, I prefer to break the php. That way you prevent having too large strings in PHP.
Should my button be inside or is it something else?
Your elements should be between your body tags. You can also change your click function to the next one:
$(document).on('click', "#some-btn",function (){
$("p").append("Hello Everyone");
});
This way your event will trigger even for the elements that are added to the DOM once it is rendered.
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>
For some reason some Javascript/Jquery code does not work when placing it inside PHP.
<?php
if (x > 0) {
?>
<script type="text/javascript">
alert("Hi!");
$("#fault_message_mail").hide();
</script>
<?php
} else . . .
?>
In case x>0 it is executed only the Alert and not the hide method(I'd like to hide a div declared in some html code).
Why it is not executed? Is it prudent to use jQuery inside PHP?
I've been searching similar questions in Stack overflow, however I cannot find a positive answer.
Thank you
You'll need to attach a handler something like:
$(document).ready(function(){
("#fault_message_mail").hide();
});
or
$('#mybutton').click(function(){
("#fault_message_mail").hide();
});
You have to inlude jQuery library if you have not included :
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
and then you have to write you code inside ready function like this :
$(document).ready(function(){
("#fault_message_mail").hide();
});
and third thing is that you have written a wrong syntax here :
<?php
if (x>0){ ?>
^
it should be like this :
<?php
if ($x>0){ ?>
maybe the element is not in the dom, try the following
if (x>0){ ?>
<script type="text/javascript">
jQuery(function ($) { // run on document.ready
alert("Hi!");
$("#fault_message_mail").hide();
});
</script>
<?php }