Empty GET variables displaying in the URL

前端 未结 6 748
星月不相逢
星月不相逢 2020-12-18 17:04

Here is my form :

\';
echo \'

        
相关标签:
6条回答
  • 2020-12-18 17:08

    Its not a PHP problem, Its whats populating the form. All values in the form are sent. You would need to use Javascript to check for that.

    If a form item is set to disabled, It wont be sent for example

    <script>
    $(function() {
         $('form').submit(function() {
            $(':input[value=""]').attr('disabled', true);
         }
    });
    </script>
    

    http://api.jquery.com/ready/ Should work if placed anywhere on the page

    Also you dont need to echo HTML

    <form method="get" action="" name="formulaire">
      <input type="text" name="info1" title="" value="" />
      <input type="text" name="info2" title="" value="" />
      <input type="text" name="info3" title="" value="" />
      <input type="submit" value="Envoyer" />
    </form>
    <?php
    echo '$info1 = '.$_GET["info1"].'<br />';
    echo '$info2 = '.$_GET["info2"].'<br />';
    echo '$info3 = '.$_GET["info3"].'<br />';
    
    ?>
    

    Seperates the PHP and HTML

    0 讨论(0)
  • 2020-12-18 17:10

    The browser will form the URL when you click your submit button. There is no way to change the functionality of a browser with PHP. You can try changing the form method to POST though.

    Alternatively, you can use JavaScript to intercept submitting the form, and generate the URL to forward to with JavaScript.

    0 讨论(0)
  • 2020-12-18 17:12

    Like others have said, the solution is to use Javascript to change the form when its submited. Here is your example with a javascript function that does that:

    <html>
    <head>
    <script type="text/javascript">
    function myFunction()
    {
        var myForm = document.getElementById('form-id');
        var allInputs = myForm.getElementsByTagName('input');
        var input, i;
    
        for(i = 0; input = allInputs[i]; i++) {
            if(input.getAttribute('name') && !input.value) {
                input.setAttribute('name', '');
            }
        }
    }
    </script>
    </head>
    
    <form id="form-id" method="get" action="" name="formulaire" onsubmit="myFunction()">
    <input type="text" name="info1" title="" value="" />
    <input type="text" name="info2" title="" value="" />
    <input type="text" name="info3" title="" value="" />
    <input type="submit" value="Envoyer" />
    </form>
    
    <?php
    echo '$info1 = '.$_GET["info1"].'<br />';
    echo '$info2 = '.$_GET["info2"].'<br />';
    echo '$info3 = '.$_GET["info3"].'<br />';
    ?>
    </html>
    
    0 讨论(0)
  • 2020-12-18 17:12

    we can easily make the url empty without javascript or an other plugin here the code put it on the top of the code after session code but note we have to only use post method . it is safe and not more complex

    header("Cache-Control: no cache");
        session_cache_limiter("private_no_expire");

    cheers

    0 讨论(0)
  • 2020-12-18 17:18

    Probably could be optimized, but here's my suggestion, using PHP:

    if (!isset($_GET['clean'])) {
        $_GET['clean'] = 1;
        redirect($_SERVER['SCRIPT_URL'] . '?' . http_build_query(array_filter($_GET)));
    }
    unset($_GET['clean']);
    
    • This is assuming processing is done on the same page
    • redirect() being a custom redirection function
    0 讨论(0)
  • 2020-12-18 17:24

    It can't be done with php. That is standard behavior of the html form. If you wanted to do this, you would have to use javascript and onsubmit of the form, loop over and either remove empty elements or build a query string and location.href=myQueryString.

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