473,654 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with JS validation in PHP generated form

54 New Member
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='".strips lashes($usernam e)."'>";
echo "<input type =\"text\" name=\"username \" value=\"$userna me\">";
?>

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.regist rationform.user name
if ((username.valu e==null) || (username.value =="") || (username.lengt h=="") )
{
alert("Please Enter a User Name")
username.value= ""
username.focus( )
return false
}

sample code of javascript for file2.js is

var fname=document. registrationfor m.fname
if ( (fname.value==n ull) || (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($_PO ST["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_esca pe_string() method to take extra precaution or is addslashes() method alright. if i adopt

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

thanks.
Feb 25 '08 #1
1 2598
hsriat
1,654 Recognized Expert Top Contributor
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='".strips lashes($usernam e)."'>";
echo "<input type =\"text\" name=\"username\" value=\"$userna me\">";
?>
Why are you using username as a name of two input fields?

sample code of javascript for file2.js is

var fname=document. registrationfor m.fname
if ( (fname.value==n ull) || (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
5188
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 complete in connection with attendance at a seminar. After spending more than 15 minutes on it, I clicked on the submit button - and nothing happened. Looking round the pages on Javascript form validation that Google produced for me (well,...
2
3428
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 web-servers, so we can utilize a random algorithm for server selection for requests. The application does not use either session state or application state. We recently upgraded to .NET Framework 1.1 (including the hotfix for the "doPostback...
4
1407
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. Added to a project on development machine (Win XP Pro, IIS 5.1, CLR 1.1.4322), works fine. Deployed to a shared web host (IIS 5.1, CLR 1.1.4322), works fine. Added to different project on same development machine, works fine. Deployed to 2nd...
6
2243
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 sorts of characters including cheverons (i.e. ">" and "<") and occasionally I get the following error condition (listed below). Is there anything I can do to avoid it? I use the simple line txtArticle.Text = datareader("Article") in my code to populate...
2
2096
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 am developing with VS2003 and on Windows 2000 Server and have all of the service packs for .NET and Windows. The application is running on an intranet and I'm running this test with IE6. Javascript is functional. The logon form has text fields...
1
3252
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 other input fields available to fill in. I use the JavaScript OnChange function (=clientside) that creates a querystring and does a refresh of the page. It works fine.
2
6702
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 textbox from the Validating event. Now put the focus back on the textbox click the button bar button. Nothing happens. The form causes validation is true, so is the button bar and the control.
5
19069
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 zip file referenced on http://www.javascript-coder.com/html-form/javascript-form-validation.phtml I managed to get everything working and was testing through Firefox that alerts were generated. However at the very end I thought I'd better check...
12
2215
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) it's messed up.. <hrgets stretched out as far as the width of the window.. (in the interests of "fluid" design I have not given the sidenav div a fixed width.. now this is causing me problems in IE... oh brother...)
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
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
8814
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
8706
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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
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.