使用Ajax更改网站语言 [英] using ajax for change website language

查看:61
本文介绍了使用Ajax更改网站语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的PHP html代码来更改我的网站语言,我需要使用Ajax在选择语言后不重新加载,但是老实说我以前从未使用过Ajax,而且我也不知道如何使用它.

I Have very simple PHP html code for change my website language and I need to use Ajax to not-reload after select language but honestly I never used ajax before and I don't have any idea how to use that.

我用Google搜索它,发现了一些代码,但是失败了.

I google it and found some code but I fail.

HTML:

 <form action="" method="get">
      <input type="submit" value="per" id="per" name="per">
      <input type="submit" value="eng" id="eng" name="eng">
</form>

PHP:

function lang()
{
    $lang = 'per';
    if (isset($_GET['per']))
        return $lang = 'per';
    else
        return $lang = 'eng';
}

Ajax:

$.ajax({
    type: "GET",
    url: 'index.blade.php',
    data: {name: 'per'},
    success: function(data){
               alert(data);
               window.location.reload();
             }
    });

所有代码都位于名为

index.blade.php

当我单击按钮时,php代码运行正常,只需要ajax不重新加载页面

php code working fine just need for ajax to not-reload page when I click buttons

推荐答案

尝试一下:

html:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<form action="" >
    <button type="submit" id="button1">Click
    </button>
</form>

<script type="text/javascript" src="1.js"></script>
<!--<script type="text/javascript" src="2.js"></script>-->
</html>

js:

document.getElementById("button1").addEventListener("click",function(e){
    //alert("hello");
    e.preventDefault();  //a button's default behavior is to submit the form which this function prevents
        $.ajax({
        url:"example.php",
        success:function(result){

            alert(result);
            location.href=location.href; //here you can specify where you want to get your ajax call to redirect to.
        }

    })
    return false;

})

php文件:

<?php
echo "Hello world";
?>

希望这就是您想要的!

这篇关于使用Ajax更改网站语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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