how to display iframes with sites from a mysql database? - php

Hi guys I'm trying to somewhat of an autosurfer and for the life of my I cannot figure out how to use iframes on my site to display all of the sites I have in my mysql database. Now all of the website urls are stored in a column in my table in the database, so I assume I'll need to assign them to an array. But my main problem is getting them to display each site for fifteen seconds then load a different one. I understand iframes for the most part, but I don't get how to get it to show a site, refresh show another, etc. I also can't figure out how to load the sites into an array for the iframe to use. Please help. Thanks.

You will need to use JavaScript to refresh your iframe.
Let's say this is your iframe:
<iframe id="iframe" src="http://www.google.com"></iframe>
You'll need to use JavaScript to do the refresh, and AJAX if you want it to get data from your database. So do something like this in a javascript file (I'm using jQuery):
$(function(){
function refresh_iframe(){
$.get('iframe_url.php', function(data){
// set iframe src to the url that the php gave us
$('#iframe').attr('src', data);
// run this function again in 15s
setTimeout("refresh_iframe()",15000);
});
}
// run the first time
refresh_iframe()
});
Then in iframe_url.php you'd have something like:
<?php
$q = mysql_query("SELECT `url` from `iframe_urls`");
$results = mysql_fetch_assoc($q);
$key = array_rand($results);
echo($results[$key]['url']);
?>

Related

How can I get the jQuery variable from the link of the displayed YouTube video into PHP to send to a database?

Hello, I am making a jQuery YouTube search and display web application and I need to send the link of a specific video to the database using PHP.
In the application, the user searches for a video and the unique code for that video gets inserted into a set YouTube link using an "id" variable. I want to capture the full link (including the unique id) and put it into the database without having to open a new page (which is how AJAX does it, I think).
Here are links to some similar problems, but they haven't worked for me. (getting youtube video id the PHP, How do I find all YouTube video ids in a string using a regex?, https://stackoverflow.com/questions/19977278/how-to-get-php-data-in-javascript, Data transfer from JavaScript to PHP, http://www.webdeveloper.com/forum/showthread.php?249863-pass-jquery-value-to-php-variable (sorry for the massive amount of links, Stack told me to include them if they were similar and didn't work))
Here is the code that makes it work so far.
<script type="text/javascript">
//on thumbnail click
$videoDiv.click(function(e) {
displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
});
//Display the video
function displayVideo(id, title) {
//embed player
swfobject.embedSWF('http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1',
'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
{ id: "youtubevideo" } );
}
</script>
Obviously, this isn't the whole function, but hopefully it should be enough.
Please help me, I've run out of places to look.
Hi Niklas,
Thank you for answering me, I’ve tried that out and it turns out that my code is a bit more complicated to allow for that.
My project for university also needs to have information sent to the page that the video is on, currently I have the form for the user on one page and the code to search YouTube and display the user’s information as well as the video on the next (external) page.
Unfortunately AJAX is still one of the languages I haven’t yet learnt, so the documentation you linked me to didn’t make much sense.
My code is very complicated as it uses the YouTube API as well as JavaScript, jQuery and PHP. The main js file for this searches YouTube and once the user clicks on the thumbnail, it fires up a displayVideo function, which also features the information from the previous page.
I can’t redirect the user to a new page after they select the video, just to get the id into PHP, because that would wipe the data from the first form when the user gets redirected back to the YouTube form.
I’m hoping you could please assist me.
Answer
Okey, know I understand what you're asking. That you would do something like this:
<script type="text/javascript">
$(function() {
//on thumbnail click
$videoDiv.click(function(e) {
var videoURL = displayVideo(entry.media$group.yt$videoid.$t, entry.title.$t);
$.post('url-to-your-php-file', {videoURL: videoURL}, function(response) {
// Do stuff with the response given/echoed by the PHP file
});
});
//Display the video
function displayVideo(id, title) {
//embed player
var videoURL = 'http://www.youtube.com/e/' + id + '?enablejsapi=1&playerapiid=ytplayer&autoplay=1';
swfobject.embedSWF(videoURL,
'video-placeholder', '544', '408', '9.0.0', null, null, { allowScriptAccess: "always" },
{ id: "youtubevideo" } );
return videoURL;
}
});
</script>
Hope this helps and good luck!
Answer before revised question
I won't write the code for you, but you're right in your thinking and I will give you some links that may help. You need to make an AJAX call to a PHP file which saves the link to the database.
AJAX Call
You're using jQuery, which makes AJAX calls real easy. Have a look at the jQuery .ajax() documentation. Though in this case it being a really small and "unadvanced" call you need to make, the shorthand wrapper function .post() will do nicely for you.
Retrieve the link in PHP
After that you can retrieve the link in the PHP using the global $_POST variable.
Adding the link to the database
For adding it to the database, I would recommend the PHP PDO library for making the connection and queries.
Hope this helps and don't hesitate to ask if there's anything you wonder!

Pass value to PHP file using JavaScript and receive new set of values from PHP to JavaScript and display in Browser

I have a page called test.html. This HTML page will contain 2 JavaScripts file. I need to pass a variable value to a PHP file using the first JavaScript file. The user can copy this javascript on as many pages as he wants for displaying output.
The PHP file that receives the variable from the JavaScript file retrieves some data from database depending upon the variable's value. This retrieved value can contain HTML content. This PHP file will always reside on my server.
All of the retrieved content (from the PHP file) needs to be passed to the second JavaScript file so that the data can be displayed in browser. This JS file will need to stay together with the first JS file in order for the data to be displayed.
So I have this:
JavaScript File
<script type= "text/javascript" src="http://www.myserver.com/custom_script.php?unique_id=12"></script>
PHP FILE
//custom_script.php
<?php
$unique_id= (int)$_GET['unique_id'];
$res = db_res(" SELECT col1, col2, col3, .... col10 FROM table WHERE unique_id = $unique_id
LIMIT 1 ");
$rows = mysql_fetch_array($res);
?>
<div id="1"><?php echo $rows['col1']; ?></div>
<div id="2"><?php echo $rows['col2']; ?></div>
<div id="3"><?php echo $rows['col3']; ?></div>
.
.
.
<div id="10"><?php echo $rows['col10']; ?></div>
I need to send all the HTML above from the PHP file to the second JavaScript file so that the output can be displayed. Please note that the CSS styling is also applied using Div ID, so I am expecting those styles would show up too. Please note that there may be more than 10 columns, so an efficient way of passing data is highly appreciated.
So what would be the easiest and the best way to send all the HTML data from the PHP file in one go to the 2nd javascript file residing in test.html page, so that the HTML data can be displayed in test.html file?
EDIT 1:
I apologize to everyone if my question has been confusing. I just thought of an example and hence wanted to add it my edit. I hope you are all aware of what Google Analytics (GA)(or any other Website Visits Stats Tracker) does. Right? You register for a Analytics account and Google gives you a piece of JS code that you copy and paste in your website. And after couple of days, you can login into your GA account to see the stats. Correct? What I am trying to do here is just the same.
Users come to MY WEBSITE and register for an account and I give them JS files that they can paste in their website. The only difference between GA and my website is that GA is personal to you and no one else, but you, the account holder can see it. Whereas in my case, your data can BE SEEN by others as well, as long as you include the JS file on your website. Because users can't just take my PHP file and run it on their server, I am trying to access MY PHP file by giving the full path to it in the JS file.
For example:
<script type= "text/javascript" src="http://www.myserver.com/custom_script.php?unique_id=12"></script>
This is not an actual JS file, rather it is just a medium for my custom_script.php script to receive the unique_id of the user, query MY database and send back the HTML data related to this requesting user. And I am stuck with this part. Hope this clairifies what I am trying to do.
The jQuery documentation actually gives an example almost identical to your problem:
http://api.jquery.com/jQuery.get/
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
This will get an html page and insert it under an html element with class 'result'.
You should be able to replace your php script url and specify where the output should be displayed.
If youre worried about conflicts with other jQuery instances, have a read of this:
http://api.jquery.com/jQuery.noConflict/
If you want to write your own AJAX handler check out this page :
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest
Essentially you will do something like this (taken from the link):
function reqListener () {
console.log(this.responseText);
};
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "yourFile.txt", true);
oReq.send();
I think its worth doing if you've never had to do it before, but you will open yourself up to lots of potential problems that have been solved for you.
Ideally if I were doing this I would return json from the handler and allow the user to decide how to display it, but thats your call.

