0

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?

4

2 回答 2

1

是的,可以在同一页面上处理表格。

<?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>

但是,如果您选择这种技术而不是 ajax,则必须为每个插入操作刷新所有页面。

一个例子

<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();
}


?>
于 2013-07-15T08:24:51.463 回答
1

如果你真的不想使用 ajax(我认为你应该这样做)。你可以做这样的事情。

<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>

基本上它所做的是提交给自己,然后如果有提交(用 isset 测试),它将用正确的信息回显一个 div。

于 2013-07-15T08:27:37.847 回答