1

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 :-)

protld
  • 418
  • 1
  • 5
  • 12
  • From where you want to use `$getiframesrc`? – Zakaria Acharki Jul 07 '16 at 23:03
  • @ZakariaAcharki - I can't understand what you mean, can you please explain? :) EDIT: $getiframesrc will be in index.php too! – protld Jul 07 '16 at 23:05
  • Okay i see that you want to use it inside index but it's hard to answer your question if you doesn't describe the context why and how you will use it.. – Zakaria Acharki Jul 07 '16 at 23:12
  • 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!!!) – protld Jul 07 '16 at 23:15

3 Answers3

1

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
}
Ahmad ghoneim
  • 844
  • 7
  • 13
0

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.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Can we make it with PHP? I don't want a button to be clicked and show the src... I will use if($getiframesrc=='http://test.com') { } like that :/ – protld Jul 07 '16 at 23:10
  • +1 I figured out how to do that with PHP based on your first example :D thank you very much... (***Marked as Chosen Answer***). – protld Jul 07 '16 at 23:17
  • 1
    You can do the entire thing in PHP *if you can do it before the page is finished rendering (i.e. turned over to the user)* Otherwise, once the DOM is rendered, you must use javascript/jQuery/AJAX. – cssyphus Jul 07 '16 at 23:18
  • Thanks - happy to help. – cssyphus Jul 07 '16 at 23:19
0

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
}
protld
  • 418
  • 1
  • 5
  • 12