Jump to content

If/else help.


Drewdle

Recommended Posts

Hey peeps,

 

I have this page:

<?php 
include ('header.php');
?></center>
<div class=content>
<?php  
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
{  
$nameuser = $_SESSION['Username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE Username = '$nameuser'");

while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
    
    $id = $results['UserID'];
    $username = $results['Username'];    
    $email = $results['EmailAddress'];
    $location = $results['Location'];
    $website = $results['Website'];
    $about = $results['About'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$editEMAIL=$_POST['email'];
$editLOCATION=$_POST['location'];
$editWEBSITE=$_POST['website'];
$editABOUT=$_POST['about'];
$editID=$_POST['id'];

$editquery = mysql_query("UPDATE users SET EmailAddress='$editEMAIL' , Location='$editLOCATION', Website='$editWEBSITE', About='$editWEBSITE' WHERE UserID='$editID'");

if($editquery)  
        {  
            echo "<b>Success!</b><br>";  
            echo "Your profile was successfully updated. Please click<span class=class1><a href=index.php> here </a></span>to return to the index.<br><br>";  
        }  
        else  
        {  
            echo "<b>Error</b>";  
            echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";  
        }  
    }
}
?>
   <b>Edit your Profile</b>  
  <br><br>
   Please enter your details below to update your profile.  
  <br><br>
    <form method="post" action="editprofile.php" name="editform" id="editform"> 
    <table width=700px border=0 cellspacing=10><tr><td valign=top><table border=0>
<b>Required Information:</b><br><br>
<tr><td>  
        <b>Username:</b>
</td><td>
        <?php echo $username; ?>
</td></tr><tr><td>  
        <b>Email Address:</b>
</td><td>
        <input type="text" name="email" id="email" value="<?php echo $email; ?>">
        <input type="hidden" name="id" value="<?php echo $id; ?>">
</td></tr></table></td><td valign=top>
<table border=0>
<b>Optional Information:</b><br><br>
<tr><td>
     <b>Location:</b>
</td><td>
     <input type="text" name="location" id="location" value="<?php echo $location; ?>">
</td></tr><tr><td>
     <b>Your Website:</b>
</td><td>
     <input type="text" name="website" id="website" value="<?php echo $website; ?>">
</tr></td><tr><td valign=top>
     <b>Short About:</b>
</td><td>
     <textarea name="about" id="about" rows="10" cols="20"><?php echo $about; ?></textarea>
</td></tr></td>
</table> </td></tr> </table>
        <input type="submit" name="register" id="register" value="Update" class=btn />  
    </form>   
</div>
<?php
include ('footer.php'); 
?>

 

When the user clicks the update button the page is refreshed and displays either the error or success message, however, it also still shows the form.

 

Where would I add the if and else tags so that it only shows one of them untill its refreshed with a meta refresh?

 

Thanks

 

 

Link to comment
Share on other sites

You need to set a "flag" variable, then use it as the condition for whether to display the form or not. Give this a shot, and see if it does what you need it to.

 

. . . 
if($editquery)
	{
		echo "<b>Success!</b><br>";
		echo "Your profile was successfully updated. Please click<span class=class1><a href=index.php> here </a></span>to return to the index.<br><br>";
		$display_form = FALSE;
	}
	else
	{
		echo "<b>Error</b>";
		echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
		$display_form = TRUE;
	}
}
}
if( $display_form === TRUE ) {
?>
   <b>Edit your Profile</b>  

[sNIP]

</form>   
</div>
<?php
}
include ('footer.php');
?>

Link to comment
Share on other sites

Doesn't display anything whatsoever.

 

Just pure blankness!

 

Untill I changed this:

if( $display_form === TRUE ) {

 

To this:

if( $display_form = TRUE ) {

 

And then it just did as before, showed both the message and form.

Link to comment
Share on other sites

Still shows a blank page!

 

I wouldn't know the difference between error reporting levels and I presume errors are on because I do get them.

 

for instance when I try to echo after

if ($display_form) {

 

I get a unexpected T_STRING, expecting ',' or ';' error.

 

 

 

Link to comment
Share on other sites

post them verbatim?

 

How would I do that?

 

Current code:

<?php 
include ('header.php');
?></center>
<div class=content>
<?php  
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))  
{  
$nameuser = $_SESSION['Username'];
$checkinfo = mysql_query("SELECT * FROM users WHERE Username = '$nameuser'");

while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)){
    
    $id = $results['UserID'];
    $username = $results['Username'];    
    $email = $results['EmailAddress'];
    $location = $results['Location'];
    $website = $results['Website'];
    $about = $results['About'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$editEMAIL=$_POST['email'];
$editLOCATION=$_POST['location'];
$editWEBSITE=$_POST['website'];
$editABOUT=$_POST['about'];
$editID=$_POST['id'];

$editquery = mysql_query("UPDATE users SET EmailAddress='$editEMAIL' , Location='$editLOCATION', Website='$editWEBSITE', About='$editWEBSITE' WHERE UserID='$editID'");

if($editquery)
	{
		echo "<b>Success!</b><br>";
		echo "Your profile was successfully updated. Please click<span class=class1><a href=index.php> here </a></span>to return to the index.<br><br>";
		$display_form = FALSE;
	}
	else
	{
		echo "<b>Error</b>";
		echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
		$display_form = TRUE;
	}
}
}
if ($display_form) {
?>
   <b>Edit your Profile</b>  
  <br><br>
   Please enter your details below to update your profile.  
  <br><br>
    <form method="post" action="editprofile.php" name="editform" id="editform"> 
    <table width=700px border=0 cellspacing=10><tr><td valign=top><table border=0>
<b>Required Information:</b><br><br>
<tr><td>  
        <b>Username:</b>
</td><td>
        <?php echo $username; ?>
</td></tr><tr><td>  
        <b>Email Address:</b>
</td><td>
        <input type="text" name="email" id="email" value="<?php echo $email; ?>">
        <input type="hidden" name="id" value="<?php echo $id; ?>">
</td></tr></table></td><td valign=top>
<table border=0>
<b>Optional Information:</b><br><br>
<tr><td>
     <b>Location:</b>
</td><td>
     <input type="text" name="location" id="location" value="<?php echo $location; ?>">
</td></tr><tr><td>
     <b>Your Website:</b>
</td><td>
     <input type="text" name="website" id="website" value="<?php echo $website; ?>">
</tr></td><tr><td valign=top>
     <b>Short About:</b>
</td><td>
     <textarea name="about" id="about" rows="10" cols="20" ><?php echo $about; ?></textarea>
</td></tr></td>
</table> </td></tr> </table>
        <input type="submit" name="register" id="register" value="Update" class=btn />  
    </form>   
</div>
<?php
}

include ('footer.php'); 
?>

Link to comment
Share on other sites

Verbatim: exactly; word for word as it appears; cut and paste.

 

I just pasted in the code above, and it has no parse errors. Additionally, I can change it back to read as I posted earlier, and it works as it should (to the extent that I can test it without setting up your database structure locally). You do however have several html errors, such as unquoted attributes like class=btn, and mixing use of html and xhtml tag closing styles.

Link to comment
Share on other sites

Alright, I went through and formatted your code so it would be easier to follow, and this should be the resolution:

 

Change

if( $display_form === TRUE ) {

 

To

if ($display_form === TRUE || $_SERVER['REQUEST_METHOD'] != 'POST') {

 

EDIT: Here's the code, formatted, and with any html syntax errors I spotted corrected.

 

<?php
include ('header.php');
?>
</center>
<div class="content">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
    $nameuser = $_SESSION['Username'];
    $checkinfo = mysql_query("SELECT * FROM users WHERE Username = '$nameuser'");
    while($results = mysql_fetch_array($checkinfo,MYSQL_ASSOC)) {
        $id = $results['UserID'];
        $username = $results['Username'];
        $email = $results['EmailAddress'];
        $location = $results['Location'];
        $website = $results['Website'];
        $about = $results['About'];
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $editEMAIL=$_POST['email'];
        $editLOCATION=$_POST['location'];
        $editWEBSITE=$_POST['website'];
        $editABOUT=$_POST['about'];
        $editID=$_POST['id'];
        $editquery = mysql_query("UPDATE users SET EmailAddress='$editEMAIL' , Location='$editLOCATION', Website='$editWEBSITE', About='$editWEBSITE' WHERE UserID='$editID'");
        if($editquery) {
            echo "<b>Success!</b><br>";
            echo "Your profile was successfully updated. Please click<span class=\"class1\"><a href=\"index.php\"> here </a></span>to return to the index.<br><br>";
            $display_form = FALSE;
        } else {
            echo "<b>Error</b>";
            echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
            $display_form = TRUE;
        }
    }
}
if ($display_form === TRUE || $_SERVER['REQUEST_METHOD'] != 'POST') {
?>

<b>Edit your Profile</b>
<br><br>
Please enter your details below to update your profile.
<br><br>
<form method="post" action="editprofile.php" name="editform" id="editform">
<table width="700px" border="0" cellspacing="10"><tr><td valign="top"><table border="0">
<b>Required Information:</b><br><br>
<tr><td>
<b>Username:</b>
</td><td>
<?php echo $username; ?>
</td></tr><tr><td>
<b>Email Address:</b>
</td><td>
<input type="text" name="email" id="email" value="<?php echo $email; ?>">
<input type="hidden" name="id" value="<?php echo $id; ?>">
</td></tr></table></td><td valign="top">
<table border="0">
<b>Optional Information:</b><br><br>
<tr><td>
<b>Location:</b>
</td><td>
<input type="text" name="location" id="location" value="<?php echo $location; ?>">
</td></tr><tr><td>
<b>Your Website:</b>
</td><td>
<input type="text" name="website" id="website" value="<?php echo $website; ?>">
</tr></td><tr><td valign=top>
<b>Short About:</b>
</td><td>
<textarea name="about" id="about" rows="10" cols="20" ><?php echo $about; ?></textarea>
</td></tr></td>
</table> </td></tr> </table>
<input type="submit" name="register" id="register" value="Update" class="btn">
</form>
</div>

<?php
}
include ('footer.php');
?>

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.