Ok, as you can tell, i'm a newbie🙂 Hence why i'm here!

I have created a PHP form with HTML and Javascript all on the page.

Now, I am having problems with a variable that does not seem to be defined according to my java script.

here is the code for the Variable, it is in HTML but echoed threw PHP:
===========PHP CODE====================
echo "<TR bgColor=#cccccc>
<TD>&nbsp;</TD>
<TD colSpan=45 align=right><B>TOTAL</B></TD>
<TD colSpan=10 align=center><input size=15 name=ProfTotalOne></TD>
<TD ColSpan=10 align=center><input size=15 name=ProfTotalTwo></TD>
<TR>
<TD>&nbsp;</TD>
<TD colSpan=45></TD>
<TD colSpan=10 align=center>
<INPUT type=BUTTON name=CalcButtonOne value=Calculate onClick=Calc(1);>
</TD>
<TD colSpan=10 align=center>
<INPUT type=BUTTON name=CalcButtonTwo value=Calculate onClick=Calc(2);>
</TD>";
===========END PHP CODE====================

Ok here is the JAVASCRIPT that is requesting the use of the variable that is used in the code above

============JAVASCRIPT=====================

					 ProfTotalOne.value =  avg(countAvailable(selCollection),selCollection) + "%";

===========END JAVASCRIPT===================

For some reason when I run this code, my javascript keeps telling me that my variable <b>ProfTotalOne</b> is undefined.

Why?

I have other HTML variables that i create using PHP when i load the form up and they work fine. This is a text feild and it doesn't want to work.

