473,396 Members | 1,998 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,396 software developers and data experts.

problem with JS validation in PHP generated form

i have 3 pages page1.php page2.php and page3.php

in page1.php user fills a registration form in page2.php the user can review what they have entered and make any changes and

page3.php displays a message that the registration has been completed.

i am having issues with javascript validation in page2.php

in page1.php the javascript validation file is file1.js and in page2.php it is file2.js in page1.php the javascript code i

have written works fine and does all the validation as the form elements in page1.php are written as
<input type="text" name=""> <input type="radio" name=""> ...

in page2.php i am displaying these form elements using php so that user can preview what they have entered and make changes

instead of going back to page1.php the code to display the form elements in page2.php is

<?php
echo "<input type ='hidden' name='username' value='".stripslashes($username)."'>";
echo "<input type =\"text\" name=\"username\" value=\"$username\">";
?>

in page2.php for checkboxes the code is <input type="checkbox" name="" value=""> which is normal html unlike the textfields

above using php

what i have noticed in page2.php is the checkboxes are being validated however the text fields in page2.php do not validate

properly and does not work as it works in page1.php

what i am thinking is because the textfields are being generated using the above php code the javascript is not reading and

validating however i am not sure what the exact reason is.

sample code of javascript for file1.js is

var username = document.registrationform.username
if ((username.value==null) || (username.value=="") || (username.length=="") )
{
alert("Please Enter a User Name")
username.value=""
username.focus()
return false
}

sample code of javascript for file2.js is

var fname=document.registrationform.fname
if ( (fname.value==null) || (fname.value=="") || (fname.length=="") )
{
alert("First Name is required")
return false
}
also sometime if i use fname.value in page2.php i get an error that value is not an object or is null so i have modified the

code in file2.js to some extent


can some one tell me how to go about validating textfields and other form elements in page2.php using php code to be

validated like the checkboxes in page2.php

any help will be greatly appreciated.


also in page2.php i have an insert statement which writes the information that a user has entered in the form into mysql

database. i have read about database injection recently. so i have used $username = addslashes($_POST["username"]);
due to this the value in the database is recorded as example first\name if a user typed first'name in the form should i use
mysql_real_escape_string() method to take extra precaution or is addslashes() method alright. if i adopt

mysql_real_escape_string() do i just read the values simply as $username = mysql_real_escape_string($_POST["username"]);
or do i need to write extra code because of using mysql_real_escape_string() method

thanks.
Feb 25 '08 #1
1 2582
hsriat
1,654 Expert 1GB
instead of going back to page1.php the code to display the form elements in page2.php is

<?php
echo "<input type ='hidden' name='username' value='".stripslashes($username)."'>";
echo "<input type =\"text\" name=\"username\" value=\"$username\">";
?>
Why are you using username as a name of two input fields?

sample code of javascript for file2.js is

var fname=document.registrationform.fname
if ( (fname.value==null) || (fname.value=="") || (fname.length=="") )
{
alert("First Name is required")
return false
}
also sometime if i use fname.value in page2.php i get an error that value is not an object or is null so i have modified the
Where is fname in your HTML for page2.php?
And on which event are you calling this function?
Feb 25 '08 #2

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

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked to...
2
by: Helge Kalnes | last post by:
We are running an ASP.NET application on a cluster of 3 web-servers, using the Network Load Balancing feature of Application Center. We have synchronized the machineKey in machine.config on the 3...
4
by: Alphonse Giambrone | last post by:
I have a simple login page that has two text boxes, two requiredfieldvalidators, a couple of labels and a button. It has been reduced down to almost nothing to troubleshoot the below problem. ...
6
by: The Eeediot | last post by:
Hello, Folks... I'm almost becoming a regular to this newsgroup. I am trying to display the contents of an MS-SQL Text field to a TextBox in ASPdotNET. The text in this field contains all...
2
by: Wysiwyg | last post by:
I'm going back to a previous asp.net (C#) web project after a few months of inactivity and my first form, the login, won't submit. I ran with the debugger and still can't see how to resolve this. I...
1
by: IkBenHet | last post by:
Hello, Currently I am using a large input form on a website that is based on ASP and JavaScript. Depending on the values that are filled in by the user the forms does a refresh and makes...
2
by: Tim Frawley | last post by:
Source code attached indicates my problem with validation and a button bar save button. Fill the Textbox with some text then tab off the control. The message box will display the text in the...
5
by: cbs7 | last post by:
Hi all I'm a complete newbie to web development and Javascript especially. I've been creating a form for a webpage and have used a validation script gen_validatorv2.js which I downloaded from the...
12
by: maya | last post by:
amazing, the stupid things IE does sometimes... inserted a simple <hrtag in sidenav here, www.francesdelrio.com/photoblog_new/india/page9/photos.jsp in FF it looks fine, in IE (both 6 and 7)...
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:
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?
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
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,...

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.