$("#profile_bar").mouseover(function(){
<?php $_SESSION['sessionasdf'] = 'asdf'; ?>
});
Hello! I have been busy with this for an hour, but I'm deadbrain now. Can someone help me out or give me a hint? Is the function I wrote above, even possible?
Thanks in advance!
You need an Ajax Request to do this. You can't simply start a session in a script that's already been loaded.
$("#profile_bar").mouseover(function() {
$.ajax({
url: "sessionStartPage.php",
cache: false,
success: function(data) {
alert("session has begun. Refreshing page now");
location.reload(); //reload the page to load session variables
}
});
});
As I said you have to use a technique called AJAX..
So its time to start learning:)
http://www.smashingmagazine.com/2008/10/16/50-excellent-ajax-tutorials/
PHP run on server, JQuery(the javascript) run on browser.That's different.
And you can start the session at every page on server, rather than by the event on browser.
Related
I'm incredibly new to PHP, so I'll try and be as clear as possible. Through a bit of trial and error, I created a simple form on a website I manage where people can provide a few bits of information (about four lines total). In turn, I will receive an email using ()mail with the information, which I intend to use for remarketing purposes.
The problem? It takes a good 20-30 seconds for the person who filled out their info to be taken to the Thank You page. I still end up receiving the email, but people are definitely not going to wait around for the Thank You page to load before closing the window. This is obviously not good if I'm planning on tracking conversions.
Any suggestions? A friend suggested I set up a MySQL database to collect the info, but that's way beyond my level of expertise. Are there alternatives that can help?
Many thanks.
If the form submits to PHP you could use an AJAX script of which runs the PHP page. You can setup BeforeSend to show a div that simply says "Loading please wait..." and the user is more likely to wait. I'm not saying this is the best solution but its one.
AJAX Script:
<script>
$(document).ready(function() {
$('#submitBtn').click(function() {
var variable1=$("#variable1").val();
var variable2=$("#variable2").val();
var dataStr = 'variable1='+variable1+'&variable2='+variable2;
if($.trim(variable1).length>0 && $.trim(variable2).length>0) {
$.ajax({
type: "POST",
url: "process.php",
data: dataStr,
beforeSend: function(){ $("#submitBtn").val('Loading Please wait...');},
success: function(data){
if (data!="success") {
$("#submitBtn").val('Submit')
} else {
window.location.href = "http://example.com/thankyou";
}
}
});
} return false;
});
});
</script>
I've provided you with an AJAX example above. In the PHP script you simply need to get it to echo "success" if the email is sent. If the script ends and nothing is sent back or something else is echo'd then it will set the submit button back to "Submit".
I recommend you create a div below the submit button of which is empty by default or not visible but if the AJAX fails it is shown.
I make multiple AJAX calls on a web page,and they take a lot of time to complete (5 minutes). During those calls, if I try to go to another page on my website (with a new window or not), I must wait for the AJAX calls to finish before the page loads.
I make the calls asynchronously:
ajax_seek=$.ajax({
type: 'POST',
url: url_tracks,
data: { youtubes : tab_tracks },
sucess:function(a){
console.log(a);
}
});
Anybody have an explanation ?
So like CBroe said the answer was to put "session_write_close();" in my php file wich is called
with ajax. And it worked :)
So I have my load script in jQuery:
function load() {
$.ajax({
url: '/urltoload.php',
cache: false,
success: function(html){
//do something
}
}
Using a setInterval, how can I check if a user is not on the on the page?
If I have correctly understand your question, you can check if the user "is on" the page and when leave it with the following code:
$(document).ready(function(){
$([window, document]).focusin(function(){
//Your logic when the page gets active
}).focusout(function(){
//Your logic when the page gets inactive
});
});
Another solution, maybe a little more "intricated", is to check the mouse movements (http://api.jquery.com/mousemove/#fn) each N seconds.
If you have cross-browsing issue, you can check this answer that gives a good workaround for differents browsers: https://stackoverflow.com/a/1060034/1720344
UPDATE
The pseudocode to a timeout function is this:
setTimeout(function() {
// Do something after 2 seconds
}, 2000);
By your comment, I think is best to "around" the sub-function and not the $(document).ready() (I haven't done before, timeouting the document.ready, but you can try and see what it happens ;) - I believe that this function is simple called after N seconds from the document.ready)
With a timeout of 2 seconds, you can do something like that (but I haven't tested it):
$(document).ready(setTimeout(function(){
$([window, document]).focusin(function(){
//Your logic when the page gets active
}).focusout(function(){
//Your logic when the page gets inactive
});
}, 2000));
With $(window).focus() you could check if the browser is active.
$(window).focus(function(){
// interval here
});
i need a a script that will refresh the functions:
$ping, $ms
every 30 seconds, with a timer shown,
i basicly got this script:
window.onload=function(){
var timer = {
interval: null,
seconds: 30,
start: function () {
var self = this,
el = document.getElementById('time-to-update');
el.innerText = this.seconds;
this.interval = setInterval(function () {
self.seconds--;
if (self.seconds == 0)
window.location.reload();
el.innerText = self.seconds;
}, 1000);
},
stop: function () {
window.clearInterval(this.interval)
}
}
timer.start();
}
but it refreshes the whole page, not the functions i want it to refresh, so, any help will be appriciated, thanks!
EDIT:
I forgot to mention that the script has to loop infinatly
This here reloads the whole page:
window.location.reload();
Now what you seem to want to do is reload portions of the page, those portions having been generated by php functions. Unfortunately php is server side so that means you cant get the client browser to run php. Your server runs the php to generate stuff that browsers can understand. In a web browser open a page you made using php and choose to view source and you'll see what I mean.
Here's what you'll need to do:
Make your two functions ping and ms accessable via ajax
Instead of window.location.reload() do a call to jQuery.ajax. on success write to your page
Here's what I think would be the ideal way of dealing with this... I haven't seen the php side of your problem but anyway:
make a file called ping.php and put all your ping function code in there. ditto for ms
in your original php file that called those functions, make a div at each point where you wanted a function call. Give them appropriate ids. Eg: "ping_contents" and "ms_contents"
You can populate these with some initial data if you want.
In your js put in something like this:
jQuery.ajax(
{
url : url_of_ping_function,
data : {anything you need},
type : 'POST', //or 'GET'
dataType: 'html',
success : function(data)
{
document.getElementById("ping_contents").innerHTML = data;
}
});
do another one for the other function
What you want is AJAX, Asynchronous JavaScript and XML
You can use jQuery for that.
I can put an example here, but there is a lot of information to be found on the internet. In the past I wrote my own AJAX code, but since I started using jQuery, it's all a lot easier. Look at the jQuery link I provided. There is some usefull information. This example code might be the easiest to explain.
$.ajax({
url: "test.php"
}).done(function() {
alert("done");
});
A some moment, for example on a click on a button, the file test.php is executed. When it's done, a alert box with the text "done" is shown. That's the basic.
I'm doing a long poll method chatroom. But it seems that, when a long poll occurs and I refresh the page in chrome OR i try to send another async request everything times out (i.e i cant load my domain again until i close/reopen the browser).
My client side code is:
$(document).ready(function() {
setTimeout(
function () {
longPollForMessages();
},
500
);
});
function longPollForMessages()
{
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
$('#chat_messages').append('<div>'+data.messages+'</div>');
longPollForMessages();
}
});
}
And my serverside:
while(true) {
$messages = $db->getMessages();
if (!$messages || sizeof($messages)==0) {
sleep(1);
} else {
echo '{"message":'.json_encode($messages).'}';
die();
}
}
Any ideas? Assume no syntax errors.
I can see you have already answered your own question, but I recently had a similar problem and found another way to handle it is to disable the setTimeout on ajax call, then restart it on success. That way you aren't pinging your server for information when it isn't ready to give it.
I figured it out from this question: stackoverflow.com/questions/4457178/… - php locks a given session until the page is done loading so the second ajax call wasn't able to go through. You have to release the lock by calling session_write_close();