Hi trying to create a list structure list "nvidia/ati" had before you select something in one list and then the second list updates with values based on you selection. It will not be drivers and static items tho.

So the purpose of this is to get a tree structure from members so the first list will be a list with all members im in first list to be parent to.

So if the first list is:
Tom
Jay
Markus

If i select Tom, i sould from a mysql query get all children to Tom, and so on in 5 levels.

Currently I have this bit of code:

<?php
require_once('auth.php');
require_once('config.php');
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Login Form</title>
        <link href="loginmodule.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="main">
            <h1>Register Sale </h1>
            <a href="member-profile.php">My Profile</a> | <a href="sale-page.php">Sale</a> | <a href="team-page.php">Team</a> | <a href="logout.php">Logout</a>
            <?php
            if (isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) > 0) {
                echo '<ul class="err">';
                foreach ($_SESSION['ERRMSG_ARR'] as $msg) {
                    echo '<li>', $msg, '</li>';
                }
                echo '</ul>';
                unset($_SESSION['ERRMSG_ARR']);
            }
            ?>


            <?php
            $team_connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
            if (!$team_connect) {
                die("Failed to connect to server" . mysql_error());
            }
            $db_team = mysql_select_db(DB_DATABASE);
            if (!$db_team) {
                die("Could not fetch database");
            }


            $team_qry = "SELECT member_id, firstname FROM members";
            $result = mysql_query($team_qry);
            if (!$result) {
                print "EMPTY ARRAY";
            }

            $options = "";

            while ($row = mysql_fetch_array($result)) {

                $id = $row["member_id"];
                $thing = $row["firstname"];
                $options.="<OPTION VALUE=\"$id\">" . $thing . '</option>';
            }
            ?>

            <?php
                function myfunction(){
                echo("<strong>I love PHP !</strong><br>");
                }
                ?>
             <br /><br />

            
            <td>
                <select name="jlist" onClick="myfunction()" size="3">
                    <?php echo $options ?>
                </select>
            </td>
            <td>
                <select name="jlist" onClick="myfunction()" size="10">
                    <?php echo $options ?>
                </select>

            </td>
            <td>
                <select name="jlist" onClick="myfunction()" size="10">
                    <?php echo $options ?>
                </select>

            </td>
            <td>
                <select name="jlist" onClick="myfunction()" size="10">
                    <?php echo $options ?>
                </select>

            </td>
            <td>
                <select name="jlist" onClick="myfunction()" size="10">
                    <?php echo $options ?>
                </select>

            </td>
            <?php
                    echo date('Ymd');
            ?>
            <h1></h1><i>Password Protected Area  </i></div>
    </body>
</html>

But I cant even get I love php to print out when i select something.. would love som help.. :)

Recommended Answers

All 5 Replies

You are mixing javascript and php. This is not possible.

PHP is a server-side language where as javascript is client side. They cannot communicate with each other directly. Your best bet would be AJAX, which is the workaround to your problem.

Since your experience with web development is limited, trying to use AJAX might be a nightmare for you though.

<?php
require_once 'config.php';
require_once 'auth.php';
?>

