Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2003
    Posts
    225

    Unanswered: Drop down box to populate fields for modification

    Hi

    This is my first post on here so hello, and before we go any further I have next to no real knowledge of PHP.

    I am in the process of building a website located at : www.ilt.bridgwater.ac.uk/~staua


    The site is primarily written in html, but I have a couple of mysql table linked to it via php.

    Everything is working fine except for one thing….on the contacts link on the left hand side I want to be able to do the following:

    Click on the link ---->open up my table (mysql) called “contactslists.php”

    This opens in the frame called main. This currently works fine…

    Problem starts here.

    I want to be able to amend a record, ideally I would like to be able to click on my amend records button and then give me a drop down box to select the record to amend.

    I then want the select record to populate the fields on the form that is currently visible after the amend records button is selected.

    I would then need a button to confirm and update my table.

    Below is the code for my phpfiles

    Upon clicking the contacts on the left frame the following file is activated:

    Code:
    <html>
    <head>
    <title>RTFC Contacts</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
    <body>
    <?php
         import_request_variables('p');
         if (isset($name) && isset($email) && isset($position) && isset($tel))
        {
            // check user input here
            $dberror = "";
            $ret = add_to_database($name,$position,$email,$tel,$dberror);
            if (!$ret)
                print "Error: $dberror<br />";
           else
                print "<h2>Thank you very much, $name has been added </h2>";
        }
        else
            write_form();
    function add_to_database($name, $position, $email, $tel, &$dberror)
    {
        $user = "username";
        $pass = "password";
        $db = "database";
        $link = mysql_connect("localhost",$user,$pass);
        if (!$link)
        {
            $dberror = "Couldn't connect to MySQL server";
            return false;
        }
        if (!mysql_select_db($db, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        $query="INSERT INTO Contacts (Name, Position, Email, Tel)
       	VALUES('$name', '$position', '$email', '$tel' )";
        if (!mysql_query($query, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        return true;
    //  mysql_close($link);
    }
    function write_form()
    {
        global $PHP_SELF;
        print "<form action=\"$PHP_SELF\"method=\"POST\">\n";
        print "<p><h2>Please Ammend Details As Required</h2></p>";
        print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
        print " <p> Officials Name</p>\n";
        print "<input type=\"text\" name=\"name\">"; 
    	print " <p> Officials Position</p>\n";
        print "<input type=\"text\" name=\"position\">"; 
    	print " <p> Your e-mail address</p>\n";
        print "<input type=\"text\" name=\"email\">";
    	print " <p> Officials Tel</p>\n";
        print "<input type=\"text\" name=\"tel\">"; 
        print "<p><input type=\"submit\" value=\"Register\"></p>\n</form>\n";
        print "";
    ?>
    </td><td>
    <?php
    }
    ?>
    </body>
    </html>
    When the Amend button is clicked on the above form the following phpfile is activated:

    Code:
    <html>
    <head>
    <title>Contacts List</title>
    <link rel="stylesheet" href="stylesheet.css" />
    </head>
    <body>
    <?php
        $user = "username";
        $pass = "password";
        $db = "database";
        $link = mysql_connect("localhost",$user,$pass);
        if (!$link)
            die("Couldn't connect to MySQL server");
        mysql_select_db($db, $link)
            or die("Couldn't open $db: ".mysql_error());
        $result = mysql_query("SELECT * FROM Contacts");
        $num_rows = mysql_num_rows($result);
        print "<h2>RTFC Officials Contact Details</h2>";
        print "<table border=\"1\">\n";
        while ($a_row = mysql_fetch_row($result))
    	    {
            print "<tr>\n";
            foreach ($a_row as $field)
                print "\t<td>$field</td>\n";
            print "</tr>\n";
    		    }
        print "</table>\n";
    	print "<form method='post' action='Contacts.php'>\n";
    	print "<p><input type=\"submit\" value=\"Update Records\"></p>\n";
    	print "</form>\n";
        mysql_close($link);
    ?>
    </body>
    </html>
    I hope this makes some sense, please help

    Andy

  2. #2
    Join Date
    Mar 2003
    Posts
    225
    right many thanks, but i have had a slight change of angle here, which i know makes it difficult for you to hit a moving target, but still....

    i have now opted for radio buttons

    i have the following code which sort of works to a point:

    it is founsd at: http://www.ilt.bridgwater.ac.uk/~staua/contactslist.php


    contacts list:


    Code:
    html>
    <head>
    <title>Contacts List</title>
    <link rel="stylesheet" href="stylesheet.css" />
    </head>
    <body>
    <?php    
        $user = "username";
        $pass = "password";  
        $db = "dbname"; 
       $link = mysql_connect("localhost",$user,$pass);
        if (!$link)        die("Couldn't connect to MySQL server");
        mysql_select_db($db, $link)        or die("Couldn't open $db: ".mysql_error());
        $result = mysql_query("SELECT * FROM Contacts");
        $num_rows = mysql_num_rows($result);
        print "<h2>RTFC Officials Contact Details</h2>";
        print "<form method='post' action='Contacts.php'>\n";
        print "<table border=\"1\">\n";
        while ($a_row = mysql_fetch_row($result))        
        {
            print "<tr>\n";
        if ($a_row[1] == "")
              print "\t<td>&nbsp;</td>\n";
          else                    
              print "\t<td>$a_row[1]</td>\n";
        if ($a_row[2] == "")
              print "\t<td>&nbsp;</td>\n";
          else                    
              print "\t<td>$a_row[2]</td>\n";
        if ($a_row[3] == "")
              print "\t<td>&nbsp;</td>\n";
          else                    
              print "\t<td>$a_row[3]</td>\n";
        if ($a_row[4] == "")
              print "\t<td>&nbsp;</td>\n";
          else                    
              print "\t<td>$a_row[4]</td>\n";
        print "\t<td>$field</td><td><input name='opt' type='radio' value=$a_row[0]></td>\n";
            print "</tr>\n";
                }    
        print "</table>\n";
        print "<p><input type=\"submit\" value=\"Add A New Record\"></p>\n";
        print "</form>\n";    mysql_close($link);?>
    </body>
    </html>
    how this works is that if no radio button is clicked it should act as a new entry is to be added, however if a radio button is used then it will take on the Amend process to alter current data.

    when the radio button is activated and the button clicked the following code is used:


    Contacts.php


    Code:
    <html>
    <head>
    <title>RTFC Newsletter</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
    <body>
    <?php
        import_request_variables('p');
        if (isset($opt))
        {
            if (isset($name2) && isset($email2) && isset($position2) && isset($tel2))
            {
                // check user input here
                $dberror = "";
                $ret = update_database($name2,$position2,$email2,$tel2,$dberror);
                if (!$ret)
                    print "Error: $dberror<br />";
                else
                    print "<h2>Thank you very much, $name has been added </h2>";
            }
            else
            {
                $user = "username";
                $pass = "password";  
                $db = "dbname"; 
                $link = mysql_connect("localhost",$user,$pass);
                if (!$link)        die("Couldn't connect to MySQL server");
                mysql_select_db($db, $link)        or die("Couldn't open $db: ".mysql_error());
                $result = mysql_query("SELECT * FROM Contacts WHERE ID='".$pos."'");
                $num_rows = mysql_num_rows($result);
                $a_row = mysql_fetch_row($result);
        print "numrows = '$num_rows'";
                write_form2($a_row[1], $a_row[2], $a_row[3], $a_row[4]);
            }
        }
        else
        {
            if (isset($name) && isset($email) && isset($position) && isset($tel))
            {
                // check user input here
                $dberror = "";
                $ret = add_to_database($name,$position,$email,$tel,$dberror);
                if (!$ret)
                    print "Error: $dberror<br />";
                else
                    print "<h2>Thank you very much, $name has been added </h2>";
            }
            else
                write_form();
        }
    
    
    function add_to_database($name, $position, $email, $tel, &$dberror)
    {
        $user = "username";
        $pass = "password";
        $db = "dbname";
        $link = mysql_connect("localhost",$user,$pass);
        if (!$link)
        {
            $dberror = "Couldn't connect to MySQL server";
            return false;
        }
        if (!mysql_select_db($db, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        $query="INSERT INTO Contacts (Name, Position, Email, Tel)
           VALUES('$name', '$position', '$email', '$tel' )";
        if (!mysql_query($query, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        return true;
    //  mysql_close($link);
    }
    
    function update_database($name2, $position2, $email2, $tel2, &$dberror)
    {
        $user = "username";
        $pass = "password";
        $db = "dbname";
        $link = mysql_connect("localhost",$user,$pass);
        if (!$link)
        {
            $dberror = "Couldn't connect to MySQL server";
            return false;
        }
        if (!mysql_select_db($db, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        $query="UPDATE Contacts SET Name='$name2', Position='$position2', Email='$email2', Tel='$tel2'
           WHERE ID='$pos'";
        if (!mysql_query($query, $link))
        {
            $dberror = mysql_error();
            return false;
        }
        return true;
    //  mysql_close($link);
    }
    
    function write_form()
    {
        global $PHP_SELF;
        print "<form action=\"$PHP_SELF\"method=\"POST\">\n";
        print "<p><h2>Please Add Details As Required</h2></p>";
        print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
        print " <p> Officials Name</p>\n";
        print "<input type=\"text\" name=\"name\">"; 
        print " <p> Officials Position</p>\n";
        print "<input type=\"text\" name=\"position\">"; 
        print " <p> Your e-mail address</p>\n";
        print "<input type=\"text\" name=\"email\">";
        print " <p> Officials Tel</p>\n";
        print "<input type=\"text\" name=\"tel\">"; 
        print "<p><input type=\"submit\" value=\"Register\"></p>\n</form>\n";
        print "";
    }
    
    function write_form2($name2, $position2, $email2, $tel2)
    {
        global $PHP_SELF;
        print "name2 = '$name2'";
        print "<form action=\"$PHP_SELF\"method=\"POST\">\n";
        print "<p><h2>Please Ammend Details As Required</h2></p>";
        print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
        print " <p> Officials Name</p>\n";
        print "<input type=\"text\" name=\"name2\" value=$name2>"; 
        print " <p> Officials Position</p>\n";
        print "<input type=\"text\" name=\"position2\" value=$position2>"; 
        print " <p> Your e-mail address</p>\n";
        print "<input type=\"text\" name=\"email2\" value=$email2>";
        print " <p> Officials Tel</p>\n";
        print "<input type=\"text\" name=\"tel2\" value=$tel2>";
        print "<input type=\"hidden\" name=\"pos\" value=$pos>";
        print "<p><input type=\"submit\" value=\"Register\"></p>\n</form>\n";
        print "";
    }
    ?>
    <? print "opt = '$opt'"; ?>
    </body>
    </html>
    the trouble i am having is that i want the data that is attached to the relevant radio button to be copied across to the fields for ammending, but they are not being transferred.

    i am really desperate for some help here please, i am even prepared to give you access to my site if that help.

Posting Permissions

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