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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Make the options of preceding fields dependent on the previous options selected dyn.

    Hello friends,
    I'm having a problem in trying to update the PRECEDING entries of the (SELECT OPTION)fields OF A FORM depending on the previous choice of the user on the same page (without refreshing the page).

    I'm having a setup of Postgresql 7.1 as the database PHP 4 is the front end and apche server 4.

    I tried using a combination of javascript and php but couldnt get it right .There is some sort of a syntactic error or something or the way im supossed to embed javascript within php and vice versa.The following is what I have tried.

    Here category is one of the fields and the rest of the select fields are supposed to be generated dynamically depending on the input of category (every select field depends on the previous one)

    <html>
    <head>
    <script language ="JavaScript" >
    function update()
    {
    a=document.catmod.cat_type.value;
    var x=new Array()
    var d=new Array();
    var cnt=0;
    var count=0;
    var category=document.catmod.cat_type[selectedIndex].value;
    <?php
    $db_handle =include("dbconnect.php");
    $q4="select A.type_no,A.type_name,B.mod_no, B.mod_name from category as A, module as B, contains as C where (A.type_no=C.type_no) and (B.mod_no=C.mod_no) ";
    $result4=pg_exec($db_handle,$q4);
    echo"pg_numrows($result4)";
    ?>
    var y='<?=pg_numrows($result4)?>';
    alert("y");
    //echo"<select>";
    <?php for($row=0;$row<pg_numrows($result4);$row++)
    {
    $val=pg_fetch_array($result4,$row);
    $val1=pg_result($result4,$row,'mod_name');
    ?>
    alert("gfd");
    if(a=="<?php echo $val['type_no']?>")

    x='<?=$val1?>';
    <?php } ?>
    document.catmod.mod_type.text=x;
    alert(document.catmod.mod_type.text);
    document.write(x);
    if(a=="<?php echo $val['type_no']?>")
    <?php
    echo"$val1";
    echo"<option>$val['mod_name']</option>";
    }?>
    }

    </script>
    </head>
    <body>
    <form method="POST" name="catmod" >
    <p><font face="MS Serif" size="5">CATEGORY</font><font size="6" face="MS Serif">&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
    <select name="cat_type" OnChange="update()">
    </select>

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    All of the PHP code in your javascript function has run before the browser receives the page, it simply does not work like that, if you want to do this without a page refresh you need to look into AJAX or similar XMLHttpRequest based technologies
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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