Getting value GET OR POST variable using JavaScript?

前端 未结 9 1480
一个人的身影
一个人的身影 2020-11-27 05:59

How to get value of the get or post variable on page load using JavaScript?

相关标签:
9条回答
  • 2020-11-27 06:33

    With little php is very easy.

    HTML part:

    <input type="text" name="some_name">

    JavaScript

    <script type="text/javascript">
        some_variable = "<?php echo $_POST['some_name']?>";
    </script>
    
    0 讨论(0)
  • 2020-11-27 06:33
    // Captura datos usando metodo GET en la url colocar index.html?hola=chao
    const $_GET = {};
    const args = location.search.substr(1).split(/&/);
    for (let i=0; i<args.length; ++i) {
        const tmp = args[i].split(/=/);
        if (tmp[0] != "") {
            $_GET[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " "));
            console.log(`>>${$_GET['hola']}`);
        }//::END if
    }//::END for
    
    0 讨论(0)
  • 2020-11-27 06:37

    The simplest technique:

    If your form action attribute is omitted, you can send a form to the same HTML file without actually using a GET HTTP access, just by using onClick on the button used for submitting the form. Then the form fields are in the elements array document.FormName.elements . Each element in that array has a value attribute containing the string the user provided (For INPUT elements). It also has id and name attributes, containing the id and/or name provided in the form child elements.

    0 讨论(0)
  • 2020-11-27 06:40

    This is my first Answer in stackoverflow and my english is not good. so I can't talk good about this problem:)

    I think you might need the following code to get the value of your or tags.

    this is what you might need:

    HTML

    <input id="input_id" type="checkbox/text/radio" value="mehrad" />
    
    <div id="writeSomething"></div>
    

    JavaScript

    function checkvalue(input , Write) {
      var inputValue = document.getElementById(input).value;
      if(inputValue !="" && inputValue !=null) { 
        document.getElementById(Write).innerHTML = inputValue;
      } else { 
        document.getElementById(Write).innerHTML = "Value is empty";
      }
    }
    

    also, you can use other codes or other if in this function like this:

    function checkvalue(input , Write) {
      var inputValue = document.getElementById(input).value;
      if(inputValue !="" && inputValue !=null) { 
        document.getElementById(Write).innerHTML = inputValue;
        document.getElementById(Write).style.color = "#000";
      } else {
        document.getElementById(Write).innerHTML = "Value is empty";
      }
    }
    

    and you can use this function in your page by events like this:

    <div onclick="checkvalue('input_id','writeSomthing')"></div>
    

    I hope my code will be useful for you

    Write by <Mehrad Karampour>

    0 讨论(0)
  • 2020-11-27 06:46

    You can only get the URI arguments with JavaScript.

    // get query arguments
    var $_GET = {},
        args = location.search.substr(1).split(/&/);
    for (var i=0; i<args.length; ++i) {
        var tmp = args[i].split(/=/);
        if (tmp[0] != "") {
            $_GET[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " "));
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:47

    /**
    * getGET: [Funcion que captura las variables pasados por GET]
    * @Implementacion [pagina.html?id=10&pos=3]
    * @param  {[const ]} loc           [capturamos la url]
    * @return {[array]} get [Devuelve un array de clave=>valor]
    */
    const getGET = () => {
        const loc = document.location.href;
    
                // si existe el interrogante
                if(loc.indexOf('?')>0){
                // cogemos la parte de la url que hay despues del interrogante
                const getString = loc.split('?')[1];
                // obtenemos un array con cada clave=valor
                const GET = getString.split('&');
                const get = {};
    
                // recorremos todo el array de valores
                for(let i = 0, l = GET.length; i < l; i++){
                    const tmp = GET[i].split('=');
                    get[tmp[0]] = unescape(decodeURI(tmp[1]));
                }//::END for
                return get;
            }//::END if 
    }//::END getGET
    
    /**
    * [DOMContentLoaded]
    * @param  {[const]} valores  [Cogemos los valores pasados por get]
    * @return {[document.write]}       
    */
    document.addEventListener('DOMContentLoaded', () => {
        const valores=getGET();
    
        if(valores){
                // hacemos un bucle para pasar por cada indice del array de valores
                for(const index in valores){
                    document.write(`<br>clave: ${index} - valor: ${valores[index]}`);
                }//::END for
            }else{
                // no se ha recibido ningun parametro por GET
                document.write("<br>No se ha recibido ningún parámetro");
            }//::END if
    });//::END DOMContentLoaded

    0 讨论(0)
提交回复
热议问题