<?php
$team_connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$team_connect) {
    die("Failed to connect to server" . mysql_error());
}
$db_team = mysql_select_db(DB_DATABASE);
if (!$db_team) {
    die("Could not fetch database");
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Login Form</title>
        <link href="loginmodule.css" rel="stylesheet" type="text/css" />
        <script language=JavaScript>
            function reload(form){
                var val=form.cat.options[form.cat.options.selectedIndex].value;
                self.location='my-team.php?cat=' + val ;
            }

            function reload2(form){
                var val=form.cat.options[form.cat.options.selectedIndex].value;
                var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
                self.location='my-team.php?cat=' + val + '&cat2=' + val2 ;
            }
            function reload3(form){
                var val=form.cat.options[form.cat.options.selectedIndex].value;
                var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
                var val3=form.subsubcat.options[form.subsubcat.options.selectedIndex].value;
                self.location='my-team.php?cat=' + val + '&cat2=' + val2 + '&cat3=' +val3;
            }
            function reload4(form){
                var val=form.cat.options[form.cat.options.selectedIndex].value;
                var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
                var val3=form.subsubcat.options[form.subsubcat.options.selectedIndex].value;
                var val4=form.subsubsubcat.options[form.subsubsubcat.options.selectedIndex].value;
                self.location='my-team.php?cat=' + val + '&cat2=' + val2 + '&cat3=' + val3 + '&cat4=' + val4;
            }
            function reload5(form){
                var val=form.cat.options[form.cat.options.selectedIndex].value;
                var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
                var val3=form.subsubcat.options[form.subsubcat.options.selectedIndex].value;
                var val4=form.subsubsubcat.options[form.subsubsubcat.options.selectedIndex].value;
                var val5=form.subsubsubsubcat.options[form.subsubsubsubcat.options.selectedIndex].value;
                self.location='my-team.php?cat=' + val + '&cat2=' + val2 + '&cat3=' + val3 + '&cat4=' + val4 + '&cat5=' + val5;
            }
        </script>
    </head>
    <body>
        <div id="main">
            <h1>My Team</h1>
            <a href="member-profile.php">My Profile</a> | <a href="sale-page.php">Sale</a> | <a href="team-page.php">Team</a> | <a href="logout.php">Logout</a>
            <br />
            <br />
            <?php
            if (isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) > 0) {
                echo '<ul class="err">';
                foreach ($_SESSION['ERRMSG_ARR'] as $msg) {
                    echo '<li>', $msg, '</li>';
                }
                echo '</ul>';
                unset($_SESSION['ERRMSG_ARR']);
            }
            ?>

            <?php
            @$cat = $_GET['cat'];
            if (strlen($cat) > 0 and !is_numeric($cat)) {
                echo "Data Error";
                exit;
            }

            @$cat2 = $_GET['cat2'];
            if (strlen($cat2) > 0 and !is_numeric($cat2)) {
                echo "Data Error";
                exit;
            }
            @$cat3 = $_GET['cat3'];
            if (strlen($cat3) > 0 and !is_numeric($cat3)) {
                echo "Data Error";
                exit;
            }
            @$cat4 = $_GET['cat4'];
            if (strlen($cat4) > 0 and !is_numeric($cat4)) {
                echo "Data Error";
                exit;
            }
            @$cat5 = $_GET['cat5'];
            if (strlen($cat5) > 0 and !is_numeric($cat5)) {
                echo "Data Error";
                exit;
            }

            if (!$cat5 == 0) {
                $current = $cat5;
            } else if (!$cat4 == 0) {
                $current = $cat4;
            } else if (!$cat3 == 0) {
                $current = $cat3;
            } else if (!$cat2 == 0) {
                $current = $cat2;
            } else {
                $current = $cat;
            }

            $profile_qry = "SELECT * FROM members WHERE member_id='$current'";
            $profile = mysql_query($profile_qry);
            if ($profile) {
                $member = mysql_fetch_assoc($profile);
                print "<table width=300 border=0 cellpading=3 cellspacing=2>";
                print "<tr><th width=100>Information!</th></tr>";
                print "<br />";
                print "<tr><th>First name:</th><td>" . $member['firstname'] . "</td></tr>";
                print "<tr><th>Last name:</th><td>" . $member['lastname'] . "</td></tr>";
                print "<tr><th>Adress:</th><td>" . $member['adress'] . "</td></tr>";
                print "<tr><th>Zip:</th><td>" . $member['zip'] . "</td></tr>";
                print "<tr><th>City:</th><td>" . $member['city'] . "</td></tr>";
                print "<tr><th>Country:</th><td>" . $member['country'] . "</td></tr>";
                print "<tr><th>Phone:</th><td>" . $member['phone'] . "</td></tr>";
                print "<tr><th>E-Mail:</th><td>" . $member['email'] . "</td></tr>";
                print "<tr><th>Member_id:</th><td>" . $member['member_id'] . "</td></tr>";
                print "<tr><th>Sponsor_id:</th><td>" . $member['sponsor'] . "</td></tr>";
                print "<tr><th>Join date:</th><td>" . $member['joindate'] . "</td></tr>";

                print "</table>";
                print "<br />";
                print "<br />";
            }



            $start = $_SESSION['SESS_MEMBER_ID'];
            $quer1 = mysql_query("SELECT firstname, lastname, member_id FROM members where sponsor=$start");

            if (isset($cat) and strlen($cat) > 0) {
                $quer2 = mysql_query("SELECT firstname, lastname, member_id FROM members where sponsor=$cat");
            }

            if (isset($cat2) and strlen($cat2) > 0) {
                $quer3 = mysql_query("SELECT firstname, lastname, member_id FROM members where sponsor=$cat2");
            }
            if (isset($cat3) and strlen($cat3) > 0) {
                $quer4 = mysql_query("SELECT firstname, lastname, member_id FROM members where sponsor=$cat3");
            }
            if (isset($cat4) and strlen($cat4) > 0) {
                $quer5 = mysql_query("SELECT firstname, lastname, member_id FROM members where sponsor=$cat4");
            }

            echo "<form method=post name=f1 action='dd-check.php'>";
            echo "<select name='cat' size=10 onchange=\"reload(this.form)\">";

            while ($noticia = mysql_fetch_array($quer1)) {
                if ($noticia['member_id'] == @$cat) {
                    echo "<option selected value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname] <br \></option>" . "<BR>";
                } else {
                    echo "<option value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>";
                }
            }
            echo "</select>";

            echo "<select name='subcat' size=10 onchange=\"reload2(this.form)\">";
            while ($noticia = mysql_fetch_array($quer2)) {
                if ($noticia['member_id'] == @$cat2) {
                    echo "<option selected value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>" . "<BR>";
                } else {
                    echo "<option value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>";
                }
            }

            echo "<select name='subsubcat' size=10 onchange=\"reload3(this.form)\">";
            while ($noticia = mysql_fetch_array($quer3)) {
                if ($noticia['member_id'] == @$cat3) {
                    echo "<option selected value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>" . "<BR>";
                } else {
                    echo "<option value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>";
                }
            }

            echo "<select name='subsubsubcat' size=10 onchange=\"reload4(this.form)\">";
            while ($noticia = mysql_fetch_array($quer4)) {
                if ($noticia['member_id'] == @$cat4) {
                    echo "<option selected value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>" . "<BR>";
                } else {
                    echo "<option value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>";
                }
            }
            echo "<select name='subsubsubsubcat' size='10' width='100'  onchange=\"reload5(this.form)\">";
            while ($noticia = mysql_fetch_array($quer5)) {
                if ($noticia['member_id'] == @$cat5) {
                    echo "<option selected value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>" . "<BR>";
                } else {
                    echo "<option value='$noticia[member_id]'>$noticia[firstname] $noticia[lastname]</option>";
                }
            }

            echo "</select>";

            ?>
            <br />
            <br />
            <h1></h1><i>Password Protected Area  </i></div>
    </body>
