This code is very simple so I feel like it could be shortened by relating the php variable to the jquery function parameter. if you have any ideas, please help. thank you
<script type="text/javascript">
<?php if (is_page($toppageID1)) { ?>
$(document).ready(function () {
Show('1');
});
<?php } elseif (is_page($toppageID2)) { ?>
$(document).ready(function () {
Show('2');
});
<?php } elseif (is_page($toppageID3)) { ?>
$(document).ready(function () {
Show('3');
});
<?php } elseif (is_page($toppageID4)) { ?>
$(document).ready(function () {
Show('4');
});
<?php } elseif (is_page($toppageID5)) { ?>
$(document).ready(function () {
Show('5');
});
<?php } elseif (is_page($toppageID6)) { ?>
$(document).ready(function () {
Show('6');
});
<?php } elseif (is_page($toppageID7)) { ?>
$(document).ready(function () {
Show('7');
});
<?php }; ?>
</script>
Have something like $somevalue that would help to switch instead of elseif.
<script>
<?php switch($somevalue){ ?>
$(document).ready(
function (){
<?php case 1: //?>
<?php case 2: //?>
<?php case 3: //?>
<?php case 4: //?>
<?php case 5: //?>
}
)
<?php } ?>
</script>
First off I would change that function of yours is_page to page and return a string with the ID. You already have access to those $toppageID variables in PHP so why not use them in the function itself. If you absolutely cannot for scoping or other reasons, pass them all in the function at once. You want a single output not executing the function numerous times.
Example script using the re-factored function page:
<body>
...
<!-- end of body -->
<script type="text/javascript">
(function(id) {
if(id) {
Show(id);
}
else {
// throw some error or something
}
})(<?php echo page() ?>);
</script>
</body>
If you need to funnel all the variables at once maybe something like this:
})(<?php echo page($toppageID1,$toppageID2,$toppageID3,...) ?>);
SOrry if I was unclear but im using wordpress functions. a switch works great.
<?php $currID = get_the_ID(); ?>
$(document).ready(function () {
});
Related
How do I load a script if a PHP condition is true?
For example, I got an external script called script.js and I wanna load this if it's on the product page of WooCommerce.
I've tried the following but it doesn't work because it prints the code on the page:
<?php
add_action( 'template_redirect', 'check_product_page' );
function check_product_page() {
if(is_product()) {
include ('script.js');
}
}
?>
If I write the script inside a PHP like the below, it causes a fatal error:
<?php
echo
'<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
?>
Try this:
<?php
if($condition == true){
echo '<script type="text/javascript" src="script.js"></script>';
}
?>
or:
<?php if($condition == true): ?>
<script type="text/javascript" src="script.js"></script>
<?php endif; ?>
Actually this works on my side, checkout maybe you have another issue:
echo '<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
It's because the single quotes are inside your string. You should actually do it the way which other people have shown, but if you need it to be an inline script you can do it like this:
<?php
//some php can go here..
?>
<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>
<?php //more php can go here... ?>
I am not quite sure why have I not been able to call a function from say File1 to File2 . I have used the require_once also .
Eg :
File1.php
<?php
function Test()
{
alert("Hello");
}
?>
File2
<?php
require_once("File1.php");
My code
?>
<script type="text/javascript">
My code....
$("#email_summary").click(function() {
<?php
Test();
?>
});
</script>
Could you please let me know my mistake ?
Thanks guys :)
you can not call php function by JavaScript like you did
$("#email_summary").click(function() {
Test();
});
</script>
should be
$("#email_summary").click(function() {
<?php echo Test(); ?>
});
</script>
and
function Test()
{
return 'alert("Hello")';
}
edit: if you want to make it work like your code you can do this by
File1.php
<?php function Test() { ?>
alert("Hello");
<?php } ?>
and than just include like
<?php
Test();
?>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I'm dynamically generating a list. I want to make each row hover on mouseover and clickable to a link. I want the link to pass the id of the content of the row.
Basically:
foreach ($courses as $cid=>cinfo){
$univ = $cinfo['univ'];
$desc = $cinfo['desc'];
$size = $cinfo['size'];
$start = $cinfo['start'];
print "<div class='rc_desc' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}
<script>
$(function ()
{
$('#rc_desc$cid').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc$cid').click(function ()
{
$(location).attr('href','student.php?$cid');
});
});
</script>
The issue is in the js/jquery. I want to be able to grab the $cid and pass it to the student.php page upon click. The php code above works but the js won't of course. I know the fundamental of client-side vs server-side languages. This question doesn't warrant a lecture. I know I cannot do this exactly, but it is what I want to happen ultimately. Any thoughts on how I can achieve this simply? Thanks in advance my friends!
Yes, if you include the <script> section in your PHP code, you can do something similar to the following:
<script>
var foo = <?php echo $foo; ?>;
</script>
In your case, you would be looking into the following code structure:
<script>
$(function () {
$('#rc_desc<?php echo $cid ?>').hover(function () {
$(this).toggleClass('.tr');
});
$('#rc_desc<?php echo $cid ?>').click(function () {
$(location).attr('href', 'student.php?<?php echo $cid ?>');
});
});
</script>
The reason why this is possible is because although the Javascript is run on the client-side, it's processed on the server-side first prior to being presented on the page. Thus, it'll replace all necessary instances of $cid with the one you have included.
Enjoy and good luck!
EDIT:
<script>
$(function () {
$('.rc_desc').hover(function () {
$(this).toggleClass('.tr ');
});
$('.rc_desc').click(function () {
$(location).attr('href', 'student.php?' + $(this).attr('id').split('rc_desc')[1]);
});
});
</script>
You can do it like this :
$(location).attr('href','<?php echo $cid ?>');
The javascript code doesn't know it comes from php, it appears as a constant (a literal) for it.
Yes it is possible. All you have to do is put your <?PHP echo $cid; ?> in where you need it
<script>
$(function ()
{
$('#rc_desc<?PHP echo $cid; ?>').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc$cid').click(function ()
{
$(location).attr('href','student.php?<?PHP echo $cid; ?>');
});
});
This is possible because by the time the scrip is put into the page the cid has already been replaced by the string on the server. Since PHP is server driven, before it spits back the html/script it will be just like you put it in yourself.
There is almost no way to actually communicate PHP and JavaScript. However, the best and simplest way is to set the ID within an attribute. The new HTML5 data attributes would be perfect.
For example, have a anchor tag with
<span data-id="22">some event</a>
and then:
$(this).attr('href','student.php?'+$(this).attr('data-id'));
Or just use
<?php echo $id; ?>
if it is not external JS file
Try this code
foreach ($courses as $cid=>cinfo){
$univ = $cinfo['univ'];
$desc = $cinfo['desc'];
$size = $cinfo['size'];
$start = $cinfo['start'];
print "<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}
<script>
$(function ()
{
$('#rc_desc$cid').hover(function ()
{
$(this).toggleClass('.tr');
});
$('.rc_desc').click(function ()
{
$(location).attr('href','student.php?' + $(this).attr("data-id"));
// if you want to redirect the page do this
// window.location.href = 'student.php?' + $(this).attr("data-id");
});
});
</script>
I think you block should be :
<script>
$(function ()
{
<?php foreach($courses as $cid=>cinfo) : ?>
$('#rc_desc<?php echo $cid; ?>').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc<?php echo $cid; ?>').click(function ()
{
$(location).attr('href','student.php?<?php echo $cid; ?>');
});
";?>
<?php endforeach; ?>
});
</script>
UPDATE
But you don't really need to do this, you have
"<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
You can try this
$(function (){
$('.rc_desc').hover(function ()
{
$(this).toggleClass('.tr');
});
$('.rc_desc').click(function ()
{
attrId = $(this).attr("data-id");
$(location).attr('href','student.php?'+id);
});
});
I am writing some jquery to call an ajax script every 2 seconds to get the result and update the page. I am mostly a backend programmer and could use some help on this.
This is the code I have now:
<script language="javascript">
function downloadProgress(id) {
$("#" + id + "").load("index.php?_controller=download&_action=getDownloadProgressAjax",
{
downloadId: id
}
);
setTimeout(downloadProgress(id), 2000);
}
</script>
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>">
<script language="javascript">
downloadProgress(<?php echo $dl["download_id"]; ?>);
</script>
</div>
<?php
}
?>
This does not work. What am I doing wrong or would you suggest another approach?
Thanks
I think that you are confusing your PHP script by giving it both query string variables (sent as GET) and data (which is probably getting sent as POST). Try this:
$("#" + id).load("index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id }
since you are using jquery, you can use the $.ajax function when the page is ready.
$(function () {
function function downloadProgress(id) {
$.ajax({
url: "index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id
})
setTimeout(function () {
if (downloadnotcomplete){ // this way your script stops at some pont.
downloadProgress(id);
}
},2000);
}
});
You will attach the downloadProgress(id) function to your download button or anything else, to trigger the function the first time.
The problem you are having is that you have to provide a parameterless function and not a function call to setTimeout. Also, I would do it a little bit different and use setInterval instead of setTimeout as it relays your intention better in the code. Here is how I would do it:
<script language="javascript">
$(function() {
setInterval(downloadHandler, 2000);
});
function downloadHandler() {
// I'm not sure where the id is coming from you will probably need to put a
// class on your div's so that you can select them.
$(".MyDivClass").each(function() {
var id = $(this).attr("id");
downloadProgress(id);
});
}
function downloadProgress(id) {
$("#" + id + "").load(
"index.php?_controller=download&_action=getDownloadProgressAjax",
{ downloadId: id }
);
</script>
and then on your div:
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>" class="MyDivClass"/>
<?php
}
?>
Hope this helps.
How do i do in jquery, to check if there's anything in isset SESSION user_message, each 1 seconds, and if there is then display #box else dont..
thanks
PHP file (session.php):
<?php if( isset($_SESSION) ){echo 1;}?>
JS file:
function checkSession(){
$.ajax({url: "session.php", success: function(data){
if( data == 1){
$('#box').show();
}else{
$('#box').hide();
}
}});
}
setInterval('checkSession()',1000);
This is untested, but it should do what you're looking for. Note that, typically, AJAX request expect XML or JSON, so you can reformat the session.php file the way you want.
Use PHP to echo the jQuery command:
if (isset($_SESSION['user_message'])) {
echo '<script> $("#box").… </script>';
}
This will only echo the following if $_SESSION['user_message'] is set:
<script> $("#box").… </script>
Simple as this:
<?php
if (isset($_SESSION['user_message']))
{
?>
<script>
$('#box').show(2000);
</script>
<?php
}
?>
Or you can use fadeIn or fadeOut with specified time.
<?php if (isset($_SESSION['user_message'])) { ?>
<script type="text/javasscript">
$("#box").show(1000);
</script>
<?php } ?>