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 Coder
    Join Date
    Jul 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy passing php value to javascript error

    I have a big problem.

    This bit of chunk creates text boxes based on user input.
    PHP Code:
    for($i=0;$i<$cols;$i++)
    {
       echo
    "    <td align=center width=20%>
          <input type = text name=id[$i]>
        </td>
    "

    However, when I want to use javascript, it says id[i] is not an object.

    function addList()
    {
    var cols = <?php echo $cols; ?>;

    for (i=0;i<cols;i++)
    {
    if (document.form.id[i].value == "")
    {
    document.form.id[i].value = document.form.showID.value;
    break;
    }
    }
    }

  • #2
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the problem is that in your php code, [$i] is interpreted as text. Try this instead:

    Code:
    echo "<td align=center width=20%>
          <input type='text' name='id".[$i]."> 
        </td>";


    Does that work?
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #3
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I mean this;

    Code:
     echo "<td align=center width=20%>
          <input type='text' name='id[".$i."]> 
        </td>";
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no it doesn't work.

    What else should i do?

  • #5
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I've got it. The notation [i] usually refers to an array, so that when you use javascript to call document.form.id[i], it might be looking for the input called "id" in form, and then complaining that this isn't an array, so that you can't talk about document.form.id[46] or any other number you can think of.

    You might try giving each input an id (as opposed to a name). Thus, try naming the inputs differently, like

    Code:
     echo "<td align=center width=20%>
          <input type='text' id='hopethisworks".$i."> 
        </td>";
    Now their id's are hopethisworks1, hopethisworks2, etc. In your javascript code you can now try something like:

    Code:
       for (i=0;i<cols;i++) {
      var thisInput = document.getElementById('hopethisworks'+i);
      if (thisInput.value == "") {
              thisInput.value = document.form.showID.value;
                  break;
      }
       }
    Let me know whether this works, and if not, what errors you get.
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #6
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Maybe you should try these code sets?
    PHP Code:
    for ($i 0$i $cols$i++) {
      echo 
    "<td>";
      echo 
    "  <input type=\"text\" id=\"id" $i "\">";
      echo 
    "</td>";
      
    … 
    Code:
    function addList() {
      var cols = <?php echo $cols; ?>;
      for (var i = 0; i < cols; i++) {
        if (!document.getElementById("id" + i).value) {
          document.getElementById("id" + i).value = ;
          break;
          }
        }
      }
    Notes:
    • I don’t know PHP, but I believe that the above PHP is correct.
    • Removed the outdated form‐based element referencing method in favor of the getElementById() referencing method. Changed the name attribute to the id attribute accordingly.
    • Removed deprecated attributes from table cell element. Those properties should be defined via CSS.
    • Declared the variable “i”.
    • Used a better method to test whether a value exists.
    • Added double quotation marks around attribute values as per good practice.
    • Replaced part of the JavaScript with an ellipsis since I don’t know what that part is referencing.


    Edit: TripperTreats beat me to the post.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi ! It worked.

    but then, since i am not using "name" but use "id" instead, how do I identify the variables when I click next page?

    what i mean is that if i use the name="test", on my next page, the variable is known as $test.

  • #8
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you keep the name for referencing when it's submitted, but use the id for the javascript side of things.
    Form inputs need ids so that the 'for' attribute of labels can be understood anyway, so it's good practice from a usability perspective.

  • #9
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Yeah, you can have the name attribute at the same time as the id attribute. Just make sure that they’re identical.
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

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