Open Random Post Using Javascript - javascript

I want my code when someone clicks the link then a link will open random of 3 posts between */android.html ,*/samsung.html and */xiaomi.html.
My code is like this
<?php
$YuuClass->update_dls($file['downloads'], $file_id);
echo '<script type="text/javascript">
var x=Math.floor((Math.random()*3)+ 1);
var xxx=null;
if(x=="1"){xxx="android.html"}
if(x=="2"){xxx="samsung.html"}
if(x=="3"){xxx="xiaomi.html"}
</script>';
redirect('https://safelink-ainodorama.blogspot.com/2017/02/"+xxx+"?url='.base64_encode($copy['webContentLink']));
exit();
?>
Where "+xxx+" will change random between */android.html ,*/samsung.html and */xiaomi.html when I click it.
I tried to click it with code like that only showing base64code.
Ex-link when I click
http://myweb.web.id/0aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL2Evc2RuMTgzcGVrYW5iYXJ1LnNjaC5pZC91Yz9pZD0xMUNoa0JpS3lvWkRMMGIxTWxYek4yeUt2NGRQcnJoMG8mZXhwb3J0PWRvd25sb2Fk
I want when I click it the URL will be like this
https://safelink-ainodorama.blogspot.com/2017/02/android.html?url=base64_code_here
where android.html can change randomly from 2 another page android, Samsung and Xiaomi.

Because javascript is client-side and php is server-side the javascript wont change xxx.
Instead have an array of your pages, then use array_rand to pick a random one.
Something like:
<?php
$YuuClass->update_dls($file['downloads'], $file_id);
$page = [
'android.html',
'samsung.html',
'xiaomi.html'
];
$url = 'https://safelink-ainodorama.blogspot.com/2017/02/'.$page[array_rand($page)].'?url='.base64_encode($copy['webContentLink']);
redirect($url);
exit;

Related

How to prevent those PHP variables from being cached?

I'm using this code in functions.php on WordPress to generate an affiliate link based on the visitor location, It's working perfectly but the problem is that if page caching is turned on (W3 Total Cache), The variables get cached so if someone from the UK was the first one to open the page then the second one from Germany opened the page he will get the same link that the first visitor got.
One more thing please, I'm still very new to PHP and javascript so I would appreciate if the answer was simplified enough.
<?php
add_action( 'woocommerce_before_add_to_cart_button', 'affiliate_link', 10);
function affiliate_link() {
$not_avilable_country = '<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>';
// IP Geolocation
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
// Get Custom Fields
$de_asin = get_post_meta(get_post()->ID, "wccaf_de_asin", true );
$uk_asin = get_post_meta(get_post()->ID, "wccaf_uk_asin", true );
//////////////////////////////////////////////
if ($country_code=="DE" or $country_code=="DE") {
$amazon_domain = 'https://www.amazon.de';
// $associate_id = 'bonstato-21';
$asin = $de_asin;
}
else if ($country_code=="GB" && $uk_asin!=="") {
$amazon_domain = 'https://www.amazon.co.uk';
// $associate_id = 'bonmedico-21';
$asin = $uk_asin;
}
///////////////////////////////////////////////
if( wp_is_mobile() ) {
// Amazon Link For Mobile
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>';
}
</script>
<?php
}
else {
// Amazon Link For PC
?>
<script>
function amzGo(){
window.location='<?php echo $amazon_domain ?>/gp/aws/cart/add.html?AssociateTag=<?php echo $associate_id ?>&ASIN.1=<?php echo $asin ?>&Quantity.1=1';
}
</script>
<?php
}
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="amzGo();"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
?>
A way to deactivate/disable W3 Total Cache on specific pages is to use another plugin called: "Plugin Organizer" by Jeff Sterup.
After installing and enabling it, go to settings (of the plugin) and follow the instructions to set it in the right way.
Once the plugin is enabled and settings are saved correctly, when you edit/create a new page using the editor you can see a checkbox that's show you which plugin to enable for that particular page and which to disable.
In this way you can disable W3 Total Cache on the page that's use your function on functions.php.
Hope it helps.
The most common way to bypass the cache is to serve this data via the WordPress json api and use java script to put the always fresh data into place.
The problem is not that the variable inside the code is being cached; what happens is that W3 Total Cache grabs the first page produced by your PHP code and store it in the hard disk of the server. Thenceforth, the browsers' requests are answered delivering the HTML static file stored in server's hard disk (PHP will not produce the same page again).
To solve this problem, you must transfer the "intelligence" inside your PHP code, which is responsible for create slightly different versions of your page, to your Javascript code. Bastian Haustein suggested one way to do that, using WordPress REST API. However, I would try first doing it in a simpler way: after page load, capture the link of your cached page and modify it according to the location of your visitor. Naturally, you will have to capture visitors geolocation also using JS - see the link suggested by cjmling: How to get visitor's location (i.e. country) using geolocation?

