Need help with JS form updates

Discussion in 'JavaScript' started by adzeds, Aug 11, 2011.

  1. #1
    Hi guys,

    I have been struggling away for a fair few hours so have turned to you guys for help..

    Essentially I have a voting script that when the user selects their star rating it saves their vote in a hidden html input and then gets submitted when the user submits the form..

    here is the link I am using on the 1star as an example:
    <a href="javascript:void(0)" onclick="avote(1,pr_'.$ids.'); return false;" class="one-star">1</a>   
    Code (markup):
    As there are many items being reviewed I need to pass a variable for the name of the hidden field? They are pr_0, pr_1 etc.

    The first part is the rating the second is the  name of the hidden id.

    However when I click it nothing appears to happen. I am not sure how to assign the value to my hidden input box. Here is my current attempt at the functions:
    function avote(score, id)
    { 
    document.reviews.pr_+id.value = score; 
    }
    Code (markup):
    All help much appreciated!
     
    adzeds, Aug 11, 2011 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    why not update DB on the fly with AJAX?
     
    astrazone, Aug 11, 2011 IP
  3. Perpetual infinity

    Perpetual infinity Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function avote(score, id)
    { 
    var pr = "pr_"+id;
    document.reviews[pr].value = score; 
    }
    Code (markup):
     
    Perpetual infinity, Aug 17, 2011 IP
  4. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #4
    does it work in all browsers?
    im not saying it doesnt
    just usually i use longer version like document.forms["reviews"].elements["pr_"+id].value
     
    tiamak, Aug 17, 2011 IP
  5. Perpetual infinity

    Perpetual infinity Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    "reviews" is the form name and "pr_"+id is the input name, so the "forms" and "elements" are not needed

    ["reviews"]
    is the same as
    .reviews

    You could even use it in jQuery if you wanted.

    $('element')["html"]("hi");
     
    Perpetual infinity, Aug 18, 2011 IP
  6. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #6
    i know that but as far as i remember i had problems with this shorter version in some old browsers,
    so just in case it is good to know longer version too ;-)
     
    tiamak, Aug 18, 2011 IP
  7. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #7
    document.forms["form_name"].elements["field_name"].value is the best way to do it, especially you are calling a number of elements..

    you can also use document.getElementsByName
     
    JohnnySchultz, Aug 22, 2011 IP