-1

I am trying to pass a variable in an html field, I succeed in that task thank to this answer : https://stackoverflow.com/a/5422395

The scenario is a user click on : http://some.site.com/somePage.html?forename=Bob&surname=Jones

And the field forename is prepopulate on the page with Bob, but I encounter an issue with the answer mentionned earlier, if there is no variable in the url (e.g. http://some.site.com/somePage.html) what the user enter into the field forename will not be taken into account by the form.

How could I prefill the field only if the variable is present in the url and let the user input what he wants if not ?

Thank you for your help,

Community
  • 1
  • 1
Jon
  • 45
  • 3
  • Your question's unclear as it's missing code in regards to "how" it's being filled in; an HTML form? Plus, there's no PHP here. – Funk Forty Niner Jul 29 '16 at 13:27
  • 1
    You've been given answers, so ask them now. I've posted comments under them. If one of those worked for you, then consider accepting one of those answers in order to close the question and to be marked as solved. If they haven't solved the question, then you either need to elaborate on your question, and/or let them know why it failed. – Funk Forty Niner Jul 29 '16 at 13:29

2 Answers2

1

if(isset($_GET)) ... which is fairly simple. The isset function is very useful for knowing is a value is present somewhere. Let me know if you need more help.

Edited to give a full example:

<?php 
if(isset($_GET['forename']) && !empty($_GET['forename']))
{
    echo $_GET['forename'];
}
?>
Teddy Codes
  • 489
  • 4
  • 14
  • I didn't know exactly what values he was using. Sorry for the vague answer. – Teddy Codes Jul 29 '16 at 12:46
  • 3
    The problem with `isset()` Robert is, that even an empty GET request could be considered as being "set". Therefore an (additional) `!empty()` would be best. I.e.: Using `file.php?param` would be considered as "set". Example using a standard conditional `if (isset($_GET['param']) && !empty($_GET['param']) )`. That can easily be converted to a ternary operator. – Funk Forty Niner Jul 29 '16 at 13:12
  • 3
    Sidenote: checking if `$_GET` is set and not empty does **not** guarantee that there is a `$_GET['forename']`, but `!empty($_GET['forename'])` does. – FirstOne Jul 29 '16 at 13:45
  • I was attempting to give a broad example for the answer so it can be used multiple times in different applications. Thanks. I updated my answer though to make it more exact. – Teddy Codes Jul 29 '16 at 14:34
0

Write this in the value of the field.

if (isset($_GET['forename'])) echo $_GET['forename'];
TiFe Pariola
  • 3
  • 1
  • 2