Jump to content

echo out a variable


dazz_club
 Share

Recommended Posts

Hi guys,

 

on my form i have serveral echos that inform the users they have either incompleted the form or they have successfully compelted the form.

 

Currently when i test it out, its breaking my xhtml/css code and making the page aligned left rather then center.

 

So what i am looking to do is have the errors/success as different variables and then place them some where nicely in the form using

 

<?php echo $error ; ?>

 

currently this is my entire code

<?php
session_start();
require_once("includes/connection.php"); 

// check that the form is submitted
if(isset($_POST['submit']))
{
    // validate username
    if(isset($_POST['username']) && !empty($_POST['username']))
    {
        // use the built in mysql real escape string function to protect agains SQL Injection
        $username = mysqli_real_escape_string($connection, $_POST['username']);
    }
    else
    {
        // username does not validate, define an error
        $errors[] = 'You have forgotton to include your username.';
    }

    // we apply the same for the password field.
    if(isset($_POST['password']) && !empty($_POST['password']))
    {
        $password = md5($_POST['password']);
    }
    else
    {
        $errors[] = 'Password not provided';
    }

    // chekc that no errors have been set, if so display them
    if(isset($errors) && is_array($errors))
    {
        echo 'Errors: <ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
    }
    // no errors are set so we'll continue
    else
    {
        $sql= " SELECT * FROM members WHERE username = '$username' AND password = '$password' ";
        $result = mysqli_query($connection, $sql) or die('Query Error:<br />Query: <tt>'.$sql.'</tt><br />Error: ' . mysqli_error($connection));

        // check that the query return only ONE result
        if(mysqli_num_rows($result) == 1)
        {
            $_SESSION['is_logged_in'] = true;

            // get result set from the query and assign it to the 'user' session.
            $row = mysqli_fetch_assoc($result);
            $_SESSION['user'] = $row;

            // redirect to the login_success.php
            header('Location: free-sample/');
            exit;
        }

        // query failed, display error
        echo "Wrong Username or Password";
    }
}
// for was not submitted, display error
else
{
    echo '<div style=\"position:absolute;top:200px;left:0px;font-weight:bold;color:red;\">Please use this form to login </div>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Bulletins</title>
<link rel="stylesheet" type="text/css" href="styles/style.css" />	

</head>
<body>
	<div id="container">
		<div id="holder">
			<div id="header"><img src="../images/banner.png" style="border:none;" /></div>
			<div id="main">
				<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
					<div id="formHolder" >
					<ul class="form">
						<li> </li>
						<li>Please enter your name: <input type="text" name="username" id="username" style="color:red;"></li>
						<li> </li>
						<li>Please enter reference password: <input type="password" name="password" id="password" style="color:red;"></li>
						<li> </li>
						<li><input type="submit"  name="submit" value="submit"   title="submit" ></li>
					</ul>
				</form>
			</div>
			</div>
			<div id="footer"></div>
		</div>
	</div>
</body>
</html>	

 

kind regards

Dazzclub

Link to comment
Share on other sites

You can always do something like

<?php
$error .= "This is error one! <br />";
$error .= "This is error two! <br />";

echo $error;

/*
*    This will output: 
This is error one!
This is error two!
*/?>

 

Basically the . will add the argument to the variable instead of redefining it.

 

Hope that helps,

Cold

Link to comment
Share on other sites

Hi coldkill ,

 

Your right I can use that as I have used it on another form, but I had gone ahead and set up a variable for each message.

 

like this (code just for to look at)

<?php
session_start();
require_once("includes/connection.php"); 

// check that the form is submitted
if(isset($_POST['submit']))
{
    // validate username
    if(isset($_POST['username']) && !empty($_POST['username']))
    {
        // use the built in mysql real escape string function to protect agains SQL Injection
        $username = mysqli_real_escape_string($connection, $_POST['username']);
    }
    else
    {
        // username does not validate, define an error
	$no_username = '<div class=\"login\" >incorrrect username</div>';
        
    }

    // we apply the same for the password field.
    if(isset($_POST['password']) && !empty($_POST['password']))
    {
        $password = md5($_POST['password']);
    }
    else
    {
        $no_password = 'Password not provided';
    }

    // chekc that no errors have been set, if so display them
    if(isset($errors) && is_array($errors))
    {
        echo 'Errors: <ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
    }
    // no errors are set so we'll continue
    else
    {
        $sql= " SELECT * FROM members WHERE username = '$username' AND password = '$password' ";
        $result = mysqli_query($connection, $sql) or die('Query Error:<br />Query: <tt>'.$sql.'</tt><br />Error: ' . mysqli_error($connection));

        // check that the query return only ONE result
        if(mysqli_num_rows($result) == 1)
        {
            $_SESSION['is_logged_in'] = true;

            // get result set from the query and assign it to the 'user' session.
            $row = mysqli_fetch_assoc($result);
            $_SESSION['user'] = $row;

            // redirect to the login_success.php
            header('Location: free-sample/');
            exit;
        }

        // query failed, display error
        $both_pw_unme = "Wrong Username or Password";
    }
}
// for was not submitted, display error
else
{
    $form_login = '<div class=\"login\" >Please use this form to login </div>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Bulletins</title>
<link rel="stylesheet" type="text/css" href="./styles/style.css" />	

</head>
<body>
	<div id="container">
		<div id="holder">
			<div id="header"><img src="../images/banner.png" style="border:none;" /></div>
			<div id="main">
				<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
					<div id="formHolder" >
					<ul class="form">
						<li class="login">
						<?php echo ($both_pw_unme); ?>
						<?php echo ($form_login); ?><?php echo ($no_username); ?><?php echo ($no_password); ?></li>
						<li>Please enter your name: <input type="text" name="username" id="username" style="color:red;"></li>
						<li> </li>
						<li>Please enter reference password: <input type="password" name="password" id="password" style="color:red;"></li>
						<li> </li>
						<li><input type="submit"  name="submit" value="submit"   title="submit" ></li>
					</ul>
				</form>
			</div>
			</div>
			<div id="footer"></div>
		</div>
	</div>
</body>
</html>	

 

What i need to do next is tidy up the echo's that are in the actual form. I can use if else statements here couldn't I?

 

kind regards

Dazzclub

Link to comment
Share on other sites

You could test to see if the variables are set using isset(); or empty(); testing to see if they are set or if they have anything in them (NULL, FALSE, 0 etc count as nothing).

 

You should change

<?php echo ($form_login); ?><?php echo ($no_username); ?><?php echo ($no_password); ?>

To just use one set of <?php tags. It will save the effort on the engine constantly opening and closing when there is no HTML in between. You could go as far as to echo the entire form and just insert the variables into the echo. In fact, you could save even more effort on the engine by just doing:

<?php echo $form_login.$no_username.$no_password; ?> 

Since that is basically what you are doing, just more inefficiently.

 

PHP won't echo anything in a null variable. So really you don't need the if else statements. If the variables aren't set, nothing gets displayed.

 

Hope that helps,

Cold

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.