1

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 ?

12
  • Then use a conditional statement or better yet, use a ternary operator Commented Jan 21, 2014 at 23:56
  • 1
    So, why use a GET method in the first place; why not just go with POST? That way it's not going to show anything in the address bar. Is there a special reason for you to use GET? Commented Jan 22, 2014 at 0:03
  • 1
    @Fred-ii- I concur. By posting the form you avoid ugly urls and can handle empty variables accordingly Commented Jan 22, 2014 at 0:25
  • 1
    Thanks Mike. Yet, the OP made a mention to the affect that is using it for dynamic content. If it were up to me, I would just use conditional statements, because I for one, never ever use GET, it risks in opening up the proverbial "can(s) of worms", including potential security issues. @MikePurcell Commented Jan 22, 2014 at 0:28
  • 1
    (Assuming you're using a DB with this), would be very careful as to how your queries are done and which functions you're using. That's IF you're using a DB. You wouldn't want anyone putting in some pretty fancy hacking characters to DROP your table. @Guillaume Commented Jan 22, 2014 at 0:36

6 Answers 6

10

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes ! It works perfectly, eventhough I have to use some more JS. This solution suits me quite well, thank you very much ! :)
Good work Seff. To add to this Guillaume, if you don't want the $info1 = etc. to initially show up, you can change/modify echo '$info1 = '.$_GET["info1"].'<br />'; to if(!empty($_GET["info1"])) { echo '$info1 = '.$_GET["info1"].'<br />';} @Guillaume --- then do the rest for the others.
3

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

2 Comments

OK for seperating the PHP and HTML (my real code was more complicated). Where do I have to add this piece of JS ? My knowledge is a bit limited, sorry.
Assuming your using jquery, insert it on DOM loaded. Added code
1

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.

3 Comments

Wouldn't location.href=myQueryString replace your whole URL with the query string? I'm guessing you meant to build a whole URL and set it there...
@IMSoP You can make a link or javascript redirect that goes to just the query string with a ? as the first character. For example, location.href="?foo=1&bar=2&test=3" would work just fine. If you don't include the page name, the current page is assumed. If you wanted to go to another page, then yes, you would need to include that page name.
Ah, fair enough. I try to act as though there are no valid URL notations other than absolute and root-relative because they're such a cause of accidents, so I didn't really think through what a leading ? would mean.
0

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.

1 Comment

GET was a requirement for the application so I cannot do that. I need the parameters to be displayed in the URL.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.