Hi,

How to pass an array to a form which is above the part where the array is created

I have a php script with html in it. The page is divided into blocks using div tags. There is a form in a div tag which is at the top which needs id generated by a sql query in a div tag below it. Changing the positions of div tag will change the entire look of the page.

Recommended Answers

All 6 Replies

Put the php block that generates required data at the top or before the form div (this won't change the appearenca of the page).

<?php

?>
<html>

<body>
    <div id="main_container">
        <div id="column1">
            <form>
                //requires ids
            </form>
        </div>
        <div id="column2">
            <?php
                //gets input from a form submitted in another page
                //Compares value in the database
                while ($row = mysql_fetch_array($result))
                {
                    echo "output";
                    array1[] = //store each id in an array
                }

            ?>
        </div>
    </div>
</body>
</html>

If I put the php block above then the result appears on a blank page.

I want the page to appear in the middle with other menus and columns i.e i want the css to apply to the result

I tried putting the php block on top and the part that generates and displays the result in the div tag.
Now result appear according to the css.

But there is an If condition that echo's 'Please enter something' if all form fields are emty.
This message is coming on a separate blank page. I would like it to come on the same page of the form or in the div tag. How do I manage that?

Initialize a variable that will hold the error message:

$error_msg = '';

On the beginning it will be an empty string. When checking form fields and if all are empty (or invalid values) assign an error message to the $error_msg variable. At hte end of the script (or anywhere else) place a condition that will display a div tand the contents of the $error_msg.

if($error_msg != '') {
    echo "<div class="error-msg">$error_msg</div>";
}

Thanks.

It worked

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.