简体   繁体   中英

Displaying Database Query Results

Suppose I have a form. After I submit my form, the data is submitted to dataprocess.php file. The dataprocess.php file processes the variable sent via form and echoes desirable output.

It seems impossible to echo to a specified div in specified page only using PHP (without using AJAX/JavaScript as well). I do not want to use these because some browsers might have these disabled.

My concern is that I want to maintain the same formatting of the page that contained the form element. I want the form element to be there as well. I want the query result to be displayed below the form.

I could echo exact html code with some modification but that's memory expensive and I want it systematic.

  1. Is it possible to process the form within the same page? Instead of asking another .php file to process it? How does one implement it?

  2. The above is just for knowledge. It will be long and messy to include the PHP script within the same HTML file. Also, that method might not be efficient if I have same process.php file being used by several forms.

I am actually looking for efficient methods. How do web developers display query result in same page? Do the echo all the html formatting? also, does disabling JavaScript disable jQuery/AJAX?

Yes it is possible to process the form on the same page.

<?php 

if (isset($POST)) 
{
  //write your insert query
}

?>

<html>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <!-- Your form elements and submit button -->
        </form>
        <table>
        <?php
          //your select query in a while loop
        ?>
        </table>
    </body>
</html>

But if you choose this technique instead of ajax, you have to refresh all the page for each insert action.

An example

<div id="dialog-form">
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
            <table>
                <tr>
                    <td>Job</td>
                    <td>
                        <input type="text" name="job" />
                    </td>
                </tr
            </table>
            <input type="submit" value="Insert" />
    </fieldset>
    <input type="hidden" name="doProcess" value="Yes" />
    </form>
</div>

<?php

$myQuery= $db->prepare("INSERT INTO Jobs (job) VALUES (:p1)");


if (isset($_POST['doProcess']) && $_POST['doProcess'] == 'Yes')
{
    $myQuery->bindValue(":p1", $_POST['job'], PDO::PARAM_STR);  
    $myQuery->execute();
}


?>

if you really dont want to use ajax (which i think you should). You can do something like this.

<form action="" method="POST">
   <input type="text" value="something" name="something_name"/>
    <?php
        if(isset($_POST['something_name'])){
            echo '<div id="display_something_name_if_exists">';
            echo $_POST['something_name'];
            echo '</div>';
        }
    ?>
</form>

Basically what it does is submits to itself and then if there is a submission (tested with isset), it will echo a div with the correct information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM