Parse error with php/java script

Discussion in 'Javascript' started by Ian Davies, Nov 22, 2005.

  1. Ian Davies

    Ian Davies Guest

    Hello
    I have found the following script php/java for dynamic menu lists. Where a
    selection from the first updates (filters items in) the other. I have
    modified it for my tables.
    However I am getting an error

    Parse error: parse error, unexpected T_VAR in
    e:\domains\i\iddsoftware.co.uk\user\htdocs\QuestionDB\Questions.php on line
    42

    line 42 being
    var mainselect = document.FormName.MainCategory;

    Im fairly new to php and dont use Java upto now. Can anyone see where I ve
    gone wrong

    <?php
    session_start();
    include("../include/connection.php");
    include("../Secure/login.php");
    $Heading= 'QUESTION SELECTOR';
    $Menu='<a href="../Secure/logout.php">Logout</a><br>';
    include 'HeadQuest.php';


    $Query = "SELECT SubjectNo, SubjectDesc, StatusID FROM Subjects WHERE
    StatusID = 1 ORDER BY SubjectDesc ASC";
    $Result = mysql_query( $Query, $conn );

    ?>

    <HTML>
    <HEAD>
    <SCRIPT language="JavaScript">

    function BodyLoad()
    {

    var select = document.FormName.MainCategory;

    select.options[0] = new Option("Choose One");
    select.options[0].value = 0;

    <?PHP

    $ctr = 1;
    While( $Row = mysql_fetch_array($Result) ) {
    echo "select.options[".$ctr."] = new
    Option(\"".$Row['SubjectNo']."\");\n";
    echo "select.options[".$ctr."].value = \"".$Row['SubjectNo']."\";\n";
    $ctr++;
    }
    ?>
    }

    function Fill_Sub()
    {

    <?PHP
    var mainselect = document.FormName.MainCategory;
    var subselect = document.FormName.SubCategory;

    if( mainselect.options[mainselect.selectedIndex].value != 0 ) {

    subselect.length = 0;
    }

    $Query = "SELECT SubjectNo, SubjectDesc, StatusID FROM Subjects WHERE
    StatusID = 1 ORDER BY SubjectDesc ASC";
    $Result = mysql_query($Query, $conn );

    while( $Row = mysql_fetch_array($Result) ) {
    ?>


    if( mainselect.options[mainselect.selectedIndex].text == "<?PHP echo
    $Row['SubjectNo']; ?>" ) {
    <?PHP

    $Query2 = "SELECT TopicID, TopicDesc, KS, SubjectNo FROM ttopic WHERE
    StatusID = 1 AND SubjectNo = ".$row['SubjectNo']." ORDER BY TopicDesc ASC";


    $Result2 = mysql_query($Query2, $conn );

    $ctr = 0;

    While( $Row2 = mysql_fetch_array($Result2) ) {
    echo "subselect.options[".$ctr."] = new
    Option(\"".$Row2['TopicID']."\");\n";
    echo "subselect.options[".$ctr."].value =
    \"".$Row2['TopicID']."\";\n";
    $ctr++;
    }
    ?>
    }
    }

    <?PHP
    }

    mysql_close($conn);
    ?>
    }

    </SCRIPT>
    </HEAD>

    <BODY onload="BodyLoad();">
    <FORM name="FormName" method="POST" action="">
    <TABLE border="1">
    <TR>
    <TD>Main Category</TD>
    <TD>Sub Category</TD>
    </TR>
    <TR>
    <TD>
    <SELECT name="MainCategory" onchange="Fill_Sub();">
    </SELECT>
    </TD>

    <TD>
    <SELECT name="SubCategory" size="4">
    </SELECT>
    </TD>
    </TR>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>
     
    Ian Davies, Nov 22, 2005
    #1
    1. Advertising

  2. Ian Davies wrote:
    >
    > Parse error: parse error, unexpected T_VAR in
    > e:\domains\i\iddsoftware.co.uk\user\htdocs\QuestionDB\Questions.php on line
    > 42
    >

    <snip>

    > function Fill_Sub()
    > {
    >
    > <?PHP


    This is in the wrong spot...

    > var mainselect = document.FormName.MainCategory;
    > var subselect = document.FormName.SubCategory;
    >
    > if( mainselect.options[mainselect.selectedIndex].value != 0 ) {
    >
    > subselect.length = 0;
    > }


    This is where the "<?php" should be...

    > $Query = "SELECT SubjectNo, SubjectDesc, StatusID FROM Subjects WHERE
    > StatusID = 1 ORDER BY SubjectDesc ASC";
    > $Result = mysql_query($Query, $conn );


    <snip>

    --
    Justin Koivisto, ZCE -
    http://koivi.com
     
    Justin Koivisto, Nov 22, 2005
    #2
    1. Advertising

  3. Ian Davies

    Ian Davies Guest

    Thanks
    That helped
    But now get a syntax error on line 69
    which is

    echo "subselect.options[".$ctr."].value =
    \"".$Row2['TopicID']."\";\n";

    my debugger is saying

    if( mainselect.options[mainselect.selectedIndex].value == "12" ) {
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
    resource in e:\domains\i\mysite\user\htdocs\myfolder\Questions.php on line
    67
    } }

    It would seem that there is a problem with

    mainselect.options[mainselect.selectedIndex].value

    Ian



    "Justin Koivisto" <> wrote in message
    news:...
    > Ian Davies wrote:
    > >
    > > Parse error: parse error, unexpected T_VAR in
    > > e:\domains\i\iddsoftware.co.uk\user\htdocs\QuestionDB\Questions.php on

    line
    > > 42
    > >

    > <snip>
    >
    > > function Fill_Sub()
    > > {
    > >
    > > <?PHP

    >
    > This is in the wrong spot...
    >
    > > var mainselect = document.FormName.MainCategory;
    > > var subselect = document.FormName.SubCategory;
    > >
    > > if( mainselect.options[mainselect.selectedIndex].value != 0 ) {
    > >
    > > subselect.length = 0;
    > > }

    >
    > This is where the "<?php" should be...
    >
    > > $Query = "SELECT SubjectNo, SubjectDesc, StatusID FROM Subjects

    WHERE
    > > StatusID = 1 ORDER BY SubjectDesc ASC";
    > > $Result = mysql_query($Query, $conn );

    >
    > <snip>
    >
    > --
    > Justin Koivisto, ZCE -
    > http://koivi.com
     
    Ian Davies, Nov 22, 2005
    #3
  4. Ian Davies wrote:
    > Thanks
    > That helped
    > But now get a syntax error on line 69
    > which is
    >
    > echo "subselect.options[".$ctr."].value =
    > \"".$Row2['TopicID']."\";\n";
    >
    > my debugger is saying
    >
    > if( mainselect.options[mainselect.selectedIndex].value == "12" ) {
    > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
    > resource in e:\domains\i\mysite\user\htdocs\myfolder\Questions.php on line
    > 67
    > } }
    >
    > It would seem that there is a problem with
    >
    > mainselect.options[mainselect.selectedIndex].value


    Or there's something wrong with the query. You blindly start a while
    loop without first checking to see if the query has succeeded. In this
    case, if $Result isn't a valid result resource, the query failed for
    some reason... to help debug, use mysql_error() to see what the problem is.

    --
    Justin Koivisto, ZCE -
    http://koivi.com
     
    Justin Koivisto, Nov 22, 2005
    #4
  5. Ian Davies

    RobG Guest

    Ian Davies wrote:
    > Hello
    > I have found the following script php/java for dynamic menu lists. Where a
    > selection from the first updates (filters items in) the other. I have
    > modified it for my tables.
    > However I am getting an error


    When posting code, post what is received at the client (use view
    source). The PHP side should be discussed in a PHP forum.

    Once you have sorted out what the client should get, then you can work
    out how to generate it.


    > Parse error: parse error, unexpected T_VAR in
    > e:\domains\i\iddsoftware.co.uk\user\htdocs\QuestionDB\Questions.php on line
    > 42
    >
    > line 42 being
    > var mainselect = document.FormName.MainCategory;
    >
    > Im fairly new to php and dont use Java upto now. Can anyone see where I ve
    > gone wrong


    Asking PHP questions in a JavaScript group? Thinking JavaScript is
    Java? ;-)


    > <?php
    > session_start();
    > include("../include/connection.php");
    > include("../Secure/login.php");
    > $Heading= 'QUESTION SELECTOR';
    > $Menu='<a href="../Secure/logout.php">Logout</a><br>';
    > include 'HeadQuest.php';
    >
    >
    > $Query = "SELECT SubjectNo, SubjectDesc, StatusID FROM Subjects WHERE
    > StatusID = 1 ORDER BY SubjectDesc ASC";
    > $Result = mysql_query( $Query, $conn );
    >
    > ?>
    >
    > <HTML>
    > <HEAD>
    > <SCRIPT language="JavaScript">


    The language attribute is deprecated, type is required:

    <script type="text/javascript">


    >
    > function BodyLoad()
    > {
    >
    > var select = document.FormName.MainCategory;
    >
    > select.options[0] = new Option("Choose One");
    > select.options[0].value = 0;


    IE has issues with setting option attributes this way. Set all the
    values in one go (hey, saves a line of code too):

    select.options[0] = new Option("Choose One", "0", true, true);


    sets the first option to have text 'Choose One', value as '0', default
    selected as true and currently selected as true.


    >
    > <?PHP
    >
    > $ctr = 1;
    > While( $Row = mysql_fetch_array($Result) ) {
    > echo "select.options[".$ctr."] = new
    > Option(\"".$Row['SubjectNo']."\");\n";
    > echo "select.options[".$ctr."].value = \"".$Row['SubjectNo']."\";\n";
    > $ctr++;
    > }
    > ?>
    > }
    >
    > function Fill_Sub()
    > {
    >
    > <?PHP
    > var mainselect = document.FormName.MainCategory;
    > var subselect = document.FormName.SubCategory;
    >
    > if( mainselect.options[mainselect.selectedIndex].value != 0 ) {


    The value of a form control is always returned as a string, so be
    careful when evaluating it. In this case it is OK because the string
    '0' and number 0 will be evaluated as you expect, but sometimes it will
    trip you up.


    >
    > subselect.length = 0;
    > }

    [...]
    > <TD>
    > <SELECT name="SubCategory" size="4">
    > </SELECT>
    > </TD>


    A select element with no options is invalid HTML.

    [...]


    --
    Rob
     
    RobG, Nov 23, 2005
    #5
  6. RobG wrote:
    > When posting code, post what is received at the client (use view
    > source). The PHP side should be discussed in a PHP forum.


    It was - 4 of them.

    > Asking PHP questions in a JavaScript group? Thinking JavaScript is
    > Java? ;-)


    More like someone not sure if it was a php or js thing. The post went to
    4 php groups, and one js group...

    --
    Justin Koivisto, ZCE -
    http://koivi.com
     
    Justin Koivisto, Nov 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    1,154
    Daniel Vallstrom
    Mar 15, 2005
  2. raviraj joshi
    Replies:
    0
    Views:
    342
    raviraj joshi
    Jul 4, 2009
  3. reshma shinde
    Replies:
    0
    Views:
    418
    reshma shinde
    Jul 4, 2009
  4. Rajive Narain
    Replies:
    0
    Views:
    1,988
    Rajive Narain
    Sep 18, 2009
  5. raviraj joshi
    Replies:
    0
    Views:
    174
    raviraj joshi
    Jul 4, 2009
Loading...

Share This Page