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.

Dani AI

Generated

Short answer: do the work that builds the ID list before you render the HTML, but avoid printing or redirecting during that processing. Gather the IDs and any validation messages into variables, then output those variables inside the top form/div when you print the page. That keeps the page layout and CSS intact while still using data that comes from a later query.

What to check and why you saw a blank page

  • A blank page usually means a fatal error or that the script redirected/exited before the HTML container was sent. While debugging, enable PHP error reporting or check the web server/PHP error log to see the real cause.
  • Don’t echo diagnostic text during processing. Instead store messages and arrays in variables and render them where the layout exists. This is the approach was getting at — collect state, then print it in the appropriate div so the message appears inside your page frame rather than on a new blank page.

Practical alternatives and improvements

  • If you cannot move the query for layout reasons, capture output and place it later with output buffering. See PHP’s output-control functions for details (ob_start() and friends).
  • For a smoother UX, fetch IDs asynchronously after the page loads and populate the form via AJAX.
  • Security and compatibility: avoid the old mysql_* extension; use mysqli or PDO with prepared statements instead (PDO manual).

Final tips

  • Use a simple “process-then-render” pattern: handle POST/DB at the top, set arrays/flags/messages, then include the HTML template that reads those variables. That keeps presentation separate, avoids layout breakage, and makes validation messages appear exactly where you want them.

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.