Jump to content

Newb Question: Loading HTML in one instance, and another HTML in another


smc

Recommended Posts

Hello,

Okay basically my deliuma is that I had one php script. This PHP is going to add users to a database. When you go to the page the PHP detects that a variable "submit" is not defined as true so I want it to show the HTML that has the forms and submit button, etc. When you click the submit button the script goes back to itself but with the ending ?submit=true to declare that it has been infact submitted, then proccess and adds the information to the PHP. My problem is I want it when it is submited to show an HTML on the page that says it's submitted as opposed to the HTML showing the forms.

In essence, I need to load different HTML for different instances in the same PHP script.

How do I do this?
Link to comment
Share on other sites

Trivial example:

[code]
<?php
if($_GET['submit']){
?>
<p>You've successfully submitted my trivial form.  Tada.</p>
<?php
}else{
?>
<form action='test.php'>
<input type='hidden' name='submit' value='1' />
<input type='submit' name='formSubmit' value='submit' />
</form>
<?php
}
?>
[/code]

Best,

Patrick
Link to comment
Share on other sites

I personally have it like this


[code]<?php
if (isset($_POST['submit'])) {
// validate
// errorcheck
// clean
// process from db
// update builder (entry whatever)
if (mysql_query($query)) {
$show = "yes";
}

}
?>
<?php
if ($show != "yes") {
?>
<!-- XHTML/CSS form right here, with submit button -->
<?php
}
?>[/code]
Seems to just be the way I always have done it when I do same page processing.
Link to comment
Share on other sites

Perfect! Exactly what I needed, thanks :)

One more question I have on this subject though is that is there anyway way to call an HTML page into the if else functions. ie: Having two complete page sets of HTML can get a bit messy, is there any way to have the HTML external and call it in depending on the situation?

Thanks!!
Link to comment
Share on other sites

There is.  The best way would be to implement some 2 or 3 tier architecture i.e., MVC but this introduces quite a bit of overhead and requires some knowledge of object oriented paradigms.  You can achieve what you are after using the PHP include construct.  We would modify the afore mentioned code as follows:

[code]
<?php
if($_GET['submit']){
        include 'success.php';
}else{
include 'someForm.php';
}
?>
[/code]

Where success.php and someForm.php contain their respective html from the previous example.  I hope this was of some help.

Best,

Patrick
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.