</html>

This code works in firefox but not in IE and crome then I get these errors!

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\NSE-Klient\my-team.php on line 180
Notice: Undefined variable: quer4 in C:\wamp\www\NSE-Klient\my-team.php on line 189 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\NSE-Klient\my-team.php on line 189

This is how it looks in firefox:
http://img522.imageshack.us/img522/6553/eyy.png

And here is IE:
http://img714.imageshack.us/img714/5960/53970192.png

Member Avatar for diafol

regardless of your code, you need ajax. I would suggest something like jQuery and search for 'chaining select' or similar. It is quite straightforward though.

$('select').change(function() {
  doChanges($(this).attr("id"),$(this).val());
});

All select boxes are now linked to the js doChanges function which has 2 parameters: the id of the calling select widget and the changed value. Your doChanges should then pass on the data to a php script. The php script processes all the data and decides which select boxes to update. This info (box ids and option values) can be passed back to the doChanges script for updating. Personally I'd pass the info from php to js via JSON.

#EDIT
Just thought, you don't want your last select box to trigger a change as this will not have any effect on the hierarchy wiil it? Oh well, this only needs a small modification.

regardless of your code, you need ajax. I would suggest something like jQuery and search for 'chaining select' or similar. It is quite straightforward though.

$('select').change(function() {
  doChanges($(this).attr("id"),$(this).val());
});

