javascript等待php完成功能并刷新页面 [英] javascript waiting php to complete function and refresh page

查看:96
本文介绍了javascript等待php完成功能并刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

  1. 我从下拉菜单中选择了网站的币种,并使用onchange事件触发了javascript函数changecurrency()
  2. 通过使用jquery的changecurrency()函数,我会触发php文件,以便根据下拉选择的值更新会话变量
  3. 我需要刷新页面才能根据所选货币计算正确的价格

一切正常,直到最后一点,因为页面刷新无法按预期方式工作...我认识到,在更新会话变量的php函数完成之前,javascript正在刷新页面.我试图使用回调函数来告诉javascript等待php完成服务器工作,但没有成功. 代码如下:

HTML:

<select id='selectcurrency' onchange='changecurrency()'>
    <option value='1' selected >RUB</option>
    <option value='2' >USD</option>
    <option value='3' >EURO</option>
    <option value='4' >GBP</option>
    <option value='5' >TRY</option>
</select>   

echo "<div id='div_session_write'> </div>";

Javascript:

function changecurrency(){
        updatesesion(function() {
        var url      = window.location.href;
        window.location.href = url;
    });    
}

function updatesesion(_callback){
    var myselect = document.getElementById("selectcurrency");
    var x =(myselect.options[myselect.selectedIndex].value);
    jQuery('#div_session_write').load('update_currency.php?val=' + x );
    _callback();    
}

PHP文件"update_currency.php"以更新会话变量

ob_start();
session_name ("Mysite");
session_set_cookie_params (0,"/","mysite.com",false, true);    
session_start();   

if (isset($_GET['val'])) {
    $_SESSION['currency'] = $_GET['val'];
}
session_write_close();
ob_end_flush();

最初,我尝试使用header('location:..')函数从php文件中重新加载页面,但是什么也没有发生,因此,我转而使用javascript刷新页面,但是仍然没有成功. 有什么想法我做错了吗? 谢谢!

解决方案

将回调函数作为第二个参数传递:

jQuery('#div_session_write').load('update_currency.php?val=' + x, _callback);

这样,当请求完成加载时,它会由jQuery自动调用.请参阅文档以获取更多信息: http://api.jquery.com/load/

I have following case:

  1. I have drop down menu from where I select currency for a website and trigger javascript function changecurrency() using onchange event
  2. With changecurrency() function using jquery I'm trigger php file in order to update session variable based on dropdown selected value
  3. I need to refresh page to calculate correct prices based on selected currency

Everything works fine till last point as page refreshing does not working as expected... I recognized that javascript is refreshing page before php function to update session variables is completed. I tried to use callback function to tell javascript to wait php to complete server work but without success. Code is as follow:

HTML:

<select id='selectcurrency' onchange='changecurrency()'>
    <option value='1' selected >RUB</option>
    <option value='2' >USD</option>
    <option value='3' >EURO</option>
    <option value='4' >GBP</option>
    <option value='5' >TRY</option>
</select>   

echo "<div id='div_session_write'> </div>";

Javascript:

function changecurrency(){
        updatesesion(function() {
        var url      = window.location.href;
        window.location.href = url;
    });    
}

function updatesesion(_callback){
    var myselect = document.getElementById("selectcurrency");
    var x =(myselect.options[myselect.selectedIndex].value);
    jQuery('#div_session_write').load('update_currency.php?val=' + x );
    _callback();    
}

PHP file "update_currency.php" to update session variable

ob_start();
session_name ("Mysite");
session_set_cookie_params (0,"/","mysite.com",false, true);    
session_start();   

if (isset($_GET['val'])) {
    $_SESSION['currency'] = $_GET['val'];
}
session_write_close();
ob_end_flush();

Originally I tried to reload page from the php file using header('location: ..') function but nothing was happening, in this reason I moved to variant to use javascript to refresh page, but still without success. Any ideas what I'm doing wrong? Thanks!

解决方案

Pass your callback function as the second parameter:

jQuery('#div_session_write').load('update_currency.php?val=' + x, _callback);

That way, it is automatically called by jQuery when the request finishes loading. See the documentation for more info: http://api.jquery.com/load/

这篇关于javascript等待php完成功能并刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