Home » Php » Waiting until a variables value isset to display it php

Waiting until a variables value isset to display it php

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a variable $infoCollectedAndSold that is being set inside a form (its values are whatever is being checked in the checkbox).
I want to display this variable, once its values are set. Currently the echo is displaying nothing until i submit the form and then the values are displayed from the variable. Is it possible to display the information inside the variable without submitting the form?

//Set variable to the form inputs
$infoCollectedAndSold = "";
if (!empty($_POST['infoCollectedAndSold'])) {
    foreach ($_POST['infoCollectedAndSold'] as $value) {
        $infoCollectedAndSold .= $value . ', ';
    }
}

//Display variable content (Does not display until form is submitted)
<?php
if(isset($infoCollectedAndSold)){
   echo '<h3>'.$infoCollectedAndSold.'</h3>';
}
?>
How to&Answers:

$infoCollectedAndSold is always set as the first line of your code. You have to check it against an empty string if you don’t want the h3 tag to display.

if($infoCollectedAndSold !== "")

If you instead want to print the h3 regardless of its initial emptiness and then populate it while you fill in the form without submitting it you have to move the logic to frontend, eg with javascript.