Pass value from pop up to parent window via window.opener - php

I know that similar questions have been asked before but I have not been able to find one that uses window.opener in the same way that I have.
I have a page that opens a pop up to display/edit/delete values in a db. Once it has done it's thing I use this to end the script and go back to the parent.
die( "<script>window.opener.location.reload();window.close();</script>");
This works well but I would like to pass a value back to the parent that I can use $_GET to retrieve.
This is for a a calendar and I use the above code to refresh the parent page so that the changes made to the diary event displayed in the pop up are automatically displayed (otherwise the user has to refresh themselves). At the moment when the page refreshes it goes back to the default date which is today rather than the date of the event affected. I would like to pass the date back to the parent
tia

Sorted it, thought of a much simpler answer, I used sessions instead, kept it simple

you can put a js code into your popup window
<script>
window.opener._newVar = "new value for parent window";
// now you can direct access (_newVar) after popup closed.
</script>

Instead of .reload(), just navigate to the same page with the querystring you want by setting location.search:
window.opener.location.search = "?value=" + val; window.close();

Related

Reloading Page Data after PHP Submit

ok folks,
I have created a PHP page that is querying a database, and through a whileloop, displays the contents of that database table with a REMOVE and PUSH button. The REMOVE button removes it from the database entirely, and the PUSH button pushes that entry into another database and sets a variable that the entry has been pushed.
What I'm running into is that I can't quite get the page to refresh, in turn running an new query of the first database and displaying only those entries that have not been removed or pushed.
I can only get the query to run correctly if I manually refresh the page, whether it be F5 or control+r (command+r).
What is the proper way to refresh the page so that the query will run again on page load?
If you want to reload the page using Javascript, try this:
window.location.reload(true);
You can also see this answer:
How to reload a page using JavaScript?
there are two ways
If putting extra load on db is not a problem, use jquery methods likes $.get()
$.get('url',{},function(data){
//load results in appropriate div;
});
If you don't want to put any extra load on database just hide the row when it is removed or pushed.
$('.remove').click(function{
$(this).css('display','none');
});
similarly make it for pushed
Do you have some extreme caching setup on your web hosting solution?
If maintaining nice-looking URLs on this page is a non-issue you could always set a timestamp in PHP and append it to the string.
I'm not big on PHP but a javascript example would look something like this.
ts = new Date();
urltorefresh += '?timestamp=' + ts.getTime();
location.href = urltorefresh;
This would make sure the page is absolutely not in the browser cache since this specific URL have never been requested before.

Can I Pass value from form to popup window, display something based on what was passed, then pass something back?

I need to do this using PHP and Javascript.
I have one form, with a few inputs. One of them is gender.
When they click a link/button I need to pass the value of the gender drop down to a popup.
Based on that value I show either male or females portraits.
I'd like to pass the value back to the parent window once they select a portrait and then click a button.
I have the code to display the portaits. But how do I pass the values back and forth, and how do I make a value passed from the parent window available to my PHP code in the new popup?
Any help greatly appreciated.
The problem is well defined, but the question itself is still somewhat vague. I'll give it a shot though. If you really mean a pop-up (separate window) rather than an on-page dialog (such as jQuery UI elements and so forth), then the communication channel you'll want to use is the object returned from window.open() to communicate from parent page to child page, and window.opener to communicate from child to parent. For example:
var genderPicker = document.getElementById('genderSelect');
var gender = genderPicker.options[genderPicker.selectedIndex].value;
var portraitChooserWindow = window.open("path/to/script.ext?gender=" + gender);
// portraitChooserWindow is now a reference to the newly opened pop-up (or null if the browser blocked pop-ups)
From the child window, window.opener is now a reference to the parent window.

Problem with pagination - no success yet

Hey guys I am really messing up with this.
This is I'm doing: If there are 100 users to fetch from Database, I'm using pagination and showing 10 users at a time. when user will click on next page, he will get next 10 users through ajax(called ajax on click) and so on. I'm showing 10 page-links right now with first, next last and previous links.
This is how flow will go: On a.php created links and called ajax function with every link, passing url(b.php) & target(where I will get result), with url also passing clicked pageno., this pageno. will go to b.php and next 10 users will be shown with the help of ajax.
This is the problem: Currently I'm showing 1-10 links with first and last links, unable to show next and previous links because to redirect to next or previous, I am not getting the current page number on a.php i.e i'm passing to b.php. also links are created in foreach loop.
I am trying hard to get this done, but no success yet.
waiting for valuable reply.
Ok, I think I understand...
I think what you need to do is store the current page number as a JavaScript variable. Use the function that is doing the Ajax to update this variable and also update the next/previous links.
Hope this helps.
Update:
On second thoughts, here's a better idea. Having your ajax call directly in your link means you have to put together a url and update it, which is awkward and annoying. This isn't ideal. Instead it is better to store the variables and have some set functions work with them in a set way. So, you could have something like:
<a href="javascript:void(0)" onclick="gotoNextPage()">
Rather than passing them the number of the page to go to, you can store the current page number in a javascript variable and do something like this:
gotoNextPage(){
// lets assume the current page number is stored in a
// variable defined outside of this function called curr_page
curr_page++; // increment the current
call_ajax('user_info.php?pageno='+curr_page);
}
This way the code for the link doesn't ever change.
You can still pass other variables (such as 'order', 'perpage' etc) to gotoNextPage() but you might find you don't need to.
A couple of things to bear in mind:
1. Using jQuery or something similar would probably make things easier for you.
2. Anyone without javascript enabled will not be able to use your site. Consider changing it to something like
<?php
echo '<a href="your_main_page.php?page_number=', ($curr_page + 1), '" onclick="gotoNextPage()">';
?>
This way it would work for via the ajax method but would also work (if not as smoothly and with a page reload) for people without javascript. It's a bit more work for you though...so hat's your choice!
Hope this makes sense. It's been a long day!

Update Database record using jQuery/PHP and a link and not a button

I am attempting to make a registration-like page for a small forum. To register for an event, I simply want the users to click a 'link'. (It doesn't have to be a link, but I don't want it to look like a button, so fancy CSS is acceptable).
On this page there will be a limited number of openings per event. (Simple database query to populate this list). The users will then select one of the openings by clicking on it. At this point, I'd like that opening to close and the page NOT to reload (just the Div that link is in).
I know PHP/MySQL very well. Actually, DOING the insertion of the data is easy. What I don't know how to do is the clicking the link and not reloading the entire page but still showing that opening is now closed.
I've looked at jQuery. It seems to have the ability to do the autoreload. Can anyone help me out?
The insert in the DB will be the user's forum name (they are logged into the forum, I have access to that variable within my script). That is all that will get inserted into the database.
Look at the Ajax methods. To update the database you probably must do a $.post. Something like this will work:
$("#myButtonId").click(function() {
$.post(serverurl, $("myformId").serialize(), function() {
// This will be called when the post is complete
$("#mydivId").html('update html here');
});
});

posting variables between windows in php

my title may not be clear. But basically what I want to do is:
There will be a link in my php form
<a href="somepage.php" target=_blank>Update</a>
when the user clicks on the link, a new browser window opens and allows him to select some options. There will be close button in that window. When he clicks on that 'close' button, there is a post form from where the parent window should get the selected value.
When I get that selected value from that child browser window, how I am going to refresh parent browser window to reflect what user has selected in child window?
Environment : PHP
Can anyone give me some idea?
As far as I know you can't refresh a parent window using PHP (or, more correctly, pure HTML). You will need a bit of Javascript in your onLoad event:
window.opener.location.reload(); // Refresh
- or -
window.opener.location.href = "targetpage.php"; // Redirect
You would be able to refresh a child window that was opened before from that page using a named target:
<a href="new_window.php" target="my_new_window_i'm_going_to_refresh">
(apostrphe for demonstration purposes only :)
repeated clicking on that link should refresh the child every time. It doesn't work the other way round, though.
In parent expect the refreshed values something like as:
parent.php?value=xxx
on popup, when user clicks close, you can always use window.opener.location = "parent.php?value=XXX" ... this will refresh the parent with the value selected.

Categories