Thanx in advance
Ulysses

    A few points...

    1. use the [php] and [/php] tags to show your code...

    2. if you use heredoc syntax, your pages will be easier to read, and will allow you to use quotes without escaping.

    3. use quotes around each of your HTML attributes.

    4. be careful to close your tags properly. You are missing a couple.

    5. use 'id=', not 'name=' if you want Javascript to access your document objects.

    6. I am sure you don't mean to use colspan in these tags. Use width instead. colspan tells the cell to SPAN that many columns.

    echo <<<EOS
    <TR bgColor="#cccccc"> 
      <TD> </TD> 
      <TD width="45" align="right">
        <B>TOTAL</B>
      </TD> 
      <TD width="10" align="center">
        <input size="15" id="ProfTotalOne">
      </TD> 
      <TD width="10" align="center">
        <input size="15" id="ProfTotalTwo">
      </TD>
    </TR>  <--- this was missing
    <TR> 
      <TD> </TD> 
      <TD width="45"></TD> 
      <TD width="10" align="center">
        <INPUT type="BUTTON" id="CalcButtonOne"
               value="Calculate" onClick="Calc(1);"> 
      </TD> 
      <TD width="10" align="center"> 
        <INPUT type="BUTTON" id="CalcButtonTwo"
               value="Calculate" onClick="Calc(2);"> 
      </TD>
    </TR> <--- this was missing
    EOS;
    

    You are also missing your <table> tags, but I'm going to assume here that you just gave us part of your code...

    The fifth point above is most likely why you didn't have access to the element. Try the changes I suggested and let us know if it worked.

      
      $my_var <<< EOS 
      <TR bgColor="#cccccc">
      		<TD>&nbsp;</TD>
      		<TD width="15" align="right"><B>TOTAL</B></TD>
      		<TD width="10" align="center"><input size="15" name="ProfTotalOne"></TD>
      		<TD width="10" align="center"><input size="15" name="ProfTotalTwo"></TD>
      </TR>
      <TR>
       <TD>&nbsp;</TD>
       <TD width="45"></TD>
       <TD width="10" align="center">
       <INPUT type="BUTTON" name="CalcButtonOne" value="Calculate" onClick="Calc(1);">
       </TD>
       <TD colSpan="10" align="center">
       <INPUT type="BUTTON" name="CalcButtonTwo" value="Calculate" onClick="Calc(2);">
       </TD>
      </TR>
      </table>
      EOS;
      
      

      I am trying to switch over to heredoc syntac that you suggested. Looks good, i agree.

      I keep getting a parseing error on the first line of code.

      What am i doing wrong?

        The problem is the space between <<< and EOS, and you are missing an = sign. There can be no space, and make sure there is no space AFTER "EOS" either.

        Incidentally, "EOS" is just what I use (End Of String). You can use anything you want:

        $myString = <<<BABALOU
        your stuff
        goes here
        BABALOU;

        Note that everything inside the string in heredoc syntax is blue. This is because the PHP parser on this site recognizes the proper syntax. Your code is NOT all blue, showing that there is an error in your syntax. If you edit your post, put the = sign in and remove the space, it should be fine.

          
          create_Prof_Questions($result);  
          
          $my_var=<<<EOS 
          <TR bgColor="#cccccc">
          		<TD>&nbsp;</TD>
          		<TD width="15" align="right"><B>TOTAL</B></TD>
          		<TD width="10" align="center"><input size="15" id="ProfTotalOne"></TD>
          		<TD width="10" align="center"><input size="15" id="ProfTotalTwo"></TD>
          </TR>
          <TR>
           <TD>&nbsp;</TD>
           <TD width="45"></TD>
           <TD width="10" align="center">
           <INPUT type="BUTTON" id="CalcButtonOne" value="Calculate" onClick="Calc(1);">
           </TD>
           <TD width="10" align="center">
           <INPUT type="BUTTON" id="CalcButtonTwo" value="Calculate" onClick="Calc(2);">
           </TD>
          </TR>
          </table>
          EOS;
          

          Ok here is the code now. SAME ISSUE.
          I have a PHP function call before i use the heredoc syntac.
          it just goes like this:

          function create_Prof_Questions($result ) {
          
          
          $cnt = 1; //Counter to increment array
          $clr = 0; //Switches color 0=99999  1=CCCCCCC
          while ($row = mysql_fetch_array($result) ) {
          
          			if($clr == 0) {
          					 echo "<TR bgColor=#999999>";
          						 $clr=1;
          			} else {
          						 print'<TR bgColor=#CCCCCC>';
          						 $clr=0;
          			}
          							echo "<TD valign=top> $cnt </TD>
          							<TD width=45>$row[questions]</TD>
          							<TD width=10 valign=top><SELECT name=ProChoices1>
          							<OPTION value=N/A selected>N/A</OPTION>
          							<OPTION value=0>0 - Never</OPTION>
          							<OPTION value=25>25 - Seldom</OPTION>
          							<OPTION value=50>50 - Occasionally</OPTION>
          							<OPTION value=75>75 - Often</OPTION>
          							<OPTION value=100>100 - Always</OPTION>
          							</SELECT>
          							</TD>
          							</TR>";
          							$cnt++;
           }
          }
          
          

          what are your thoughts?

          ulysses

            Ok, anyone out there?

            I am using the heredoc syntac now. But it still does not recognize my text box.

            any help?

            ulysses

              $my_var=<<<EOS  

              You are setting the heredoc = to a my_var. Where are you printing my_var?

                weevil,

                Thank you for the help.

                I actually now have the heredoc working now.

                What I was refering to was that my JavaScript still does not recognize my text field variable.

                every time i try and use it, the javascript returns an error saying undefined.

                ulysses

                  Use ID=, not NAME= in your tags.

                  <input type="text" id="myTextField">
                  NOT
                  <input type="text" name="myTextField">

                  If you are attempting to access the select field, then you need to do it like this:

                  prochoice = document.myform.ProChoices1.options(document.myform.ProChoices1.selectedIndex).value;

                  Ain't Javascript fun?


                    ===========JAVASCRIPT==========================
                    function Calc(i) {

                    selCollection = document.getElementsByName("ProChoices1");
                    ProfTotalOne.value = <---ERROR HERE
                    avg(countAvailable(selCollection),selCollection) + "%";
                    }

                    
                    =======THIS IS THE PHP CODE I'M HAVING ISSUES WITH====
                    
                    echo <<< EOS
                    <TABLE summary="Professionalism">
                    <TR bgColor="#cccccc">
                    <TD>&nbsp;</TD>
                    <TD width="600"><b>Professionalism</b></TD>
                    <TD align="middle" width="45"><b>Call 1</b></TD>
                    <TD align="middle" width="45"><b>Call 2</b></TD>
                    EOS;
                    	//Call the creation of the Questions Tables
                    	create_Prof_Questions($result);
                    
                    echo <<< EOS
                     <TR bgColor="#cccccc">
                     <TD>&nbsp;</TD>
                     <TD width="600" align="right"><B>TOTAL</B></TD>
                     [COLOR=red]<TD width="45" align="center"><input size="15" id="ProfTotalOne"></TD> <---ERROR HERE FROM JAVASCRIPT[/COLOR]
                     <TD width="45" align="center"><input size="15" id="ProfTotalTwo"></TD>
                     </TR>
                     <TR>
                     <TD>&nbsp;</TD>
                     <TD width="600"></TD>
                     <TD width="45" align="center">
                     <INPUT type="BUTTON" id="CalcButtonOne" value="Calculate" onClick="Calc(1);">
                     </TD>
                     <TD width="45" align="center">
                     <INPUT type="BUTTON" id="CalcButtonTwo" value="Calculate" onClick="Calc(2);">
                     </TD>
                     </TR>
                    </table>
                    EOS;
                    
                    
                    //This function displays the values of the Questions	
                    function create_Prof_Questions($result ) {
                    
                    
                    $cnt = 1; //Counter to increment array
                    $clr = 0; //Switches color 0=99999  1=CCCCCCC
                    while ($row = mysql_fetch_array($result) ) {
                    
                    if($clr == 0) {
                    echo <<< EOS
                      <TR bgColor="#999999">
                    EOS;
                    $clr=1;
                    } else {
                    echo <<< EOS
                     <TR bgColor="#CCCCCC">
                    EOS;
                    $clr=0;
                    } //End Else
                    
                    echo <<< EOS
                     <TD width="10" valign="top"> $cnt </TD>
                     <TD width="600">$row[questions]</TD>
                     <TD width="25" valign="top"><SELECT name="ProChoices1">
                     <OPTION value="N/A" selected>N/A</OPTION>
                     <OPTION value="0">0 - Never</OPTION>
                     <OPTION value="25">25 - Seldom</OPTION>
                     <OPTION value="50">50 - Occasionally</OPTION>
                     <OPTION value="75">75 - Often</OPTION>
                     <OPTION value="100">100 - Always</OPTION>
                     </SELECT>
                     </TD>
                     </TR>
                    EOS;
                    $cnt++;
                    }//End IF
                    
                    }//END Function	
                    
                    

                    OK here is the code that I am using. Well at least part of it. This is for one of the errors. i'll deal with the second one AFTER i've delt with this error.

                    So, i've marked were I add the object(textField) as well as where the Javascript gives me the error.

                    Any thoughts?

                    Ulysses

                      Ulysses, I realize things like this are frustrating. You are coming off a little bit short and "huffy," and I'm sure it's due to frustration, but remember: we are here to help each other but are under no obligation, and this IS a PHP forum, not Javascript. In most forums you would have been flamed by now, called a newbie, and virtually spit upon.

                      That said, I would venture to guess that you need to use the Document Object Model (DOM) correctly. To get to that field, you need to refer to the whole "path" to it. Imagine if you told a program to look for a file in the "images" directory on your computer. If you didn't specify precisely (c:\multimedia\images), your program would give you an error. I don't know what your form is named, so I'm going to assume it's called "myForm." To get to your field, use:

                      document.myForm.ProfTotalOne.value

                      If you don't understand the DOM, I suggest you take a look at www.devguru.com or www.webmonkey.com .

                      HTH

                        Originally posted by BuzzLY
                        Ulysses, I realize things like this are frustrating. You are coming off a little bit short and "huffy," and I'm sure it's due to frustration, but remember: we are here to help each other but are under no obligation, and this IS a PHP forum, not Javascript. In most forums you would have been flamed by now, called a newbie, and virtually spit upon.

                        That said, I would venture to guess that you need to use the Document Object Model (DOM) correctly. To get to that field, you need to refer to the whole "path" to it. Imagine if you told a program to look for a file in the "images" directory on your computer. If you didn't specify precisely (c:\multimedia\images), your program would give you an error. I don't know what your form is named, so I'm going to assume it's called "myForm." To get to your field, use:

                        document.myForm.ProfTotalOne.value

                        If you don't understand the DOM, I suggest you take a look at www.devguru.com or www.webmonkey.com .

                        HTH

                        Hey BuzzLY ,

                        Sorry if i came accross quick and huffy, not my intentions at all. I appretiate all the help i can get, you are right this is very frusturating, specialy sinc e I had it working not to long ago and for some reason now, it won't work.

                        I guess why i came off so 'huffy' as you put it was because I do this all at work. And at the time of the post I was just trying to get it out there as fast as possible so maybe I'd have a responce before I went home and I could fix it up. My appologies. I didnt' think it was that bad, i'll double check next time. It's just crazy at work and then i'm working on this project on the side.

                        Thanx for all the help. I understand the javascript part that you say. So basicly this is a javascript, NOT a PHP decleration error? I wasn't sure what was wrong the JS or the PHP but you believe it's the Javascript so i'll look to fix that hten maybe.

                        i'm off to bed. I'll catch up in the morning.

                        Ulysses

                          YESSS!!!!!!!!!!!!!!!!!!!!!

                          How stupid do I feel?:???

                          You want to know what the issue was?

                          do ya?

                          come on..i konw you do😛

                          i didn't ahve a FORM name in my HTML form tag!!!! Can you believe it?

                          so now my variable reads

                          myform.variablename.value

                          and it works PERFECT@!!!!

                          i can't believe this. I'm soooooooooooo sorry I waisted your time. I will stick around and hope i can help others on this form.

                          Thank you again,
                          sincerly
                          Ulysses Freeman

                            It's not a waste of time. Incidentally, you can refer to your forms on your page as an array. If that was the first (or only) form on the page, you could have used document.forms[0].variablename.value. Just an FYI...

                              Write a Reply...