All select boxes are now linked to the js doChanges function which has 2 parameters: the id of the calling select widget and the changed value. Your doChanges should then pass on the data to a php script. The php script processes all the data and decides which select boxes to update. This info (box ids and option values) can be passed back to the doChanges script for updating. Personally I'd pass the info from php to js via JSON.

#EDIT
Just thought, you don't want your last select box to trigger a change as this will not have any effect on the hierarchy wiil it? Oh well, this only needs a small modification.

Sorry i dont get why I need to change anything to ajax? The code I have works fine from my laptop and localhost... in firefox tho :/

func.php

<?php

function getFirst() {
    $start = $_SESSION['SESS_MEMBER_ID'];
    $result = mysql_query("SELECT DISTINCT member_id, firstname, lastname FROM members WHERE sponsor=$start")
            or die(mysql_error());

    while ($first = mysql_fetch_array($result)) {
        echo '<option value="' . $first['member_id'] . '">' . $first['firstname'] . $first['lastname'] . '</option>';
    }
}

if (@$_GET['func'] == "drop_1" && isset($_GET['func'])) {
    drop_1($_GET['drop_var']);
}

function drop_1($drop_var) {

    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT member_id, firstname, lastname FROM members WHERE sponsor='$drop_var'")
            or die(mysql_error());

    echo '<select name="drop_2" id="drop_2" size="10">
          ';

    while ($drop_2 = mysql_fetch_array($result)) {
        echo '<option value="' . $drop_2['member_id'] . '">' . $drop_2['firstname'] . $drop_2['lastname'] . '</option>';
    }

    echo '</select>';
    echo "<script type=\"text/javascript\">
$('#wait_2').hide();
    $('#drop_2').change(function(){
      $('#wait_2').show();
      $('#result_2').hide();
      $.get(\"func.php\", {
        func: \"drop_2\",
        drop_var: $('#drop_2').val()
      }, function(response){
        $('#result_2').fadeOut();
        setTimeout(\"finishAjax2('result_2', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>";
}

if (@$_GET['func'] == "drop_2" && isset($_GET['func'])) {
    drop_2($_GET['drop_var']);
}

function drop_2($drop_var) {
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT member_id, firstname, lastname FROM members WHERE sponsor='$drop_var'")
            or die(mysql_error());

    echo '<select name="drop_3" size="10" id="drop_3">
          ';

    while ($drop_3 = mysql_fetch_array($result)) {
        echo '<option value="' . $drop_3['member_id'] . '">' . $drop_3['firstname'] . $drop_3['lastname'] . '</option>';
    }

    echo '</select> ';
    echo "<script type=\"text/javascript\">
$('#wait_3').hide();
    $('#drop_3').change(function(){
      $('#wait_3').show();
      $('#result_3').hide();
      $.get(\"func.php\", {
        func: \"drop_3\",
        drop_var: $('#drop_3').val()
      }, function(response){
        $('#result_3').fadeOut();
        setTimeout(\"finishAjax3('result_3', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>";
}

if (@$_GET['func'] == "drop_3" && isset($_GET['func'])) {
    drop_3($_GET['drop_var']);
}

function drop_3($drop_var) {
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT member_id, firstname, lastname FROM members WHERE sponsor='$drop_var'")
            or die(mysql_error());

    echo '<select name="drop_4" size="10" id="drop_4">
          ';

    while ($drop_4 = mysql_fetch_array($result)) {
        echo '<option value="' . $drop_4['member_id'] . '">' . $drop_4['firstname'] . $drop_4['lastname'] . '</option>';
    }

    echo '</select> ';
    echo "<script type=\"text/javascript\">
$('#wait_4').hide();
    $('#drop_4').change(function(){
      $('#wait_4').show();
      $('#result_4').hide();
      $.get(\"func.php\", {
        func: \"drop_4\",
        drop_var: $('#drop_4').val()
      }, function(response){
        $('#result_4').fadeOut();
        setTimeout(\"finishAjax4('result_4', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>";
}

if (@$_GET['func'] == "drop_4" && isset($_GET['func'])) {
    drop_4($_GET['drop_var']);
}

function drop_4($drop_var) {
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT member_id, firstname, lastname FROM members WHERE sponsor='$drop_var'")
            or die(mysql_error());

    echo '<select name="drop_5" size="10" id="drop_5">
          ';

    while ($drop_5 = mysql_fetch_array($result)) {
        echo '<option value="' . $drop_5['member_id'] . '">' . $drop_5['firstname'] . $drop_5['lastname'] . '</option>';
    }

    echo '</select> ';
    echo '<input type="submit" name="submit" value="Submit" />';
}
?>
</html>

team.php

<?php
include('db.php');
include('func.php');
require_once 'auth.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Team-page</title>
        <link href="loginmodule.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

        <script type="text/javascript">
            $(document).ready(function() {
                $('#wait_1').hide();
                $('#drop_1').change(function(){
                    $('#wait_1').show();
                    $('#result_1').hide();
                    $.get("func.php", {
                        func: "drop_1",
                        drop_var: $('#drop_1').val()
                    }, function(response){
                        $('#result_1').fadeOut();
                        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
                    });
                    return false;
                });
            });

            function finishAjax(id, response) {
                $('#wait_1').hide();
                $('#'+id).html(unescape(response));
                $('#'+id).fadeIn();
            }
            function finishAjax2(id, response) {
                $('#wait_2').hide();
                $('#'+id).html(unescape(response));
                $('#'+id).fadeIn();
            }
            function finishAjax3(id, response) {
                $('#wait_3').hide();
                $('#'+id).html(unescape(response));
                $('#'+id).fadeIn();
            }
            function finishAjax4(id, response) {
                $('#wait_4').hide();
                $('#'+id).html(unescape(response));
                $('#'+id).fadeIn();
            }
        </script>
    </head>

    <body>
        <div id="main">
            <h1>My Team </h1>
            <a href="member-index.php">Back</a> | <a href="register-form.php">Register prospect</a> | <a href="my-team.php">My Team</a> | <a href="logout.php">Logout</a>
            <p>
                <?php
                if (isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) > 0) {
                    echo '<ul class="err">';
                    foreach ($_SESSION['ERRMSG_ARR'] as $msg) {
                        echo '<li>', $msg, '</li>';
                    }
                    echo '</ul>';
                    unset($_SESSION['ERRMSG_ARR']);
                }
                ?>

                <form action="" method="post">

                    <select name="drop_1" id="drop_1" size="10">

                        <?php getFirst(); ?>

                    </select>

                    <span id="wait_1" style="display: none;" >
                        <img alt="Please Wait" src="ajax-loader.gif"/>

                    </span>

                    <span id="result_1" style="display: none;"></span>
                    <span id="wait_2" style="display: none;">
                        <img alt="Please Wait" src="ajax-loader.gif"/>

                    </span>

                    <span id="result_2" style="display: none;"></span>
                    <span id="wait_3" style="display: none;">
                        <img alt="Please Wait" src="ajax-loader.gif"/>
                    </span>
                    <span id="result_3" style="display: none;"></span>
                    <span id="wait_4" style="display: none;">
                        <img alt="Please Wait" src="ajax-loader.gif"/>
                    </span>
                    <span id="result_4" style="display: none;"></span>

                </form>
            </p>


            <?php
                        if (isset($_POST['submit'])) {
                            $drop = $_POST['drop_1'];
                            $drop_2 = $_POST['drop_2'];
                            $drop_3 = $_POST['drop_3'];
                            $drop_4 = $_POST['drop_4'];
                            $drop_5 = $_POST['drop_5'];
                            echo "You selected a ";
                            echo $drop . " " . $drop_2 . " " . $drop_3 . " " . $drop_4 . " " . $drop_5;
                        }
            ?>
        </div>
    </body>
</html>

Now I have the problem.. first, the first box have correct unicode, åäö is ok.. but the others dont have correct unicode... :/

Second how the.. do I make this print a from like before with the info about the member currently been selected, So if i go from Tom to Markus the full info should be printed about Tom first and then Markus and so on.. ?

GREATFULL FOR HELP! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.