protld
protld

Reputation: 440

Echo the src value of an iframe on a page

If I have a file called 'index.php' and this file contains a lot of HTML lines...

Also (index.php) have this iframe:

<iframe src="http://test.com" />

How I can use PHP to get the src which is "http:/test.com" ... so it will be like that:

$getiframesrc=THE_CODE_WHICH_I_WANT_SOMEONE_TO_TELL_ME_ABOUT_IT;

And I can easily echo the src of the iFrame by echo $getiframesrc;

For example: If I want to make a browser using PHP, I want the URL Address Box's text to be the value of the iframe src (THIS IS ONLY AN EXAMPLE!!!)

So, please guys tell me what should be :

"THE_CODE_WHICH_I_WANT_SOMEONE_TO_TELL_ME_ABOUT_IT" .

EDIT: $getiframesrc will be in index.php too!

And thanks :-)

Upvotes: 1

Views: 2024

Answers (3)

protld
protld

Reputation: 440

Firstly, thank you very much @gibberish ( gibberish ) for your answer, it's the best answer for me and I'm using it now :-) .

I figured out how to do that with PHP (thanks for @gibberish to help, because his example helped me.) - But sorry :/ I can't say how I did that because it's very hard coded (everything is manual in that) ... so I will simplify it and post the answer :-)

Next to the @gibberish answer, we can use PHP GET variable and set the iFrame src with it.

Example:

PHP Part in index.php :

<?php
$iframesrc=$_GET['iframesrc'];
?>

HTML Part in index.php :

<iframe src=<?php echo $iframesrc; ?>></iframe>

And then we can access http://Mysite.tld/index.php?iframesrc=http://test.com ;)


So now, I can code well - like that:

if($iframesrc !=="http://test.com")
{
            //code
}

Upvotes: 0

cssyphus
cssyphus

Reputation: 40038

Here's a working example -- and make sure you close your <iframe> tag.

$('button').click(function(){
  var src = $('iframe').attr('src');
  alert( src );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<button>Read iframe src</button>

<iframe src="http://test.com"></iframe>


To re-use the src variable elsewhere on the page, just declare it outside the $('button').click(function() function -- or even outside the $(document).ready() fn.


EDIT:

To get the variable data to PHP... By the time the javascript runs, the DOM has been rendered. PHP has finished execution and will not run again on that page.

So, what to do? How to get the variable into PHP? You have two choices:

(1) Use a form - When submitting a form, the data is sent to the PHP file specified in the action= attribute on the <form> opening tag:

<form action="your_secondary_php_file.php" method="post">

Downside to a form is that user is navigated away from the page, or (at the very least) the page is refreshed.

(2) Use AJAX. AJAX (very simple, not to worry) will send your data to a back-end PHP file, the PHP file can do something with that data, and then it can (optionally) send new data/HTML/text/whatever back to the AJAX code block.

Advantage of using AJAX - will not refresh or move away from the current page. All user-entered data remains as is, and you can pro-grammatically receive data back from the PHP side and dynamically update the page with the new data. Magic by another name.

This answer contains some simple examples of AJAX.

Upvotes: 0

Ahmad ghoneim
Ahmad ghoneim

Reputation: 929

you can use ajax and jquery to get the src value then send it to the php file

Jquery

$(document).ready(function(){
var vidsrc = $("#iframeid").attr("src");
        $.post( "index.php", { videosource: vidsrc });  
});

index.php

if (isset($_POST["videosource"]))
{
$videosource = $_POST["videosource"];
// code to be excuted
}

Upvotes: 1

Related Questions