Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...