Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

php - Empty GET variables displaying in the URL

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...