jQuery remove attribute won't work if multiple clicks in fast speed

I have some check boxes on my page. Through jQuery change function I am appending a div if checkbox is checked. If checkbox is not checked remove that div. It works fine if clicks in slow speed. Whenever multiple click attempts happens in fast speed it don't remove the div. It keeps appending the div multiple time. Can someone please help me fix this? Following is my attempt.
<script type="text/javascript">
$('#include_addon_checkbox_<?PHP echo $addon_ids; ?>').change(function(){
if($(this).is(':checked')){
var summary_div = '<div class="sidebar_addon" id="sidebar_addon_<?PHP echo $addon_ids ?>"><div class="addon-content"><?PHP echo str_replace("_", " ", $addons_keys) ?><p><?PHP echo $addon_package_name; ?> <span>(more details)</span></p></div><div class="addon-price">$ <?PHP echo $addon_package_price; ?></span></div>';
$('#addons_container').append(summary_div);
$('#sidebar_addon_<?PHP echo $addon_ids ?>').hide().slideDown(750);
}
else {
$('#sidebar_addon_<?PHP echo $addon_ids ?>').slideUp(750, function(){ $(this).remove(); });
}
});
</script>
I think it is about your animation timings. You can lover animation times to 200-300ms. Or disable all checkboxes until your animation has ended. Then enable them.
maybe the php variable's problem.
you can try this:
<script>
var addon_ids = "<?PHP echo $addon_ids ?>";
var content = .....;
and so on......;
</script>
<script>
$(function(){
$('#include_addon_checkbox_'+addon_ids ).change(function(){
add you code.
})
})
</script>
Before loading the page, the php variable assigned to the js variable.
then jquery load it.

PHP and Java Script interaction handling

I have a requirement where on clicking button, I need to show all the links captured. The java script code would be in a separate file test.php, and the links are captured in an additional file links_generation.php. The function 'getLinks()' has a conditional flow that if no of links is less than 5, it would display all the links, else it would display 5 links and additional text that "x more links are there".
Now, I have a requirement where on click of button, I would like to show all the links captured. I'm not sure how to capture this using javascript.
can someone pleaser help how to proceed with this.
function getLink($result,$currency,$lang) {
foreach($result as $id => $data) {
$num++;
if($num <= 5) $list = $list.linkName(substr($id,3));
}
$list = substr($list, 0, -2);
echo "<li>".$lang['pagelike1']." ".$lang['pagelike2'].": ";
echo $list;
if($num > 5) echo "...".$lang['and']." ".($num-5).$lang['other_ad'];
echo "</li>";
}}
PS: the code is slightly modified,and pls be assured that this is working. I would need to incorporate the new functionality. Would appreciate your help.

Send Information From A Webpage To Another

