GET with javascript instead of PHP to track stats - javascript

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
}

Related

Can't echo code correctly. CSS dissapears

I want to make an email-signature page with Elementor and then generate the HTML-code like the code when you that appears when you "View Source" of a page, but code gets not exactly correct. I am using a Custom post type and a Elementor template that is not published so it works only when writing: https://mysite123123.com/a/site/?theme_template_id=1164
I have a PHP snippet plugin and this code gets the Post type HTML but not the same like when you press rightclick/View Source. (Its missing some css etc)
Can you see what Im doing wrong or if you know how to echo the correct html from a webpage like a regular visitor see it?
(Im using this code to give a user a HTML signature)
<?php
global $wp;
$vpost = get_post($post_id);
$urlcard = $vpost->post_name;
$current_url = home_url( $wp->request );
$data = file_get_contents("https://mysite123123.com/a/".$urlcard."/?theme_template_id=1164");
$html_encoded = htmlentities($data);
echo $html_encoded;
You can tell the browser how it should render your data using a Content-Type HTTP Response header.
To have your data rendered as plain text -- as the 'View source' view does -- use "text/plain" as mime-type;
header("Content-Type: text/plain");
echo $data

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>';

Creating a interactive avatar selection

Similar to how XBOX LIVE or PSN allows users to select an image/avatar from their database, how would I be able to achieve the same result using Javascript/jQuery, I used PHP to achieve this functionality, however, for further learning, I'd like to explore how this functionality could be achieved through other means (js or jQuery), my PHP code is down below:
<?php
$imageFile = "../img/avatars/";
$imageLoop = glob($imageFile. '*.*' , GLOB_BRACE);
foreach($imageLoop as $image){
echo "<label id='avatarLabel'><input type='radio' class='avatarInput' name='avatar' value='$image' >
<img src='$image' id='avatarImage'></label>";
}
?>
?>
my JQuery attempt at displaying images from folder:
$(document).ready(function(){
("body").load("imgs/");
)}
There is NO way to do it using php. Php can only process the file from POST data ad it will be located in a temp directory. At that moment there will be no data about original file folder.
Might be there are ways to do it using javascript, but it will be safety violation...
Give this a try, if I understand it correctly:
<?php
$dir = '../img/avatar/';
$loop = GLOB($dir. '*{.jpg,.png}', GLOB_BRACE); // look for only jpg and png
foreach ($loop as $image) {
$filename = basename($image); // only file name without path
echo '<input type="radio" name="avatar_register" value="' . $filename . '" required><img src="' . $image . '">';
}
?>
The numeric value of $x doesn't have any meaning and I have changed to $filename so you can locate the image with
$dir . $_POST['avatar_register'];
Since you are just selecting existing images from folder, no upload process is needed. You simply need to know which image user has picked.
I also have removed the id="" as it's inside a loop. Which means multiple ids with same value will be generated. This may cause some future problem if you are trying to locate the id from DOM

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

Targeting dynamic id's within a javascript file

In a php file I have tags with dynamic id's.
Example:
<ul id="<?php echo 'ul_'.$xx; ?>">
<li id="test_li">...</li>
</ul>
In a separate JavaScript file I am wanting to use the load function and target that div.
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
My question is, how could I load that php within a JavaScript file?
Notes:- The reason I have it set up this way is due to looping multiple ul's. I just want my ajax file to refresh the li's within the specific ul I am inside of.
Edit: If I use classes instead of Id's it adds my newly inserted rows into all of the ul's with the same class. If I use a plain Id it only works on the first ul. Further explaining why I need to target the way I am.
Unable to find a solution using the path I was on, I looked for alternate methods. Worked out I could add the event in the php file on the submit form button click.
The reason I wasn't using this method at first was because the code was running before it submitted to the DB and therefore was always behind in relation to the new data being submitted.
To my surprise I fixed this by just duplicating the line of code.
<script>
$("<?php echo '#enter_submit_button'.$linkid ?>").click(function(){
/*1*/
$('#<?php echo 'ul__'.$linkid; ?>').load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
$("<?php echo '#insert_new'.$linkid; ?>").fadeOut();
/*2 with delay*/
$('#<?php echo 'ul__'.$linkid; ?>').delay( 800 ).load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
});
</script>
I honestly couldn't say exactly why this works but the logic behind trying the method came from hoping that the double load would pick up the latest submission.
The simplest answer would be to put your JavaScript code into the PHP file inside
<script></script> tags, i.e, just before closing your <body> tag, do this:
<script type="text/javascript">
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
</script>
But if you must use an external js file, then you could do this:
Create a (.php) file and write:
<?php
header("Content-type: application/javascript");
?>
// your js code with php inside (as you would do in HTML)
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>
Then, in your main (html/php) file, put in:
<script src='myjs.php' type='application/javascript'></script>
Hope that solves it.

Categories