在PHP中在同一页面上加载多个页面

时间:2013-03-03 06:56:42

标签: php

我对PHP比较陌生。对于我的网页,我需要在同一页面加载多个页面..因为我有一个名为cwt.html的基本HTML页面,其中包含所有复选框和提交按钮。单击“提交”按钮后,还应在同一页面中加载与所选复选框(cwt.html)关联的下一页(例如processing.php)。

<html>
<head>
<title> Conditions We Treat </title>
</head>
<body>
<form id = form1 action = "processing1.php" method = "post">
<input type = "checkbox" name = "sickness[]" value = "Nausea">Nausea</input><br/>
<input type = "checkbox" name = "sickness[]" value = "Constipation">Constipation</input><br/>
<input type = "checkbox" name = "sickness[]" value = "vomiting">Vomiting</input><br/>
<div id = "submit1"><input type = "submit" name = "submit" value = "submit"></input></div><br/>
</form>
</body>
</html>

在此网页中单击提交按钮后,控件应转到processing1.php,但内容必须加载到同一页面中

<html>
<head>
<title> Conditions We Treat </title>
</head>
<body>
<?php
echo "hi"
foreach($_POST['sickness'] as $s)
{
    $con = mysqli_connect("localhost","root","","collofnursing");
    //mysql_select_db("collofnursing");
    $res = mysqli_query($con,"select * from condwetreat");
    while($row = mysqli_fetch_array($res))
    {
        echo $s;?> <br><br><?php
        }
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您可以使用jquery,通过onclick事件将提交方法更改为函数:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function onSubmitForm(){
    $.get('page1.php').success(function(html) {
        $('#page1').html(html);
    });
    $.get('page2.php').success(function(html) {
        $('#page2').html(html);
    });
}
</script>

<div id="page1"></div>
<div id="page2"></div>

答案 1 :(得分:0)

我建议使用jQuery .load()函数,该函数使用AJAX加载页面。

Here is the link!

答案 2 :(得分:0)

正如其他人所说,使用jQuery将HTML代码段加载到div。由于您已经拥有完整的HTML文档,因此您加载的文本不应该是完整的HTML文档 - 如果您将其加载到div中,则需要使用HTML。

我想知道我是否可以提供一些建议 - 与问题无关! - 你可能觉得有用。随着您的应用程序变得越来越复杂,您会发现将逻辑和表示代码分开是最简单的 - 即使从一开始,您只需将前者放在PHP文档的开头,而将后者放在最后。当您了解有关PHP的更多信息时,您会发现将这些作为单独的文件并使用require_once加载“模板”是个好主意。

如果你这样做,你会发现你编写PHP的方式在逻辑和模板之间有所不同。对于模板,它有助于将每个PHP语句保存在单个PHP块中,因此基本上文档仍然是HTML。这更整洁,有助于利用IDE中的语法着色和标记验证。因此,我会这样写你的模板:

<?php
// Move this 'logic' code out of the way of your view layer
// Here, the brace form of loops is preferred
// Ideally it'd get moved to a different file entirely
$con = mysqli_connect("localhost","root","","collofnursing");
mysql_select_db("collofnursing");
$res = mysqli_query($con,"select * from condwetreat");

?><html>
<head>
    <title> Conditions We Treat </title>
</head>
<body>
    <!-- The PHP 'tags' are now easier to indent -->
    <!-- So use while/endwhile, foreach/endforeach etc -->
    Hi
    <?php while($row = mysqli_fetch_array($res)): ?>
        <!-- So it's HTML to style stuff, rather than putting
             HTML tags into PHP echo statements :) -->
        <p class="symptom">
            <?php echo $row['symptom'] ?>
        </p>
    <?php endwhile ?>
</body>
</html>