Reputation: 19
I am relatively new to PHP.. For my web page, I need to load multiple pages in the same page.. as in I have a basic HTML page named cwt.html which has all the checkboxes and submit button. Once the Submit button is clicked, the next page(say processing.php) associated for the selected check-boxes(of cwt.html) should also be loaded in the same page..
<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>
Once the submit button is clicked in this web page, the control should head to processing1.php, but the content has to be loaded in the same page
<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>
Upvotes: 0
Views: 4124
Reputation: 20439
As others have said, use jQuery to load an HTML snippet into a div
. Since you already have a complete HTML document, the text you load should not be a full HTML document - just the HTML you would need if you'd loaded it into the div
to start with.
I wonder if I could offer some advice - not related to the question! - which you might find useful. As your application gets more complex, you'll find it neater to keep your logic and your presentation code separate - even if to start with, you just put the former at the start of the PHP document, and the latter at the end. As you learn more about PHP, you'll find it is a good idea to have these as separate files and load the 'template' in using require_once
.
If you do this, you'll find that the way you write PHP differs between logic and template. For the template, it helps to keep each PHP statement in a single PHP block, so essentially the document remains as HTML. This is neater, and helps take advantage of the syntax colouring and tag validation inside an IDE. Thus, I would write your template this way:
<?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>
Upvotes: 0
Reputation: 3809
You can use jquery, change your submit method by an onclick event to the function:
<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>
Upvotes: 1
Reputation: 2761
I recommend using the jQuery .load() function which loads pages using AJAX.
Upvotes: 0