I know this is almost duplicated, a lot of people asked that and a lot answered them by PHP Form but I really didn't find anything for my problem.
I have a page called platforms.php and this page has a group of image-links which are: Windows,Mac, Android and iOS.
So what I want is when somebody clicks the Windows link (as an example) they go to a page called download.php and the page should say You are using Windows!.
Please not that I don't want to create a page for every link, I want it to be one page only.
Thanks in advance!
Make URL like that
<a href = "http://example.com/download.php?device=window" >Window</a>
<a href = "http://example.com//download.php?device=mac" >Mac</a>
On your page download.php
if(isset($_GET['device'])) {
$device = $_GET['device'];
}
if ($device == 'window' ) {
// window message here
} elseif ($device == 'mac' ) {
// mac message here
}
send an extra parameter in your url and then in the same single page use the condition like:
let the parameter name be 'pagetype'.
download.php
if ($_REQUEST['pagetype'] == 'windows')
{
//your output for windows
}
else if ($_REQUEST['pagetype'] == 'mac')
{
//your output for mac
}
else if(..)
{
.
.
.
Note:
You can replace $_REQUEST, with $_GET or $_POST, depending on method you use to post data to this page
You can have an HTML link and send the data through GET parameters. Something like (in platforms.php):
<a href="download.php?os=Windows >Windows</a>
<a href="download.php?os=Mac >Mac</a>
<a href="download.php?os=Android >Android</a>
<a href="download.php?os=iOs >iOs</a>
And then in downloads.php, getting the variable "os" and do whatever you want with it:
if ( isset($_GET['os'] ) ) {
echo "You are using " . $_GET['os'];
}
you can add image like
<img src="window.jpg"/>
<img src="ios.jpg"/>
and on download.php page you can check
$value = $_GET['key'];
if(value== 'window')
if(value== 'ios')
etc
hope this is what you want.

load() and getscript() not always working

On my index.php page I use the jquery load() to load other pages in a div "bodycontent". This works fine and dandy. I disable regular links, and must make the href the page name, without the php, and tag the php back on when the load() occurs. One of my pages I'm loading in the div I have dialog boxes pop up on links.
When you initially go to the page, this works fine, but if you steer away by clicking on "products" or any other link on the page at all, and then go back to "home" and click on a link "read more", the dialog boxes will not show up again. You have to refresh, and then it will work until you click a link again. Also, this works fine in my IE 9, ver9.0.8, but older IE at work (like 8), and at least one other persons chrome the dialog box will not come up at all. There are also IE9's at my work that this will just not work on, despite me deleting temp internet files, ect. On my computer it works fine on brand new downloads of firefox, chrome and IE, so I'm also wondering if my method is backwards compatible and cross browser, and if not what I can do to make it so. I'm also using get script in a function with the load to pull the jquery, jquery ui, and my javascript file for the dialog.
I've also looked at things like ajaxcomplete and such, but being mine initially works, I'm not really sure what to do.
My site main index page loading home.php initially (the news, ect)
Fiddle with my navigation javascript, which makes the pages inactive/active and load()/getscript in the div
dialog.js:
$(document).ready(function() {
$('#dialogbox').dialog({
autoOpen: false,
title: 'Loading title...',
modal: true,
width: 500,
height: 400
});
});
function readMore(id,title,cat,desc,post,auth) {
//alert(id +","+ title +","+ cat +","+ desc +","+ post +","+ auth);
var $dialog = $('#dialogbox').html('Category: '+ cat +'<br/>'+ desc +'<br/>---------------------------------<br/>Posted by: '+ auth +'<br/>' + post);
$dialog.dialog('open');
$dialog.dialog("option","title",title);
}
How I pull to function readMore:
<?php
require('config/dbconfig.php');
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);
/* fetch values */
while ($stmt->fetch()) {
//echo 'id: '. $id .' title: '. $title;
echo "<table border='0'>";
$shortDescLengthn = strlen($descn);
if ($shortDescLengthn > 106) {
$sDCutn = 106 - $shortDescLengthn;
$shortDescn = substr($descn, 0, $sDCutn);
} else {
$shortDescn = $descn;
}
echo "<h1>$titlen</h1>";
echo "<tr><td>$shortDescn...</td></tr>";
echo '<tr><td><a href="javascript:void(0);" onclick="'
. 'readMore(' . $idn . ',' . htmlspecialchars(json_encode($titlen)) . ','
. htmlspecialchars(json_encode($categoryn)) . ','
. htmlspecialchars(json_encode($descn)) . ',' . htmlspecialchars(json_encode($postdaten)) . ','
. htmlspecialchars(json_encode($authorn)) . ')">Read More</a></td></tr>';
echo "<tr><td>Written by: $authorn</td></tr>";
echo '<tr><td><img src="images/hardcore-games-newsbar-border.png" width="468px" /></td></tr>';
}
echo "</table><br />";
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
So again, basically I need to see why this isn't working after I click a link, and how it can be more cross browser and backwards browser compatible.
All I had to do was add:
$('#bodycontent').empty();
before the load(), and it works on every computer I've tried so far. I'm guessing the div needed to be emptied before loading new html in it, so going back it was keeping the js and such from getscript and messing up. Also only kept the single js file that was required only by home.php to be loaded, and put everything else in the index page (like it should have been anyways).

Categories