Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript dynamic element name validation

    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-Höjd-Mått-Spröjs-Spaltventil-Persienner-Våning.

    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 frågan" 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

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,552
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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:
    Code:
    <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:
    Code:
    <?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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,552
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by iBall View Post
    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:
    Code:
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by iBall View Post
    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....."

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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:

    Code:
    <?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);
    }
    ?>

  • #7
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by iBall View Post
    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.
    :-)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,552
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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:

    Code:
    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???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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:

    Code:
    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:
    Code:
    <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:
    Code:
    <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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •