So I have a form on PHP/HTML page. User submitss it to that same PHP/HTML page. So now PHP page I will have $_POST data. I want to when page is refreshed opnt some popUp browser windows which url's will be relative to users POST request. like www.example.com/bal-bla-bla.php? id=$_POST['StreamId']
Include some <script> elements with window.open calls in them in the response … then watch as every popup blocker in the world blocks them.
if ( isset($_POST['submit']) ) {
echo '<script>window.open ("'.$_SERVER['PHP_SELF'].'myplayer.php?stream_id='.$_POST['StreamId'].'","myplayer");</script>';
}
edited:
you can always display a message before open the window that advice the user to accept this new window!
var flag = confirm(" This window is not an ADV! ;-) ");
if (flag)
window.open("'.$_SERVER['PHP_SELF'].'","myplayer");
You would need to do this client side in javascript.
You could use window.open() in a document.onload event handler.
However, chances are if the user has a pop-up blocker this will be blocked.
Related
So currently my website works like this; you post an update and through iframe your update gets added to the database and then shown in a list below. But the problem is that when you clicked "Submit" the text you wrote is still shown in the textarea because the website doesn't update completely. I have tried to have "onsubmit" and "onclick" but both remove the content of the textarea before it gets added to the database so it displays an empty message.
What should I do in order to delay it just a second or how do I make it wait for the iframe to "send" data to my PHP-script?
Give your textarea an id and supposing that iframe is an element of the page that contains the textarea you have done the following in the iframe page:
<?php
//code should be done after db add
?>
<script>
o = parent.document.getElementById('textareaID');
o.value = '';
</script>
<?php
//the end of code or something else
?>
You are able to see those demos on jsbin:
http://jsbin.com/ulOyiVo/1 The page with iframe. Supply the textarea with any text and then click on simulate submit link
http://jsbin.com/EyuBeLo/1/ The iframe page
If your only problem is to have a delayed response, you could trigger a setTimeout function to your onClick, with the given setTimeout:
setTimeout(
function() {
alert('hello');
},1250 //in milliseconds
);
You can define a click or a submit event using jQuery and send a request to your server. You can handle the event when the server responded using a callback. In that callback you need to do whatever it is needed to do. Using setTimeout in this case is an unnecessary hack. You will either set up a big time to wait harming the user experience or in case the page responds later than the specified time your page will work unexpectedly. So, instead of that try defining an event.
I have a page where if you click on a link, it exposes a div that using ajax displays content from a dbase.
After a user edits this content on the server, I'd like to use PHP to return the user to that page. This is no problem using a redirect
header("location:page.php")
However, when the user comes back to the page, ideally, I'd like to have the content in the div open automatically so the user can immediately see edits without having to find the link to open the div and click on it.
Is this possible, either with something in the url to fire the javascript or alternaively, when you load the page with a certain parameter, triggering javascript to open the div.
The code to open the div is a simple javascript call:
View Content
showDiv just uses ajax to display something from the server using responsetext.
Thanks for any suggestions
header("location:page.php?show=1")
Then in page.php body tag:
<body <?php if($_GET['show']==1) { ?>onload="showDiv()"<?php } ?>>
I'm trying to learn how make an AJAX script
for a LIKE button, on my website. I have the following questions:
if i'm sending 1 variable.... id.. I do this
data: "action=vote_up&id="+(this).attr("id")",
is this syntactically correct if i'm sending two variables id and id1 ?
data: "action=vote_up&id="+(this).attr("id")&id1="+(this).attr("id1")",
2) What goes into the href attribute? The php page or the AJAX?
<img scr="like.png">
3) which is run first.. The php page or the AJAX.
4) Is it mandatory for me to use jQuery or Pure Javascript for running AJAX
thanks for your time and patience. I most appreciate it.
1) Yes, you could simple undestand it as a PHP-Get request to a script, so multiple vars are possible, like Adam mentioned.
2) For backwards compatibility you should just link to a PHP/whatever-Script that provides the same functionality but doesn't rely on javascript (Not everyone has js enabled). In your javascript you just disable the defult click actione ( see: http://api.jquery.com/event.preventDefault/ ) otherwise it you only want to allow the like funktionality if js is enabled than you could just link to the page anchor '#'.
3) The page runs first. It is progressed by the server and than sent to your browser. In the browser the recieved javascript will start its action.
4) Everything you are using in jquery is based on simple javascript functions, but jquery is much more comfortable ;) The equivalent to the ajax method of jquery is XMLHttpRequest ( http://www.w3schools.com/xml/xml_http.asp )
Here is a idea, hope it helps.
If handle_vote.php is the URL responsible for the handle the up vote, you must do two things:
the a href is the URL with the query string for the up vote, your data, is this case. It must be generated for you server application. It will be used in case of no javascript.
you should put you event to handle the up vote in the a onclick event, to send the ajax request, and use the preventDefault jQuery function to avoid the default event. In this case, a href will never be used, the js will suppress the link click.
A code sample will be almost like this, in you php page:
<a class="like" href="handle_vote.php?action=vote_up&id=<?php echo $post_id; ?>"><img src="like.png"></a>
And it as your jQuery script:
$(function() {
$('a.like').on('click', function(e) {
e.preventDefault();
$.get($(this).attr("href"));
});
});
You can personalize as you like, it is only the idea of how to do it.
<img scr="like.png"> put onclick event on that link, and make AJAX request` to increment count, on success response update count clicks on button. And you forgot about one thing, you should save the state of that button. Because one user can go to your site and click 1000 times on it.
I know there a bunch of topics on this, but I couldn't determine what to do based on what I read in the other topics.
I have a page "abc.php". The user can do a search which then populates a form with 2 ajax requests. Then if the user navigates to another page and then clicks BACK to "abc.php", the contents of the form is not complete because the ajax doesn't run. Is there a way to make this happen?
Modify your URL when you do the ajax by adding the search terms there after a hash (e.g. http://example.com/search.php#search-terms-here)
Then when the page is loaded, read the search terms back from the URL.
This is a very nice article / tutorial on enabling the back-button using jQuery.
Using history.js, the following function 'listens' to changes in the url bar, and calls a function to load the appropriate page:
History.Adapter.bind(window,'statechange',function(){
var State = History.getState();
page(State.url);
});
function page(url) {
//AJAX
}
Now whenever you want to change the page, you call:
History.pushState({state:X}, "Page Title", "Page Url");
This will update the browser's url bar, and automatically call page(State.url) for the new url; and all the browser features like forward/back button, bookmarks, etc... should work.
popup when the browser closes
I am using onbeforeunload event
<script>
function showPopup()
{
urlstring = "http://www.mydomain.com/popup.php";
window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");
}
</script>
<body onbeforeunload="showPopup(); ">
but it also show popup when ever I hit back space and page refresh.
I want show popup only when browser close and not show when hit back space.
but it shows all conditions.
Please suggest me any other solution for this.
This is showing a popup every time because the onbeforeunload event is not aware of the domain you are leaving, it is only aware of the current page unloading for a new one.
Well , here's the bitter truth...
onbeforeunload is fired on page refresh and browser close.
The onbeforeunload event is fired every time the page is about to unload.
Which includes
Clicking on a link
Submitting a form
Closing the browser(or tab)
Refreshing the page
You might find this helpful
http://www.webdeveloper.com/forum/archive/index.php/t-36808.html
<script>
function showPopup(){
urlstring = "http://www.mydomain.com/popup.php";
window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");
}
window.onunload= showPopup();
</script>
Note: This is DOM Level 0. Not part of any standard
EDITED: Note Infotekka's link to the document.onunload will also work appropriately for this problem.
You can't.
Your page, including its events, is not aware of other tabs, other pages or the browser itself for that matter. It also doesn't know why it is unloaded. The onunload event is the closest you will get, but it fired everytime when your page is unloaded, so also on navigation.