...

View Full Version : javascript dynamic element name validation



gelki
05-18-2012, 10:30 PM
Hi!
I am pretty shore that i have seen a solution (snippet) but could'nt find it anywhere.
I have no problem to server side validate the form fields but are a noob to javascript and kindly hope for someone in forum could share some nice appoach to my problem.

On my site:
almost must see to get the picture, the admin can take the link away if impossible to have here. I just dont know how i can describe it better then showing a real life.
http://www.pvc-fonster.org/pvc-offert/ you see below table with following structur

Pos.-Modell-Glas-Antal-Bredd-Hjd-Mtt-Sprjs-Spaltventil-Persienner-Vning.

On right side you have a button named: "add new row" swedish="ny rad"
its 2 thing i need to do.
1. You can only add new row if all fields have >0
2. If you click on "send this" Swedish="skicka offert frgan" then all fields must be >0 before a submission is trigged.
The problem i have that my form input names are dynamic. like:
<input type="text" name="fHojd_1" value="0">
name="fHojd_1" --> digit 1 is increasing in each adding row. So i really do not have a clue what to do? Or what should i do. Any help would be a life saver for me. :-)


// Sorry for my spelling.
Robert

felgall
05-18-2012, 11:30 PM
Why are you giving the form fields different names rather than setting them up as an array - using an array would make both the server side code and the JavaScript easier.

Old Pedant
05-19-2012, 01:47 AM
Felgall's point is a good one!

It's not commonly done except with checkboxes and radio buttons (required with radio buttons), but it's perfectly legal to give the same name to *any* form fields, and you can even do it when they aren't the same type!

Example:


<form onsubmit="something.php" method="post">
Enter the names of up to 5 friends:
<input name="friend[]"><br/>
<input name="friend[]"><br/>
<input name="friend[]"><br/>
<input name="friend[]"><br/>
<input name="friend[]"><br/>
<input type="submit" value="These are my friends"/>
</form>

Then the PHP code is no different than if you had a group of checkboxes:


<?php
$friends = $_POST["friend"];
$userid = $_SESSION{"userid"];
for ( $f = 0; $f < count($friends); ++$f )
{
$friend = mysql_real_escape_string( $friends[$r] );
$sql = "INSERT INTO friends ( userid, friendname ) "
. " VALUES( $userid, '$friend' )";
mysql_query($sql);
}
?>

If you aren't using PHP--if you are using ASP or JSP for example--you don't need the [] on the ends of the names of those fields. Only PHP isn't smart enough to figure out you have more than one field of the same name by itself.

********

So, Gelki, Felgall is saying that if you gave *ALL* your fields the name fHojd (of fHojd[] if your server-side code is PHP), then you don't have to worry about numbering them.

Old Pedant
05-19-2012, 02:11 AM
Will you eventually be storing the data in a database? If so, on the server side you again just loop through the array to build an sql statement to bulk load the data into the database with a single insert.


Which only works with MySQL. Which iBall conveniently forgets to mention. Likely he doesn't know the SQL needed for other databases.

He is referring to the fact that MySQL allows an INSERT like this:


INSERT INTO friends ( userid, friendname )
VALUES( 1, 'Joe' ),(1,'Sam'),(1,'Old Pedant');

Which is NOT supported by most other DBs. (Possibly PostGres supports it now; it didn't many years ago when I was using it.)

Also... It's really no big deal to do the multiple inserts unless your site is one that is getting hit with hundreds of times per hour with pages that do such inserts. The improvement you get from the above MySQL-only trick is so miniscule that it would be very hard to measure its affect on the average site.

Once again, iBall obviously peeked at my answer (despite his claim that he doesn't read my posts) or he would not have felt the need to be so emphatic in his "Don't fall into the newbie trap..." comment. Which would, of course, be totally inappropriate if, for example, you were using SQL Server as your DB. (Though with SQL Server, there are ways to accomplish a similar trick but that don't work with some DB libraries; the trick wouldn't work with the PHP mySQL library, for example.)

Philip M
05-19-2012, 07:33 AM
That's why I asked if the op will be using php. If yes, then it is very likely he/she will also be using MySQL. If not, the op can post back with the database they might use.



Another example of iBall's slippery evasiveness! He did not ask if the OP was using PHP. He said "If you'll be using php....."

gelki
05-19-2012, 10:10 AM
Thanks!
I am using ordinary asp. Working perfectly for my needs. MySql as saving plattform. Will absolutly convert to PHP when i have time. Reason i did not changes to php is 500000 functions and ready to use codes in asp. So eventuelly i will force my self to abandon asp. Will never ever convert to asp.net. Microsoft are just to greedy for me!

okey heres is my server side code to grab the form.


For Each Item In Request.Form
If Left(Item, 2) = "ID" Then
If Request.Form(Item) = "1" Then
intID = Split(Item, "_")(1)
fModell = Request.Form("fModell_" & intID)
fGlas = Request.Form("fGlas_" & intID)
fAntal = Request.Form("fAntal_" & intID)

if Request.Form("fBredd_" & intID) <> "" then
fBredd = Request.Form("fBredd_" & intID)
fBredd = RemoveSigns(fBredd,1)
fBredd = RmChr(fBredd, lCase("abcdefghijklmnopqrstuvwxyz"))
if IsNumeric(fBredd) then
if cLng(fBredd) = 0 then
fBredd = 0
end if
else
fBredd = 0
end if
else
fBredd = 0
end if

if Request.Form("fHojd_" & intID) <> "" then
fHojd = Request.Form("fHojd_" & intID)
fHojd = RemoveSigns(fHojd,1)
fHojd = RmChr(fHojd, lCase("abcdefghijklmnopqrstuvwxyz"))
if IsNumeric(fHojd) then
if cLng(fHojd) = 0 then
fHojd = 0
end if
else
fHojd = 0
end if
else
fHojd = 0
end if

mSystem = Request.Form("mSystem_" & intID)
fSprojs = Request.Form("fSprojs_" & intID)
fventil = Request.Form("fventil_" & intID)
fPersienner = Request.Form("fPersienner_" & intID)
fplacering = Request.Form("fplacering_" & intID)

update to database code

End If
End If
Next

i am just take a look at:


<?php
$friends = $_POST["friend"];
$userid = $_SESSION{"userid"];
for ( $f = 0; $f < count($friends); ++$f )
{
$friend = mysql_real_escape_string( $friends[$r] );
$sql = "INSERT INTO friends ( userid, friendname ) "
. " VALUES( $userid, '$friend' )";
mysql_query($sql);
}
?>

gelki
05-19-2012, 11:29 AM
In that case I would suggest you do a bulk insert but unfortunately I can't help you with the asp code to build the sql statement. Hopefully old pedant or someone else who knows asp will be able to help.

Okey.
Thanks for your time anyway.
I am really appreciate for all answers.
:-)

Old Pedant
05-19-2012, 10:37 PM
Here's how to do it in ASP, assuming that *ALL* the name fields use name="fHojd" (do *NOT* use the [] with ASP!), then it's easy:



For n = 1 To Request.Form("fHojd").Count
name = Request.Form("fHojd")(n)
SQL = "INSERT INTO tablename ( ..., fHojd ) " _
& " VALUES( ...,' " & Replace(name,"'","''") & "')"
conn.Execute SQL
Next

HOWEVER...

Now that you show us you have FIVE of these numbered fields that have to be accessed from the POST all at the same time... I would personally stick with your original numbered field system.

Why, again, did you want or need to change it???

gelki
05-20-2012, 10:57 AM
Here's how to do it in ASP, assuming that *ALL* the name fields use name="fHojd" (do *NOT* use the [] with ASP!), then it's easy:



For n = 1 To Request.Form("fHojd").Count
name = Request.Form("fHojd")(n)
SQL = "INSERT INTO tablename ( ..., fHojd ) " _
& " VALUES( ...,' " & Replace(name,"'","''") & "')"
conn.Execute SQL
Next

HOWEVER...

Now that you show us you have FIVE of these numbered fields that have to be accessed from the POST all at the same time... I would personally stick with your original numbered field system.

Why, again, did you want or need to change it???

Thanks man!
Yes exactly what you mean. The posted fields with same name will be a trouble becuse this fields needs to be filled out after the post was made with new changes values (Post back and fill the fields correctly fileds by fields in correct row.).

for now i have following in my form section.


For list vailues post back is reciving by:

<select name="fGlas_<%=x%>" id="fGlas_<%=x%>">
<option value="0" <% if request.form("fGlas_"&x) = 0 then response.write("selected") end if%>></option>
<option value="1" <% if request.form("fGlas_"&x) = 1 then response.write("selected") end if%>>2-glas</option>
<option value="2" <% if request.form("fGlas_"&x) = 2 then response.write("selected") end if%>>3-glas</option>
<option value="3" <% if request.form("fGlas_"&x) = 3 then response.write("selected") end if%>>Vet ej</option>
</select>

For text fields:

<input type="text" name="fBredd_<%=x%>" value="<% if request.form("fBredd_"&x) <> "" AND IsNumeric(request.form("fBredd_"&x)) then response.write(request.form("fBredd_"&x)) else response.write("0") end if %>">

How can i put back values on the form changes with same name in arrays?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum