Skip to content Skip to sidebar Skip to footer

Using Ajax For Change Website Language

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 an

Solution 1:

Try this:

html:

<html><head><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script></head><formaction="" ><buttontype="submit"id="button1">Click
    </button></form><scripttype="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.
        }

    })
    returnfalse;

})

php file:

<?phpecho"Hello world";
?>

Hope this is what you are looking for!

Solution 2:

I suggest studying a few tutorials on Ajax, I will try to briefly touch the subject here.

First of all, when doing Ajax, you basically call a web page or script (an actual url, not a function written in PHP). The result that you get (HTML, XML, CSS, JSON, JPG), you can use in your code, insert ii in the DOM, change the document, etc.

In my opinion, changing the language is a site-wide action that should probably be implemented as a normal call. Genearaly, if you change the language, then the whole page should be translated, from the top (title) to the last bit of text down before the closing body.

If you just need to change a small portion of the web page, please see the URL jQuery get

The page uses an example

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

that performs what you want, just change the url. Hope I helped a bit.

Post a Comment for "Using Ajax For Change Website Language"