Afternoon,
I have this function that works great and displays the results of a form submission to a user. But it works so well it shows all fields even if nothing has been selected. Is there a way to fix this so if the field value is null then don't show?
Thanks in advance
Laura
<? $url = '../ThankYou.php'; $text = "<h1>Your form submission:</h1><p></p>"; $space = ' '; $line = ''; foreach ($_POST as $key => $value) { $j = strlen($key); if ($j >= 50) {echo "Name of form element $key cannot be longer than 50 characters";die;} $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}--$space{$value}<p></p>$line"; $text .= $conc; $space = ' '; } echo $text; ?>
Well,
you can add a statement:
if(!$j == 0) { if ($j >= 50) {echo "Name of form element $key cannot be longer than 50 characters";} else { $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}--$space{$value}<p></p>$line"; $text .= $conc; $space = ' '; } }
Thanks for that, but it doesn't seem to make a difference. Still the same results.
Not sure if it would make a difference, but in leatherback's reply, wouldn't:
if($j != 0)
be more logical than:
if(!$j == 0)
?
Yeah, is a bit shorter. .
both [edit]should [/edit] have the same result though..
J.
Just noticed something..
shouldn't $j = strlen($key); be $j = strlen($value);
Are you really interested in the key of the $POST[] key? (The fieldname in the form) or in the value (The data submitted by the user)
Thanks for all the help. Actually, I probably don't even need the
if ($j >= 50) {echo "Name of form element $key cannot be longer than 50 characters";} else
statement, for brevity and clarity I should take it out. But it still shows the empty fields that are submitted, even with the changes you both suggested.
Hmmmm. I'll have to go get more coffee to figure this out. Thanks again.
Oh so simple once you hit the expresso 🙂
I changed
$j = strlen($key);
to
$j = strlen($value);
and now it works perfectly. Thanks again for all the help
Or, to address more directly the relevant condition, you could use:
if (!empty($_POST[$key]))
BTW, these are not the same:
if ($j != 0) // and if (!$j == 0)
Although they will often resolve to the same value, often they won't.
Thanks for that. I've run into something else related. Multiple selections show up as "array"
I'm trying to find an elegant and scalable solution, less is more, but I keep getting string to array conversion errors.
Any shove in the right direction would be appreciated.
Thanks so much
Originally posted by lmayer4 Oh so simple once you hit the expresso 🙂 I changed $j = strlen($key); to $j = strlen($value); and now it works perfectly. Thanks again for all the help Laura [/B]
Originally posted by lmayer4 Oh so simple once you hit the expresso 🙂 I changed
Laura [/B]
see my last post...
J
Yup, see I needed the coffee.
Thanks for all your help.