Creating a rollover with PHP and Javascript - javascript

I've read a TON of question and answers about rollovers using PHP and Javascript and can't figure out why this isn't working. So I have this PHP code:
$rollover = '$("#' . $godName . '").mouseenter(function() {
$("#' . $godName . '").attr("src","img/gods/god_cards/dark/' . $godImage . '2.png");
});
$("#' . $godName . '").mouseleave(function() {
$("#' . $godName . '").attr("src","img/gods/god_cards/' . $godImage . '.png");
});';
$godName and $godImage are variables retrieved through a query to a database. I wanted to use this code to cut down on the amount I'd have to write to create a rollover for over 50 images, so basically it would create a function for every image retrieved in the query. Then I wanted to stick it in a javascript tag like so:
<script type="text/javascript">
<?php echo $rollover; ?>
</script>
But it doesn't work. And I've tried doing it with and without the echo and no version of whatever I've found on here or anywhere else works. When I echo it out as just text it looks fine, it spits out what it should so I'm not sure why the javascript just doesn't seem to be accepting the php. Any help?

Do you check if the HTML Dom is ready? You are using jQuery, so you can use:
<script type="text/javascript">
$(function(){
<?php echo $rollover; ?>
});
</script>

Related

How to navigate to a page using base_url() function in codeigniter?

I want to navigate to a page after doing some operations. Earlier I navigated like this:
echo "<script>alert('not deleted..'); window.location='http://localhost/CodeIgniter/crud/index.php/Crud_C'</script>";
And it was working perfectly.
But now I want to navigate like this using base_url():
$this->load->helper('url');
echo "<script>alert('not deleted..'); window.location='" . <?= base_url('index.php/Crud_C'); ?> . "';</script>";
Which is not working. I have used
$config['base_url'] = 'http://localhost/CodeIgniter/crud/';
in config.php file;
What should be the correct syntax of the code after echo?
Thanks
You can use the code as below.
echo "<script>alert('Not Deleted...'); window.location.href = '" . base_url('index.php/Crud_C') . "'</script>";

Pass PHP variable to javascript inside php variable?

my script generates the image using php/html. once it generates, i want to display the image without reloading the page.
I call the javascript(to change the image) function within the PHP itself. but i want to know how to pass new image name in this java script?
$newpath as src variable
Here is the code:
$newpath="output/".$outputfilename;
echo '<script type="text/javascript">',
'document.getElementById("latestimage").src="<newpath>";',
'</script>';
You should concatenate your variable when echo, like:
$newpath="output/".$outputfilename;
echo '<script type="text/javascript">
document.getElementById("latestimage").src="' . $newpath . '";
</script>';

How to know system command is in use in php?

I have a HTML/PHP code as shown below in which on click of a button conversion of mp4 into mp3 starts happening.
HTML/PHP Code:
<?php foreach ($programs as $key => $program) { ?>
<tr data-index="<?php echo $key; ?>">
<td><input type="submit" id="go-btn" name="go-button" value="Go" data-id="<?php echo $key; ?>" ></input></td>
</tr>
<?php }?>
Php code (where mp4=>mp3 conversion happens):
$f = $mp4_files[$_POST['id']];
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
system('C:\ffmpeg\bin\ffmpeg.exe -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination_dir . DS . $parts['filename'] . '.mp3', $result);
break;
}
As soon as the button is clicked from the HTML/PHP Code above, the text gets changed from Go to Converting in the UI because I have added JS/jQuery code in my codebase but this JS/jQuery code which I have added just change the text only.
It doesn't actually know that the Conversion is happening in the background.
JS/jQuery code:
$("input[name='go-button']").click( function() {
// Change the text of the button, and disable
$(this).val("Converting").attr("disabled", "true");
});
Problem Statement:
I am wondering what modification I need to do in the JS/jQuery code above so that UI actually knows that conversion is happening in the background.
Probably, we need to add make establish some connection between JS/jQuery and php code above but I am not sure how we can do that.

wordpress - php and javascript dont go well together

I know some HTML and CSS but zero JAVA or PHP. I would like to insert LineIt button into my functions.php in WordPress:
The code goes like this:
<img src="imgpath" alt="LINE it!" />
Unfortunately this will display only the line link without my permalink. The_Permalink will be display as text not as dynamically taken URL.
I tried to do something like this:
<img src="linkin" width="[Width of Button]" height="[Height of Button]" alt="LINE it!" />'
But of course that didn't work. Oh and I don't want to use tags <script >.
Could help?
Thanks
Here is the function in my functions.php that I'm trying to modify with custom "LineIT" button:
function show() {
$return = '<aside class="show1 show2">'
. show_content() .
'<div class="show-box">'
. apply_filters('show_filter', show_rander()) .
'<div class="show3">'
. getIcons() .
'</div></div>**<img src="linkin" width="[Width of Button]" height="[Height of Button]" alt="LINE it!" />**<div style="clear:both;"></div>'
. show_content()
. show_content2() .
'</aside>
return apply_filters('show_buttons', $return);
}
according to the document at https://codex.wordpress.org/Function_Reference/the_permalink, the the_permalink() function is usually applied in the template within The Loop. Whereas functions.php is usually provide convenient functions and register them for the future use or register in The Loop.
So put the_permalink() in functions.php might not work as what you expected.
The general template files are like home.php, page.php or category-6.php(which will provide specific template for the category with id as 6).
In these files, the Loop usually is like:
<?php
if(have_posts()){
while(have_posts():
the_post();
the_content();
?>
//here you can add the link
<img src="imgpath" alt="LINE it!" />
<?php
endwhile;
endif;?>
Please refer to this page https://codex.wordpress.org/The_Loop_in_Action for further knowledge of the loop.
if you really want to use the functions.php, I would suggest using the other function get_permalink($id) and get_the_title($id) which will get the permalink of the post by the id and add a function for it. so the function will be:
function get_line($id){
$url = get_permalink($id);
$title = get_the_title($id);
return '<img src="imgpath" alt="LINE it!" />';
}
and then call the function in The Loop of the template.
To learn more about wordpress template, please refer https://developer.wordpress.org/themes/basics/template-hierarchy/
As suggested by #lhrec_106 I have used 3rd option and modified code works like a charm. Thanks!
I have inserted function get_line($id) before my function and then simply entered
. get_line($id) inside my custom function. Button works and share via Line my dynamically generated url.
Thank you

GET with javascript instead of PHP to track stats

I use a simple PHP script placed on each page of my websites to track some stats.
<?php
echo( "<img src='http://maindomain.com/stats.php?var1=" . #$_GET['var1'] . "&var2=" . #$_GET['var2'] . "&r=" . #$_SERVER['HTTP_REFERER'] . "' width='1' height='1' border='0' />" );
?>
This script works fine, once placed on domain1.com and domain2.com the file in maindomain.com/stats.php must validate the GET and register the stats.
The problem is that i also have domain3.com and this site is not PHP but simple HTML.
So i'm looking for a way to change the script in javascript, something like this:
<script src="http://maindomain.com/stat.php?var1=XXX&var2=YYY&r=HTTP_REFERER"></script>
I think this is possibile but i don't know how to pass the GET vars with js.
You could try using CSS instead of JavaScript.
If you create your stylesheet as such; style.css.php, you can execute php code in the stylesheet, and let the rest of the stylesheet just be as it already is.
Something like:
<?php
include('stats.php')
?>
html {
color: #000
}

Categories