Use Ajax to send data to external page, and then load said external page

I have a thumbnail gallery populated from a database using php. Each thumbnail is also a link. What I would like is to be able to load an external php page in each case, with the large version of each thumbnail on it, flanked with related images.
The database tables are all set up properly in a relational sense, but I'm not sure about the functionality of being able to send data, in this case imgId, from the thumbnail gallery page, to the external page, and load the external page at the same time.
I thought it would be possible to do with a form submit, but since I need this functionality on every single thumbnail link, I thought that Ajax would work using jQuery. But alas it doesn't seem to be sending the data when I click on the link.
Hopefully someone can give me some advice. Thanks in advance.
The HTML:
<a target="_blank" href="secondary_imgs.php" class="gallery" value="16">
<img src="new_arrivals_img/thumbnails/boss-skaz1_black_front.jpg">
</a>
The jQuery:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $('.gallery').attr('value') });
});
Use $(this) inside the function to reference the clicked gallery
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') });
});
Try this:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') }, function(data) {
$('body').append(data);
});
});
If that does nothing when you click on it, browse to "/secondary_imgs.php?imgID=xxx" and see if it is returning the data correctly at all.
Ok, I feel kinda stupid, but I figured out that all I needed to do was pass the data through the URL. The PHP code below shows what I mean, and I do thank those who helped me with this problem. My fault for making it faaaar more complicated than needed.
My question is however, is it safe to place data in the href as I'm showing below? I remember someone mentioning a potent ion problem concerning google spiders and the like.
The PHP:
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<a target='_blank' href='secondary_imgs.php?imgId=".$row['imgId']."'></a>";
}

Opening up a webpage inside a div tag instead of a tab

I was wondering if this was possible to do. I know you can pull a html file and put it on your page like this, <a href='index.php?content=Contact.html'> . Is there someway to pull a webpage from a URL to your site. So instead of a link open in another tab, it would open that webpage on your current site page. If it's not possible, is there some sort of similar solution I can use.
You can use the iframe tag to display other web pages inside your own web page.
When you say:
I know you can pull a html file and put it on your page like this, <a href='index.php?content=Contact.html'>
Actually, that's not a normal feature - it's only a function of whatever index.php file is on your server, and simply displaying content referenced by a GET parameter can actually quite dangerous depending on whether you know what to protect against and how.
I guess it depends on how "embedded" in to your site you wish for it to be, but it sounds like you could use iframes.
If you want to load an external website into your site, have a look at iframes [docs].
If you want to update parts of the page with content coming from your domain and without refreshing the whole page, you can use Ajax.
Yes, it is possible. You can use AJAX to get the other file and then set the .innerHTML property of your div to the loaded content. In the simplest way, with jQuery you'll have something like this:
var data = jQuery.get("http://my.domain.com/file.html")
$('#mydiv').html(data);
you could use jquery's load() function. i would do it something like this:
$(document).ready(function () {
$('a').click(function (e) {
e.preventDefault();
$('#myDiv').load($(this).attr('href'));
}
}

Adding text to <title></title> if gathered mysql data is new?

What is that called and how do you do it? My page refreshes every 10 seconds using jquery querying a database.
I get new tables casually and I want to be able to update the field to display how many new tables were gathered. I want this because instead of viewing the page to see if any updates happened I can just look at the title of the page inside a tab instead of switching over every 10 seconds you know?
How can this be done? Thanks.
Also the data I am gathering is text in a small table and data is just failed admincp user credentials.
You can change the title of a HTML document using JavaScript:
var title = "This is my new title, can be anything";
document.title = title;
I don't know how you are storing the number of new fields, but you could use a function like this:
function updateTitleBar (newTables)
{
document.title = newTables;
}
... Or you can just use document.title = newTables where you define the newTables somewhere else (and change that value every time you refresh the page in jQuery).
Maybe something like this?
<body onload="runTicker()">
function runTicker(){
setTimeout("document.title = new Date();runTicker()", 5000);
}
But instead of doing document.title = new Date(), replace that with a call to a function that would make an AJAX call to your PHP script. Perhaps using jquery if you're accustomed to that already?

Categories