0

Hi. 'm new to php..please help.. 'm working with dropdown and submit button.. the dropdown contains certain values..if the user selectes one option in dropdown and clicks submit button, values corresponding to the dropdown should be displayed in table format from the database. the file name is campus.php
'm attaching the code below..

<!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" />
</head>
<body>

<form action="campus.php" method="POST">
<table width="878" height="48" border="0" align="center">
  <tr>
    <th width="107" scope="col">Campus</th>
    <th width="755" scope="col">
    <select name="campus" id="campus">
      <option selected="selected" >Select</option>
      <option value="1">Amritapuri</option>
      <option value="2">Kochi </option>
      <option value="3">Ettimadai</option>
      <option value="4">Banglore</option>
      <option value="5">Mysore</option>
    </select>
      <label>
      <input type="submit" name="submit" id="submit" value="Submit" />
      </label></th>
  </tr>
</table>
</form>
<?php
     if(isset($_POST['campus']))

{
        $table =mysql_escape_string($_POST['campus']);
        echo $table;
        $con = mysql_connect("localhost","admissions","admission") or die('Connection error : ' . mysql_error());;
        mysql_select_db("amritavarsham13", $con);

        $result = mysql_query("select SNO,NAME,GENDER,ENROLLNO,YROFSTUDY,COURSE,CAMPUS,FACULTYINCHARGE,CONTACTNO from searchportal where CAMPUS='$table';") or die ("SQL Error" . mysql_error());

        print "<center>Campuswise listing: $table</center>";
        print "<table align=center border=1>\n";
        print "<tr><td>SNO</td><td>NAME</td><td>GENDER</td><td>ENROLLNO</td><td>YROFSTUDY</td><td>FACULTYINCHARGE</td><td>CONTACTNO</td>";

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
            {
                print "\t<tr>\n";
                foreach ($row as $col_value)
                {
                            echo "\t\t<td>" . htmlentities($col_value) . "</td>\n";
                }
                echo "\t</tr>\n";
            }
        echo "</table><center>\n";

    /*else
    {   
        die("Could not enter data: ' . mysql_error()"); 
    }   
    echo "Please enter the required fields\n"*/

        mysql_close();
        mysql_close($con);

}   
?>
</body>
</html>
4
Contributors
7
Replies
35
Views
4 Years
Discussion Span
Last Post by diafol
0

the dropdown contains certain values..if the user selectes one option in dropdown and clicks submit button, values corresponding to the dropdown should be displayed in table format.the values are retreived from the database.

0

OK, this looks like a straightforward pick and display.

Don't hard code your dropdown.

'SELECT campus_id, campus_name FROM campus ORDER BY campus_name'

Use the while loop to build the dropdown...

$dropdown = '';
while($data = mysql_fetch_assoc($result))
{
    $dropdown .= "\n\t<option value='{$data['campus_id']}'>{$data['campus_name']}</option>";
}

//then in your html...

<select name="campus">
<?php echo $dropdown;?>
</select>

Your selection of students is ther same...

$campus_id = (int) $_POST['campus_id'];

Also consider moving away from mysql to mysqli or PDO. Possibly also splitting the name, so that they can be sorted on firstname or secondname - depending on how you sort them where you are - if it's by firstname, no prob. However, it can be handy to split them anyway as you can then easily build up custom name formats.

Edited by diafol

0

Sir, its not the case.. the aim is to Retrieve data from database in html table format when dropdown value is selected.

0

Sir, its not the case.. the aim is to Retrieve data from database in html table format when dropdown value is selected.

I was just pointing out that your dropdown should be derived from the DB and that you shouldn't hardcode these values as it leads to maintainability issues.

I know what you're doing, just unsure as to the help you require. You don't state in any of your posts, which issues you're having difficulty with.

As you're starting out in php, why not just use a subset of your fields - say three of them to begin with - NAME, GENDER, ENROLLNO.

Build your table around those to see if it works. Then adding fields to the query and to the html table will be easy.

If you need interactivity, then as Assembly Guy suggests, use Ajax. You may find the jQuery variety easier to handle than plain javascript.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.