Hello i need a little solution for a variable in my code its currently printing error messages but i want to keep these error messages so that i can print them any where in my html using this code <?php print_r($messages); ?>
becuase currently its printing the error message on the top of the page and my whole page messed up with this

Any solution of store messages in variable please look at my php code

include ('connectdb.php');
if (isset($_POST['formsubmitted'])) {
session_start();

    $error = array();//Declare An Array to store any error message

    if (empty($_POST['business_name'])) {
        $error[] = 'Please Enter a Business Name ';
    } else {

        if (preg_match("/^[a-z0-9 .\-]+$/i", $_POST['business_name'])) {
           //regular expression for text inputs
            $business_name = $_POST['business_name'];
        } else {
             $error[] = 'Business Name is invalid  ';
        }


    }

        if (empty($_POST['business_title'])) {
        $error[] = 'Please Enter a Business title ';
    } else {

        if (preg_match("/^[a-z0-9 .\-]+$/i", $_POST['business_title'])) {
           //regular expression for text inputs
            $business_title = $_POST['business_title'];
        } else {
             $error[] = 'Business title is Invalid  ';
        }

    }


    if (empty($_POST['business_type'])) {
        $error[] = 'Please Enter a Business Type ';
    } else {

        if (preg_match("/^[a-z0-9 .\-]+$/i", $_POST['business_type'])) {
           //regular expression for text inputs
            $business_type = $_POST['business_type'];
        } else {
             $error[] = 'Business Type is Invalid  ';
        }

    }




    if (empty($_POST['business_website'])) {
        $error[] = 'Please Enter a Business Website ';
    } else {

        if (preg_match("/^((?:http(?:s)?\:\/\/)?[a-zA-Z0-9_-]+(?:.[a-zA-Z0-9_-]+)*.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/", $_POST['business_website'])) {
           //regular expression for text inputs
            $business_website = $_POST['business_website'];
        } else {
             $error[] = 'Business website is invalid  ';
        }

    }



    if (empty($_POST['business_facebook'])) {
        $error[] = 'Please Enter a Business Facebook ';
    } else {
        $business_facebook = $_POST['business_facebook'];

                if (preg_match("/^((?:http(?:s)?\:\/\/)?[a-zA-Z0-9_-]+(?:.[a-zA-Z0-9_-]+)*.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/", $_POST['business_facebook'])) {
           //regular expression for text inputs
            $business_facebook = $_POST['business_facebook'];
        } else {
             $error[] = 'Business Facebook is invalid  ';
        }
    }   

    if (empty($_POST['business_contact'])) {
        $error[] = 'Please Enter a Business Contact ';
    } else {

        if (preg_match("/^[a-z0-9 .\-]+$/i", $_POST['business_contact'])) {
           //regular expression for text inputs
            $business_contact = $_POST['business_contact'];
        } else {
             $error[] = 'Business Contact is invalid  ';
        }


    }



    if ($_POST['special_meal'] == "Yes" && empty($_POST['offers_deals'])) {
        $error[] = 'Please describe what would you like to offer?';
    } else {
        $special_meal = $_POST['special_meal'];
        $offers_deals = $_POST['offers_deals'];
    }





    if (empty($_POST['business_number'])) {
        $error[] = 'Please Enter a Contact Number ';
    } else {

                        if (preg_match("/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/", $_POST['business_number'])) {
           //regular expression for text inputs
            $business_number = $_POST['business_number'];
        } else {
             $error[] = 'Contact Number is invalid  ';
        }
    }





    if (empty($_POST['business_email'])) {
        $error[] = 'Please Enter your Email ';
    } else {


        if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['business_email'])) {
           //regular expression for email validation
            $business_email = $_POST['business_email'];
        } else {
             $error[] = 'Your Email Address is invalid  ';
        }


    }






    if (empty($error)) //send to Database if there's no error '

    { // If everything's OK...

        // Make sure the email address is available:
        $query_verify_email = "SELECT * FROM users_registration  WHERE email_address ='$business_email'";
        $result_verify_email = mysqli_query($dbc, $query_verify_email);
        if (!$result_verify_email) {//if the Query Failed ,similar to if($result_verify_email==false)
            echo ' Database Error Occured ';
        }

        if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .


            // Create a unique  activation code:
            $activation = md5(uniqid(rand(), true));

$_SESSION["business_name"] = $_POST['business_name'];
$_SESSION["business_title"] = $_POST['business_title'];
$_SESSION["business_type"] = $_POST['business_type'];
$_SESSION["business_contact"] = $_POST['business_contact'];
$_SESSION["business_website"] = $_POST['business_website'];
$_SESSION["business_facebook"] = $_POST['business_facebook'];
$_SESSION["business_email"] = $_POST['business_email'];
$_SESSION["special_meal"] = $_POST['special_meal'];
$_SESSION["offers_deals"] = $_POST['offers_deals'];
$_SESSION["business_number"] = $_POST['business_number'];

$bname = $_SESSION["business_name"];
$btitle = $_SESSION["business_title"];
$btype =  $_SESSION["business_type"];
$bcontact = $_SESSION["business_contact"];
$bwebsite = $_SESSION["business_website"];
$bfacebook = $_SESSION["business_facebook"];
$bemail = $_SESSION["business_email"];
$bnumber = $_SESSION["business_number"];
$boffer =  $_SESSION["offers_deals"];
$active = $activation;

            $query_insert_user = "INSERT INTO users_registration (business_name, business_type, business_website, business_contact, business_title, contact_number, business_facebook, email_address, special_meals, offers_deals, com_code) VALUES ( '$bname', '$btype' , '$bwebsite', '$bcontact', '$btitle' , '$bnumber', '$bfacebook', '$bemail', '$special_meal', '$boffer', '$active')";


            $result_insert_user = mysqli_query($dbc, $query_insert_user);
            if (!$result_insert_user) {
                echo 'Query Failed ';
            }

            if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.


                // Send the email:
                $message = " To activate your account, please click on this link:\n\n";
                $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($business_email) . "&key=$activation";
                mail($business_email, 'Registration Confirmation', $message, 'From: signup@mywebsite.com');

                // Flush the buffered output.


                // Finish the page:
                echo '<div class="success">Thank you for
registering! A confirmation email
has been sent to '.$business_email.' Please click on the Activation Link to Activate your account </div>';


            } else { // If it did not run OK.
                echo '<div class="errormsgbox">You could not be registered due to a system
error. We apologize for any
inconvenience.</div>';
            }

        } else { // The email address is not available.
            echo '<div class="errormsgbox" >That email
address has already been registered.
</div>';
        }

    } else {//If the "error" array contains error msg , display them




echo '<div class="errormsgbox"> <ol>';
        foreach ($error as $key => $values) {

            echo '  <li>'.$values.'</li>';



        }
        echo '</ol></div>';

    }

    mysqli_close($dbc);//Close the DB Connection

} // End of the main Submit conditional.

Have you set the css rule for errormsgbox?

yes I already setup rules for css
but i want to display them using the variable values to print

I am just guessing what do you intent, you can declare your array elements:
$error['business_name'] = 'Please Enter a Business Name ';
etc.

how can i convert this into array varriable

echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {
echo ' <li>'.$values.'</li>';
}
echo '</ol></div>';
}

Why would you convert anything?
You already have an array $error data (if there is no errors provided).
Just use it anywhere on page. You can echo variables few times as well.
Or specifically you can make a function with that code and call it where ever you want.

//$errors = array('Bad username', 'Bad pass', 'Not phone number input here',);

function displayErrors()
{
    global $errors;
    echo '<ol>';
    foreach ($errors as $error)
    {
        echo '<li>' . $error . '</li>';
    }
    echo '</ol>';
}

//displayErrors();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.