Been working on this since yesterday, having no success. I need to get the URL of a parent window into a form field on a child window so that I can send it along with customer from the form. Can anyone tell me how this can be done? I'd prefer it be done with PHP because I'm getting familiar with that.
Please and thanks.
If I did this on the parent page would it stor the parent's URL so that I could retreive it later?
<?php
$_SERVER['PHP_SELF'];
?>
and then to recall it on child perhaps:
if(!empty($_SERVER['PHP_SELF'])){
$link = $_SERVER['PHP_SELF'];
}else{
$link = "No URL submitted.";
}
is this about iframes?
If so, then:
parent.document.location.href - to get url of the document where the iframe is placed
top.document.location.href - get url of the document, whose url is in the address field of the browser
var childPopup = window.open();
var childForm = childPopup.document.createElement('form');
childForm.setAttribute('action', 'serverurl');
childForm.setAttribute('method', 'POST');
childPopup.document.body.appendChild(childForm);
var childText = childPopup.document.createElement('input');
childText.setAttribute('type', 'text');
childText.setAttribute('value', document.location.href);
childForm.appendChild(childText);
This is an example of Javascript that gets the URL of the parent window into a form field of the child window. Then this URL will be sent to the server as a parameter when the form is submitted.
have been working all day at this. apparently found at get parent.location.url - iframe - from child to parent
so you need <iframe src="http://your-site.com/framepage.php?parent=http://parentpage.com">
and in framepage.php (or whatever u has)
echo $_GET['parent'];
Related
how can I dynamically remember last link with php? For instance, this is the parent page's url,
http://website.com/#/events/upcoming-events/
note that I have a hash in all URLs.
and this is the child page's,
http://website.com/#/events/upcoming-events/event-1/
On the child page I have a link to go back to the parent page,
Back
$_SERVER['HTTP_REFERER']; only gives me http://website.com/ of course.
How do I get around to this then?
This will be difficult to do on server side but you can do this on client side using jQuery
$(document).ready(function() {
var hrefParts = location.href.split('/');
hrefParts.splice(hrefParts.length - 2);
$('.button-back').attr('href', hrefParts.join('/') + '/');
});
Ok so here's my situation: I have a page with 5 jquery-ui tabs, 3 of them contain a table each that is generated by php for the data, with each of them there's a set of input to filter according to the date, each table have their own form and update button. Now what I want to achieve is once I refresh and get back to the controller I want to add the corresponding fragment according to the button I pressed.
For example: If I click on the update button from the first tab I want to add #tabs-1, if it's the second one then I want to add #tabs-2.
Now I know I can do:
$_SERVER['SCRIPT_URI'] = $_SERVER['SCRIPT_URI'] . "#tabs-2";
Fine that gives me the correct url, but how do I make the browser go there at runtime? Is there any way to do this?
Here's a part of my controller, the main one.
class PageOptimisationV2C {
public static function main() {
$class = __CLASS__;
$c = new $class;
$c->get();
}
public function get() {
$this->display();
}
private function display() {
$tpl = new PageOptimisationV2V();
$client = ConsulterClient::getClientByNoClient(isset($_GET['cid']) ? $_GET['cid'] : 0);
$tpl->client = $client;
if(isset($_GET['cid'])){
$tpl->StatsLignesCellulaire = self::buildCellStatsReport();
}
$_SERVER['SCRIPT_URI'] = $_SERVER['SCRIPT_URI'] . "#tabs-2";
$tpl->display();
}
}
And then it goes on and displays the page. What would be the optimal solution to get to a fragment of the page, according to the submit button that is pressed.
I tried to make a jsFiddle to show you how the page looked but it got too messy and couldn't get the CSS to work right.
Every detail of information is appreciated.
The only way i'm aware of to do this with PHP is to do a redirect.
header('Location: ' . $_SERVER['REQUEST_URI'] .'#tabs-2');
This of course will result in another request and require you to do some workaround with your flow but its the only way to send a new url back to the browser.
Though it isn't a runtime PHP method you can also just embed the selection Javascript, for example:
var index = $.index($(<?PHP echo $tab; ?>));
$('#tabs ul').tabs('select', index);
and allow it to go through the normal selection process.
You can also just embed a hidden element, something like
<input type="hidden" name="tab-selected" id="tab-selected" value="tabs-2" />
and have some Javascript to check if the element exists and has a value and then select the tab like this:
$(function() {
if ($('#tab-selected').val()) {
var index = $.index($('#tab-selected').val()));
$('#tabs ul').tabs('select', index);
}
});
Today, with a fresh mind, I thought of something else to do while reading MWJump's answer... Why not use the action attribute of the form! It's there for a reason! So for each of my form (I have one in each of my tabs) I slapped the action attribute to be the fragment of the according tab so for the first tab action="#tabs-1" and the second action="#tabs2" and so on...
I found this solution to be the simplest and easiest in this particular case.
I have a form which uses the target attribute to target an iframe when the form is posted which posts to a PHP script. This part is working fine but I need to do something based on several results that the php script will put in the iframe.
What I am thinking of doing is when the PHP script has finished posting it echo's out some hidden input fields that contain various elements, such as the state of the post, whether it succeeded and what the final result was if it was successfully posted.
However, if I did this it would put it into the iframe so then the main web page wouldn't be able to access the hidden input fields.
How would the main web page be able to access these hidden input fields so that the main web page can perform some action, I.e. make a div within the web page show a specific error message or whatever.
The other thing is, once I know how I can get the data from the hidden input field, how would I know when I can go and get the values. I was thinking that when the form is posted via a JavaScript document.forms["myform"].submit() code I could then do a while loop and check to see if another hidden input field status is set to complete and once it says complete I can then get the values from the hidden input field.
I'm not sure if the way I suggested is the right way or doing what I want to achieve or if there is a better way of doing it.
UPDATE
I've tried what #lanzz suggested but it doesn't appear to have worked. Below is what I have tried.
$("iframe#image_upload_frame").on('load', function()
{
var iframeBody = this.contentDocument.body;
var data = $(iframeBody).find("#imageDirectory");
alert("data: " + data);
});
Below is how the iframe is defined
<iframe id="image_upload_frame" name="image_upload_frame"></iframe>
and I am echoing out a hidden input field in the php script that's within the iframe.
echo '<input type="hidden" id="imageDirectory" value="'.$imageDirectory.'" />';
The echo is definetly working as when I see view the iframe source I can see the hidden input however, the alert dialog is never shown as if something isn't working. There are no errors being reported either by the google chrome dev console.
If I understand correctly - you need a value from the iframe in the parent window, once the value is loaded into the iframe. I would add javascript to the iframe calling the parent and executing a function.
In the main frame:
function incomingValue(val) {
alert(val)
}
and somewhere in the generated iframe:
<script type="text/javascript">
parent.incomingValue("Hello world");
</script>
This should work assuming both frame sources share the same domain.
You can use postMessage for cross document communication between an iframe and it's parent.
See:
http://viget.com/extend/using-javascript-postmessage-to-talk-to-iframes
http://javascript.info/tutorial/cross-window-messaging-with-postmessage
Since you're running on the same domain, your main page's Javascript will have no trouble to access the contents of the <iframe> (example uses jQuery, you could rewrite into whatever libs you plan to use):
$('iframe#the-id-of-the-iframe').on('load', function() {
var iframeWin = this.contentWindow;
var iframeBody = this.contentDocument.body;
// access global JS vars defined in the iframe:
var someIframeVariable = iframeWin.globalIframeVariable;
// or, directly access elements in the iframe:
var someIframeElement = $(iframeBody).find('#element-id-inside-iframe');
});
A while ago I wrote a piece of code to upload a picture using some javascript and two iframes. The most important thing for me was to preview the pic. Maybe it will help you:
HTML:
<div id='fakebutton' onclick='select_pic()'>Just a button to select a pic</div>
<iframe src='uploadform.php' name'pic_frame'></iframe>
<iframe src='#' name='target_frame'></iframe>
both the iframes are hidden. The targetframe has no source (or an empty page, if you want to).
uploadform.php contains a form:
<form id='upload_form' action='dosomething.php' method='post' enctype='multipart/form-data' target='target_frame' onsubmit=''>
<input id='realfoto' name='realfoto' type='file' onchange='parent.foto_upload(window.frameElement.id)'>
</form>
and then some javascript:
First of all something to trigger the filebrowser when the user clicks the fake
function select_pic(){
b=window.frames['pic_frame'];
b.document.upload_form.realfoto.click();
}
And then a part to actually upload the pic, triggered by the onchange() in the input element:
function foto_upload(o){
var b=o;
o=getElementById(o);
if(o.contentDocument ) {o = o.contentDocument;}
else if(o.contentWindow ){o = o.contentWindow;}
else{return false;}
if(test_pic(o,b)){ //test if it is really a pic
getObj('foto_tmpdir').value=o.getElementById('tmp_dir').value;
o.getElementById('doctype_nr').value=b;
o.fotoform.submit();
}
else{
return false;}
}
In dosomething.php I perform actions on the uploaded pic (rename, resize etc). And it contains a few lines of javascript:
$a = 'upload was succes';
$b = 'my_image_name';
$c = 'whatever you want to put here';
?>
<script type="text/javascript">
window.top.window.smurf(<?php echo "'$a','$b','$c'" ?>);</script>
<?php
if you create in javascripty a function named smurf(a,b,c) you can pass along whatever you want form the php-script. One of the most important things for me was that I now can pass the filename of the uploaded pic to javascript, and use it to change an image.src for a preview.
Hope you can use something of it.
Your iframe source page should has a javascript call function instead of the hidden field. The function will call the opener window (your main page) and then it do any functionality you want. As blue print look at the following:
//in iframe src.php
<?php
if ($something){
?>
<script>
function doSomethingWithOpenerWindow(){
opener.document.write('hi);
}
doSomethingWithOpenerWindow()
</script>
<?php
}
else{
?>
<script>
function doAnotherSomethingWithOpenerWindow(){
opener.document.write('hi);
}
doAnotherSomethingWithOpenerWindow()
</script>
<?php
}
?>
On my website, I have a button which when clicked displays a feedback form in a popup window.
I need to pass the url in the PARENT window to a hidden field in the feedback form.
I tried:
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
<input type="hidden" id="addressBar" name="addressBar" value= "<?php echo $url ?>"/>
The problem with the above code is that it passes the url of the current window, in my case, the url of the feedback popup window.
How do I pass the url of the parent window to that hidden field?
Many thanks for your help
You can try $_SERVER['HTTP_REFERER'] instead of REQUEST_URI (it gives you current uri)
but keep in mind that it's not reliable...
$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'some_default_page.php';
Another way could be something like this ...
$cur_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'Feedback';
and then in popup.php just check if ref query string is set and retrieve its value.
I want to show a page of my website within iframe in another page.
I mean you can see helper.html while you are navigation main.php.
but I want to change some links in helper.html regarding to some conditions set in main.php.
The regular solution is to get content of helper.html and process it in main.php, then echo it.
But it is server side, I want this process to be client side.
Is that possible with JavaScript?
If your files are located at the same domain, you can use the top.frames property, ro refer to the window object of named frames:
Assume the top HTML to has such a structure:
<iframe name="main" /><iframe name="helper" />
Inside main:
top.frames["helper"].document.getElementById("linkID").href = "http://newlink.com";
If you're using AJAX, you can add the previously shown code in the callback handler. If main.php reloads on change, at the code within <script> tags.
If those files are on different domains but you have a full control of them, then use the following solution:
In the main page call an iframe with GET parameters. For instance:
<iframe src="foo.html?parameter=value" width="400" height="500"></iframe>
In an iframe parse GET parameters using Javascript and show an appropriate content:
// get the current URL
var url = window.location.toString();
//get the parameters
url.match(/\?(.+)$/);
var params = RegExp.$1;
// split up the query string and store in an
// associative array
var params = params.split("&");
var queryStringList = {};
for(var i=0;i<params.length;i++)
{
var tmp = params[i].split("=");
queryStringList[tmp[0]] = unescape(tmp[1]);
}
// print all querystring in key value pairs
for(var i in queryStringList)
document.write(i+" = "+queryStringList[i]+"<br/>");
Source: http://www.go4expert.com/forums/showthread.php?t=2163
Try this...
var myIframe = document.getElementById('SomeIFrame');
myIframe.src = 'www.joobworld.com';
Yes it is possible if both frames are in same domain.
See sample function: in the help frame the id of link is assumed to be "link"
function ChangeLink()
{
var Helpframe = document.getElementById("Helpframe");
var innerDoc = Helpframe.contentDocument || Helpframe.contentWindow.document;
var link = innerDoc.getElementById("link");
link.href="http://www.google.com";
}
This solution is inspired from Javascript - Get element from within an iFrame