Java script looks for STRING in HTML HEADER

Asked

Viewed 49 times

0

Good morning friends! I have a question regarding an operation that I need to perform after sending the data of a form. I have a form that your fields to fill in and a function to "submit" this information typed: My doubt is the following: after sending this information, a page will be loaded, I need a js (or any other code that fits) that reads the header of this new page and looks for the following value: "Cookie=1" If it finds this value, then I want the user to be directed to another URL.

Any idea?

ps: The code below is the form submission, after the execution of this I want "something" to read the header in search of the string I mentioned above.

submitInputValue: "Enviar",
        setFields: function(e) {
            switch (e) {
                case "login":
                    this.loginFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "pwdWillExp":
                    this.pwdWillExpFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "changePwd":
                    this.changePwdFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "pwdNotChanged":
                    this.pwdNotChangedFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                case "sessionExpired":
                    this.sessionExpiredFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "acessPrivate":
                    this.accessPrivateFields = !0, this.submitInputValue = "Acessar";
                    break;
                case "solicitPwdChg":
                    this.solicitPwdChgFields = !0, this.submitInputValue = "Alterar a Senha";
                    break;
                default:
                    this.loginFields = !0, this.submitInputValue = "Acessar"
            }

1 answer

0

You must use the PHP or JS functions that work with Cookies.

Basically you need to Write and Read a Cookie, so see the example below and apply your need.

Example with PHP:

To Write down a cookie make:

<?php

//O ultimo parâmetro é o tempo que o Cookie deve ficar no navegador.
setcookie("name_do_cookie", "value", time()+60*60);

?>

To read the information stored in the Cookie:

<?php
echo $_COOKIE["name_do_cookie"];
?>

For only upgrade do:

<?php
setcookie("name_do_cookie","novo_valor");
?>

To delete do:

<?php
unset($_COOKIE["name_do_cookie"]);
/* ou */
setcookie("name_do_cookie","",time()-1);
?>

In JS you can use the functions below:

To create:

<script>
//Criar
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
} 

//Para ler
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 

function checkCookie() {
    var username=getCookie("username");
    if (username!="") {
        alert("Welcome again " + username);
    }else{
        username = prompt("Please enter your name:", "");
        if (username != "" && username != null) {
            setCookie("username", username, 365);
        }
    }
} 
</script>

Reference: http://www.w3schools.com/js/js_cookies.asp

http://php.net/manual/en/features.cookies.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.