2013-03-03 124 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> 
相關問題