2013-03-03 100 views
0

我对PHP比较新。对于我的网页,我需要在同一个页面中加载多个页面......就像我有一个名为cwt.html的基本HTML页面一样有所有的复选框和提交按钮。一旦提交按钮后,下一个页面(说processing.php)相关选定的复选框(cwt.html的)也应该在同一个页面加载..在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> 
+1

阅读关于AJAX。 – dfsq 2013-03-03 06:58:36

+0

虽然注意到,不建立网站是没有JS支持不必要的破坏 – Eevee 2013-03-03 07:15:25

回答

0

我建议使用jQuery的.load()使用AJAX,加载网页的功能被加载。

Here is the link!

+0

非常感谢:-)它完美的工作:-) – 2013-03-03 07:47:19

+0

没有问题!我会赞赏竖起大拇指或什么的! :d – saada 2013-03-04 04:11:16

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

正如其他人所说,使用jQuery加载HTML片段为div。既然你已经有了一个完整的HTML文档,你加载的文本不应该是一个完整的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> 
相关问题