Page 1 of 1

[SOLVED]error message placing

Posted: Mon Aug 29, 2005 4:32 am
by pleigh
if i do this technique

Code: Select all

if (isset($_POST['submit']))
{
	$message = NULL;
	
	//check for project title
	if (empty($_POST['projecttitle']))
	{
		$pt = false;
		$message .= 'You forgot to enter the project title<br>';
	}
	else
	{
		$pt = $_POST['projecttitle'];
	}

        if ($pt)
       {
              //sql code goes here
       }
}

if (isset($message))
{
	echo '<font color=red>'.$message.'</font>';
}
then my form will be

Code: Select all

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" >
<input name="projecttitle" type="text" size="31" value="<? if (isset($_POST['projecttitle'])) echo $_POST['projecttitle']; ?>">
<input type="submit" name="submit" value="submit" class="buttonformat" />
the result of this is when the user supplied a blank character in the textbox, the error message will appear above the form, right?now, the deal is that, i want the error message to appear right after the textbox...can u guys help me modify the above code so i can attain this idea?thanks in advance.. :)

Re: error message placing

Posted: Mon Aug 29, 2005 4:59 am
by s.dot
pleigh wrote:if i do this technique

Code: Select all

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" >
<input name="projecttitle" type="text" size="31" value="<? if (isset($_POST['projecttitle'])) echo $_POST['projecttitle']; ?>">
<input type="submit" name="submit" value="submit" class="buttonformat" />
Perhaps it should be:

Code: Select all

<input name="projecttitle" type="text" size="31" value="<? if (isset($_POST['projecttitle'])) echo $_POST['projecttitle']; ?>"> <? if($pt = "false"){ echo $errormessage; } ?>
Or perhaps I'm not grasping your question right :P

Posted: Mon Aug 29, 2005 5:17 am
by n00b Saibot
I suggest you use associative arrays for this. for eg.

Code: Select all

if (isset($_POST['submit'])) 
{ 
    $error = array(); 
     
    //check for project title 
    if (empty($_POST['projecttitle'])) 
    { 
        $pt = false; 
        $error['projecttitle'] = 'You forgot to enter the project title<br>'; //set the projecttitle error...
    } 
    else 
    { 
        $pt = $_POST['projecttitle']; 
    } 

        if ($pt) 
       { 
              //sql code goes here 
       } 
}
and in the form you output the err like this

Code: Select all

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" > 
<input name="projecttitle" type="text" size="31" value="<? if (isset($_POST['projecttitle'])) echo $_POST['projecttitle']; ?>">
<? if (isset($error['projecttitle'])) echo $error['projecttitle']; ?>
<input type="submit" name="submit" value="submit" class="buttonformat" />

Posted: Mon Aug 29, 2005 5:31 am
by pleigh
thanks noob saibot it perfectly works... :D