473,657 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

alert user of error/missing data in form?

118 New Member
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 2569
eragon
431 Contributor
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 New Member
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
33774
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 have not entered all the values" ... and when he click "ok" then he should be directed to the same page where he was entering the values. I tried this but in my case I am just being redirected to the page ..and my message box in not being popped up!...
9
4980
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 POSTERS: Please use comp.lang.php for PHP related questions, alt.php* groups are not recommended.
3
1430
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 = document.form1.hours.value //get user input for hours var weekdays = document.form1.weekdays.value //get user input for days
6
3132
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 success, to produce a javascript alert box which shows the user a validation error message. The error message itself is dynamic, i.e., it's contained within a php variable. I seem to be running into two issues:
0
2009
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 User Private _UserName As String Private _UserCity As String <System.ComponentModel.CategoryAttribute("User Info")> _ Public Property UserName() As String Get
8
1943
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 into the database. after that is done i want to send the user to PageC, but before i do that i check a variable value. if that value is "0" then i want to raise a javascript alert telling the user they need to set some info in PageA and then...
4
2978
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 amount of work to figure out the built-in ASP .NET security features (which was fairly easy to do). So now I am tackling error handling. What we would like to do is show a JavaScript Alert box with the error information in it. Here is the code I...
2
2473
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. http://www.littleworksstudio.com/temp/usenet/focus.html To simulate the problem, just enter or change the "first" textbox and either click or tab to another control., it is tied to the onchange event. An alert box that simulates the "warning". When you close it the focus will not move...
57
5176
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 to make it check multiple fields to make sure each field has been checked for a description before submitting. Here is what i have on my form page <input type="file" name="attachment1" id="attachments" value="1"...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.