[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Change the location of Error Message:header ("Location: $err");

Posted on 2006-05-28
16
Medium Priority
?
288 Views
Last Modified: 2008-02-01
Hi!

I have a script as follows in my online PHP survey file:
___________ the script _______________________

elseif (!isset($_POST['year']))
  $err = 'form.php?err=Must+Provide+Your+Name;

// if error: Redirect to form
  if ($err != '')
{
 header("Location: $err");
  exit;
}
___________________________________________
When an invalid name is typed in, an error mesage reading "Must provide your name" shows up on the screen.

The problem is that the error message shows up at left bottom corner of the screen. I wonder whether the message may be made to show up at more visible location of the screen (if possible, right next to the "Name" textbox) and in diffrent font color (example: red) and different font size.

Hope you experts may help me!

Thanks!

duta
May 28, 2006, at 3 am


0
Comment
Question by:duta
  • 8
  • 5
14 Comments
 
LVL 9

Expert Comment

by:tolgaong
ID: 16778817
sthg like that... ???

<?php
$name="";
$surname="";
if (isset($_POST["name"])){
      extract($_POST);
      $err=array();
      if (strtolower($name)!='tolga') $err["name"]="Must Provide Your Name";
      if (strtolower($surname)!='ongunsu') $err["surname"]="Must Provide Your Surname";
      }
?>
<form action="form.php" method="post">
  <table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><strong>Name:</strong></td>
    <td><input name="name" type="text" value="<?php echo $name?>" /><?php echo (isset($err["name"]))?'<span style="color:Red">'.$err["name"].'</span>':''?></td>
  </tr>
  <tr>
    <td><strong>Surname:</strong></td>
    <td><input name="surname" type="text" value="<?php echo $surname?>" /><?php echo (isset($err["surname"]))?'<span style="color:Red">'.$err["surname"].'</span>':''?></td>
  </tr>
  <tr>
    <td colspan="2" align="right"><input type="submit" name="Submit" value="Send" /></td>
    </tr>
</table>
</form>
0
 

Author Comment

by:duta
ID: 16778839
TO: tolgaong:

Thank you so much for your kind, prompt response.

I am going to add your script to my work and test it. It may take a little while before I will come back to you.

Thanks again!

duta

May 28, 2006, at 3:55 am
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16778875
Hi,

You shouldn't just include the error text to your url and then plainly output it. Better is to use identiefier:
mysite.com/mypage.php?err=ERR_NO_USERNAME

Then at the location you want your error to show you can use:

if(isset($_REQUEST['err']) && $_REQUEST['err'] == 'ERR_NO_USERNAME')) {
  echo '<span class="err">You must provide a username</span>';
}

-r-
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:duta
ID: 16778902
TO: Roonaan:

Thank you again for your always kind, prompt response.

By the way, you wrote in your last post:
"Better is to use identiefier:
mysite.com/mypage.php?err=ERR_NO_USERNAME"  Would you kindly give a little bit more instructions about what to do with it?  Hope you may remember that I am still a struggling novice.

In addition, I have a lot more input boxes to be validated. You may check my work at "http://www.nice-man.com/bogo/".

Thanks a lot as always!

duta
May 28, 2006, at 4:33 am




0
 

Author Comment

by:duta
ID: 16778918
TO: tolgaong :

Thanks again for your kind help.

I thought you may better help me if you may kindly check my work at "http://www.nice-man.com/bogo/".

Thanks again!

May 28, 2006, at 4:39 a.m.

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16778919
Well,

The problem when you have a string in your url, and you output it directly, is that people can easily disrupt your site by adding strange phrases:
mysite.com/mypage.php?err=Look+at+me+I+am+changing+the+error+message+WOW+I+am+sooooooooooo+cool

This shouldn't be a real problem, but it isn't something you would like either.

Therefor you can use certain keywords that identify certain error messages:
ERR_NO_USERNAME turns to 'Sorry, you should fill in a username'.
(Eventually you can have a multilanguage site even, which still works because you only have to change the response text with certain error keywords, rather than your validation script which might generate an error.)

You then setup a list for yourself with possible errors, giving them semi-descriptive names:
ERR_NO_USERNAME,
ERR_NO_PASSWORD,
ERR_LOGIN_FAILED

And instead of the code below, you output err keywords:
elseif (!isset($_POST['year']))
  $err = 'form.php?err=Must+Provide+Your+Name;

Becomes:

elseif (!isset($_POST['year']))
  $err = 'form.php?err=ERR_NO_USERNAME;

Because your output always responds only to your own defined ERR_..... keywords, you have full control of what your output messages are.

-r-
0
 

Author Comment

by:duta
ID: 16778938
TO: Roonaan:

Thanks again for your very kind, prompt response.

I couldn't quitely clearly understand what to do with "ERR_NO_USERNAME" (from mysite.com/mypage.php?err=ERR_NO_USERNAME).

So I modified my script as the follows:

_________________________________________________________________________________


define ("ERR_NO_USERNAME", "You must put a valid user name.");
$err = '';
if (!isset($_POST['name']))
 
 $err = 'form.php?err=ERR_NO_USERNAME';

