473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

alert user of error/missing data in form?

118 100+
Hi folks, I have this site and idea

http://www.helraizer.co.uk/mychatbox/

at the moment I'm using javascript to alert the user if they haven't entered a username or message but all this does is popup an alert message and return false. However, if their browser does not support javascript (javascript is turned off) then it just won't send and will give them no reason why.

How would I get it so that, using php, if the submit button is pressed and either the username is less than 3 characters and/or the comment field is less than 5 characters then above the image (on the site page) a list of errors would appear.

So if nothing was wrong it shows no errors but if the user name was less than 3 characters and comment <5 it'd appear
"Error!
-Your username is too short, please enter a user name between 3 and 10 characters.
-Your comment is too short, please enter a comment between 5 and 75 characters." (or similar)

How would I do this?

I know this is a "help" forum not a "do it for me forum" but if you guys can give me a baseline for this problem, to work with from there I can probably get it from there. =)

Thanks,
Sam
Feb 4 '08 #1
2 2562
eragon
431 256MB
PHP can not make an alert box, but it can print an error to the page.

are your boxes submitting to an input page, if so i would suggest looking at the php function strlen(). pass both inputs through this, and it its less than what is expected die() your error.

example:

[PHP]$str1 = "Apple";
$str2 = "Banana";

$len1 = strlen($str1);
$len2 = strlen($str2);

if($len1 < 6) {
die('Apple is less than 5 chars.');
}

if($len2 < 4) {
die('Banana is less than 4 chars.');
}[/PHP]

so if you want to figure it out there you go, if not ask and ill work it into your code.
Feb 4 '08 #2
helraizer1
118 100+
PHP can not make an alert box, but it can print an error to the page.

are your boxes submitting to an input page, if so i would suggest looking at the php function strlen(). pass both inputs through this, and it its less than what is expected die() your error.

example:

[PHP]$str1 = "Apple";
$str2 = "Banana";

$len1 = strlen($str1);
$len2 = strlen($str2);

if($len1 < 6) {
die('Apple is less than 5 chars.');
}

if($len2 < 4) {
die('Banana is less than 4 chars.');
}[/PHP]

so if you want to figure it out there you go, if not ask and ill work it into your code.

Hey Eragon, thanks for that. I worked out a solution for the problem =)

This is the php code I used, it seems to work with out any bugs so far, could you let me know if it's good enough or if there is any way it could be improved?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. unset($errors);
  4.  
  5. include("linesfile.php5");
  6.             $filename = "data.line";
  7.             set_magic_quotes_runtime(0);
  8.  
  9.             if($_POST['submit'] && strlen($_POST['input']) < 5) {    
  10.                 $errors[] .= _NO_5;
  11.                             }
  12.             if($_POST['submit'] && strlen($_POST['username']) < 3) {
  13.                 $errors[] .= _NO_2;
  14.                             }
  15.  
  16. ?>
  17.               <div class="ddgb_entrybox">
  18.         <table width="100%" border="0" cellspacing="8" cellpadding="0">
  19.         <tr>
  20.         <td width="42%" align="right" valign="top"></td>
  21.         <td align="left" valign="top">
  22. <?php  
  23.  
  24. if(isset($_POST['submit']) && $errors[0] != null) {
  25.      echo "<h2>" . _ERROR . "</h2><ul>";
  26.         foreach ($errors as $f) {
  27.             echo "<li>" . $f . "</li>";
  28.         }
  29.         echo "</ul>";
  30. }    
  31.  
  32.             elseif($_POST['submit']) {
  33.             // grab the inputted text
  34.             $text = htmlspecialchars(stripcslashes($_POST['input'] . "\n"));
  35.             $username = htmlspecialchars(stripslashes($_POST['username']));
  36.             $color = $_POST['color'];
  37.             $font = $_POST['font'];
  38.             $ip = $_SERVER['REMOTE_ADDR'] . "\n";
  39.             $_SESSION['username'] = $username;
  40.             $_SESSION['color'] = $color;
  41.                     $dirty = array(); // dirty words here, I removed them to keep this thread clean
  42.  
  43.                         foreach($dirty AS $bad_word){
  44.                         $text = preg_replace("/$bad_word/i","****", $text);
  45.                         }   
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.             $data[] ="\n". htmlspecialchars_decode(substr($username,0,10));
  53.             $data[] =trim($color);
  54.             $data[] =trim($font);
  55.             $data[] =htmlspecialchars_decode(trim(substr($text,0,75)));
  56.  
  57.  
  58.             $datafile = new DataFile($filename);
  59.             if(!$datafile->writeNewLine($data)) die("Error writing to file");
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67.           ?>
  68.  
_ERROR
_NO_5
_NO_2
have all been defined in a different document.

Sam
Feb 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: monika | last post by:
I am not clear ... whether to use alert of JavaScript or messagebox of VBScript in ASP. But what I want is that when a user has some error I want to display a pop-up kind of a thing saying "you...
9
by: Justin Koivisto | last post by:
Is there a way to create an alert-like message box that uses a custom image? Basically, I want to replace the default ! image with something else. -- Justin Koivisto - spam@koivi.com PHP...
3
by: elaines | last post by:
I can't seem to get this to work. Any help appreciated. The alert message always comes up. Here is my code <SCRIPT type="text/javascript"> function fte() { var hours =...
6
by: bonehead | last post by:
Greetings, I'm working on an e-mail form (btw many thanks to Philip Ronan for the very cool email address format tester function, best I've seen so far). I've been trying, with limited...
0
by: Tom | last post by:
I am having a really annoying issue with serialization and a .NET User Control I am writing. For example, let's say I have a couple of classes in my control - first class is like: Public Class...
8
by: simon | last post by:
hello. hopefully another quick newbie question for you.... I have a codebehind function coded to handle the click event of a button. what this function does is insert values from a data grid...
4
by: jehugaleahsa | last post by:
Hello: I am going through an effort to standardize web applications for my company. We are trying to figure out the best practices for security, error handling, etc. I've just done a large...
2
by: Jonathan N. Little | last post by:
As part of a JavaScript precheck form validation I noticed a problem with trying to return focus to the field with an error. I have setup a demo page. ...
57
by: bonneylake | last post by:
Hey Everyone, well was hoping if someone could explain to me how to add an alert for the description fields of this multiple attachments script. The problem i think i am having is i don't know how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.