Hi,

I'm sorry if this is answered already but I'm such a newbie at programming that I apperantly can't formulate my problem to find an answer! :-/

Here goes:

I have a table filled with data. I want a user to be able to select via a dropdown list what row he/she wants to look at.

So when an option is selected in the dropdown i want one div showing the first ten columns wich are descriptives of that row. then in the next div below i want the next ten columns listed.

I woud then want if possible have four options (buttons) like a menu wich wich when pressed updates the second div to show the next ten columns (columns 21-30) and so on. makes sense? :)

Here I got the dropdown showing the right rows (which is dependant on who the user is and what he selected earlier)

I got two problems with this,
1. Is that when i select an option it reloads the page and shows the initial value instead of the choosen value.
2. I don't know how to proceed and use the input to show the relevant column values!

and a 3. as well :) In my simple mind I would use the dropdown input to use for the buttons to show the next ten values and so on. but then I have to have the input from the dropdown...

the code for my dropdown is as follows:

<form action="" method="post"> 
<label for="your_select"><select id="your_select" name="id" onChange="this.form.submit()"> 
<?php 
$result12 = mysql_query("SELECT foretag FROM users
WHERE username ='$session->username'");
$rows = mysql_fetch_array( $result12 );
$test = $rows['foretag'];

    $sql = "SELECT * FROM matningar WHERE foretag = '$test' AND matningstyp ='rst'"; 
$result = mysql_query($sql) or die (mysql_error());  
while ($row = mysql_fetch_array($result)) 
{ 
        $namn=$row["namn"]; 
        $foretag=$row["foretag"];  
        $options.="<OPTION VALUE=\"$foretag\">".$namn.'</option>';
} 
?> 
        <? echo $options ?>
    </select>
</form>

Anybody out there who could pity this fool :) Any help or suggestions are greatly welcomed!

Cheers
AddiGraddi

Recommended Answers

All 3 Replies

Hello
Really i don't know if i got that or not
what i think i got is that you want to display data when anoption is selectd with out page reload again
so i think you have to use jquery lib
the example for what you want will be some think like this

<!DOCTYPE HTML>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="Moustafa Adel ( The Traveller )" />
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <title>Untitled 4</title>
    <script type="text/javascript">
    $(document).ready(function(){
        $("select#your_select").change(function()
        {
            if($(this).val() == '')
            {
                $.ajax({
                  url: 'test.php',
                  type: "POST",
                  data: { name: "name", option: $(this).val() , ajax:1 }
                  beforeSend: function () {
                    $('#fetchdata').html('').slideUp();
                  }
                  success: function(data) {
                    $('#fetchdata').html(data).slideDown();
                  }
                });
            }

        }) ;

    })

    </script>
</head>

<body>



    <label for="your_select"><select id="your_select" name="your_selectid">
    <?php
    $result12 = mysql_query("SELECT foretag FROM users
    WHERE username ='$session->username'");
    $rows = mysql_fetch_array( $result12 );
    $test = $rows['foretag'];
    $sql = "SELECT * FROM matningar WHERE foretag = '$test' AND matningstyp ='rst'";
    $result = mysql_query($sql) or die (mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        $namn=$row["namn"];
        $foretag=$row["foretag"];
        $options.="<OPTION VALUE=\"$foretag\">".$namn.'</option>';
    }
    ?>
    <? echo $options ?>
    </select>
    <div id="fetchdata"></div>
</body>
</html>

So i assumed that you now know that you have to make anew page called test.php and recive data in it like

    $name = $_POST['name'] ;
    $ajax = $_POST['ajax'] ;
    $option = $_POST['option'] ;

    $query = 'SELECT BLA BLA BLA ';
    // fetch data
    $data = mysql_fetch_array($query);
    // add table or css styles for data as u like
    if($ajax)
    {
        echo $data ;
        exit;
    }
Member Avatar for diafol

When you say 'columns', are you talking about db table fields?

An alternative to using ajax would be to load the whole data on page load and then place it into a json object (really easy to do) - so you keep it all in javascript - so then your button can get the next set of data without refresh (vanilla php) or 'silent roundtrip' to the server (ajax).

You should be a ble to fall back to php if js turned off.

Thank you guys for taking time helping me out! I've been trying to get the first suggestion to work but it isn't working out yet.

Yes with columns I indeed mean table fields :) And you are spot on with what I'm trying to accomplish and your solution with json object sounds really good! tomorrow morning I'll look into this for sure! If you know some good examples or sites to get a crashcourse I would appreciate it!

Thanks again both of you!

Adam

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.