Empty GET variables displaying in the URL

2019-01-27 06:23发布

问题:

Here is my form :

<?php

echo '<form method="get" action="" name="formulaire">';
echo '<input type="text" name="info1" title="" value="" />';
echo '<input type="text" name="info2" title="" value="" />';
echo '<input type="text" name="info3" title="" value="" />';
echo '<input type="submit" value="Envoyer" />';
echo '</form>';

echo '$info1 = '.$_GET["info1"].'<br />';
echo '$info2 = '.$_GET["info2"].'<br />';
echo '$info3 = '.$_GET["info3"].'<br />';

?>

My problem is that after submitting, all the variables are displayed in the URL, even if they are empty.

I would like the non-empty variables ONLY to be displayed in the URL.

Is there a way of doing that with PHP ?

回答1:

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>


回答2:

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



回答3:

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.



回答4:

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.



回答5:

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


回答6:

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



标签: php forms get