__________________________________________________________________________________

Of course, it did not work.

Would you mind giving a little bit more explanations about "from mysite.com/mypage.php?err=ERR_NO_USERNAME)"?  

Thanks a lot!

duta

May 28, 2005, at 4:59 am
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16778957
The line
 $err = 'form.php?err=ERR_NO_USERNAME';
is correct.

Now you need to change $form.php. At the location where you want to have your error message put this:

if(isset($_REQUEST['err']) && $_REQUEST['err'] == 'ERR_NO_USERNAME'))
   echo ERR_NO_USERNAME;

-r-
0
 

Author Comment

by:duta
ID: 16779005
TO: Roonaan and Tolgaong:

Thank you very much for your kind effort to help me out.

By the way, I tried this way and that with your kind suggestions. But for some reason, I am not making much progress.

I just wondered whether you may download my scripts from my website (http:http://www.nice-man.com/bogo/) and check it. I thought that might help you save time.  I spent quite much time today and yesterday trying to figure this out.  I think I may need to get just a little sleep now.

Hope and believe you may kindly help me out.

Thanks a lot!

duta

May 28, 2006, at 5:38 a.m.
0
 

Author Comment

by:duta
ID: 16780344
TO: Roonaan:

Hi again!

I just wondered whether there is a way to make the error message from the script below to be displated at the top of the screen, instead of at bottom of the screen. IN other words, is it possible to make "$err" displayed at a location other than the bottom of the screen?

Thanks again!

duta

May 28, 2006, at 2:33 pm



_________________________________________________________________________________

elseif (!isset($_POST['year']))
  $err = 'form.php?err=Must+Provide+Your+Name;

// if error: Redirect to form
  if ($err != '')
{
 header("Location: $err");
  exit;
}
___________________________________________________________________________________
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16780770
Yes this is possible. You would have to change form.php and look where it outputs the errors at this stage.

Then move the code to the top of the form.

The header() function actually has no influence where the error message is actually displayed. It is used to redirect the page and reload at a certain url.

-r-
0
 

Author Comment

by:duta
ID: 16780846
TO: Roonaan:

Thanks again for your kind, prompt response.

Yes, I checked form.php over and again for last several hours, but I could not figure out why the error message was displayed at the bottom which can be viewed only when vertical scrollbar was used. But as you kindly suggested, I will check it again.

By the way, I would like to ask you one more question regarding your earlier suggestion:
You then setup a list for yourself with possible errors, giving them semi-descriptive names:
_______________________ Your earlier suggestion _________________________________________
ERR_NO_USERNAME,
ERR_NO_PASSWORD,
ERR_LOGIN_FAILED

And instead of the code below, you output err keywords:
elseif (!isset($_POST['year']))
  $err = 'form.php?err=Must+Provide+Your+Name;

Becomes:

elseif (!isset($_POST['year']))
  $err = 'form.php?err=ERR_NO_USERNAME;

___________________ end of your earlier suggestion ___________________________________
My question is: Are these (ERR_NO_USERNAME,ERR_NO_PASSWORD,ERR_LOGIN_FAILED) PHP-predefined key words? Or Do I need to define or need to something else?  Can you kindly show me an example if they are not PHP-predefined key words?

Thanks a lot!

duta

May 28, 2006, at 4:27 p.m.





0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16780863
> My question is: Are these (ERR_NO_USERNAME,ERR_NO_PASSWORD,ERR_LOGIN_FAILED) PHP-predefined key
> words? Or Do I need to define or need to something else?  Can you kindly show me an example if they are
> not PHP-predefined key words?

As mentioned. They are not predefined keywords. They are just tokens or identifiers you use instead of plain text. Like you would use id=1 in the url http://mysite.com/users.php?id=1 to identify a user, you use err=ERR_NO_USERNAME to identify an error type/number. You can of course use err=1 or err=applejuice, as long as your corresponding code in form.php transforms the code/identifier to a nice english warning.

-r-

ps: no need to thank me every post. This is what I do at EE ;-)

0
 

Author Comment

by:duta
ID: 16780916
TO: Roonaan:
Thanks again for your kind, prompt response.

I still do not clearly understand what you wrote: "They are just tokens or identifiers you use instead of plain text. Like you would use id=1 in the url http://mysite.com/users.php?id=1 to identify a user, you use err=ERR_NO_USERNAME to identify an error type/number. You can of course use err=1 or err=applejuice, as long as your corresponding code in form.php transforms the code/identifier to a nice english warning."

I just wonder whether you may check my website (http://www.nice-man.com/bogo). I uploaded my script as "survey.zip" file. If you, by any chance, got a few minutes to spare, you may download it and add just one example for how to use ERR_NO_USERNAME.

I know you are always busy, and you may just ignore this request without offending me at all. I am always grateful to all of you who are trying to help a novice like me one way or the other.

Thesedays I am spending days and nights trying to figure out.  I

Thanks!

duta

May 28, 2006, at 4:43 pm

May
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question