Hi,

I am not too good on php and MYSQL and I am currently struggeling with populating a form on my website based on a selection chosen by the user from a drop down menu.

The drop down menu is dynamic with its values populated from a database. Below the menu is a form that has two functions which are:

1. It allows the user to write information to the database.
2. Allows the user to view and edit records from the database.

The data held in the drop down menu is the the title of the record, and when a new record is added to the database it automaticalty gets added to the menu. The problem comes with when the user choses an option from the menu as the form needs to display the corrasponding data but I dont know how to do this. For example, if JAN is chosen from menu the form should be repopulated with the data input by the user for that record.

So far the code I have is:

<?php
session_start();

// Database Connection
mysql_connect("www.numyspace.co.uk", "unn_s013370", "********")or die("connect");
mysql_select_db("unn_s013370")or die ("cannot select DB");

if (isset($_SESSION["Student_ID"])) {
    $username = $_SESSION['Student_ID'];
     echo "Log in as ".$username;
} else {
    // not logged in so redirect
    header("location: LOGIN2.html");
    exit;
}

if (isset($_SESSION["Student_ID"])) {
    $First_Name = $_SESSION['Student_ID'];
}
if (isset($_SESSION["Password"])) {
    $Gender = $_SESSION['Password'];
}

//populate dropdown mene with users recorded logs

//select data from database based on Student ID
$sql="SELECT LogMonth, LogYear FROM Student_MLog WHERE Student_ID='$username'"; 
//assign query results to sql variable
$result=mysql_query($sql); 

//create drop down menue on screen
echo "<select name='logdropdown' >";
while ($row=mysql_fetch_array($result)) { 

    $LogMonth=$row["LogMonth"]; 
	$LogYear=$row["LogYear"]; 
//display db data in menu
echo "<option>$LogMonth<option/>";
} 
echo "<select/>";


?>

... and the form code is...

<!--activity field + log title-->
<div class = "activity_field">
<form action="activity.php" method="post">
<label>Log Month
<input type="text" name="logmonth" size="25">
</label>
<label>Log Year
<input type="text" name="logyear" size="25">
</label>
<br/>
<label>Activities:
 <textarea cols="50" rows="13" name="activity"></textarea>
</label> <br />
<input type="image" src="button_SAVE.png" name="image" width="170" height="100">
</form>
</div>

Any help would be greatly appriacted as I have benn looking into this for nearly a week but with no luck as of yet.

Recommended Answers

All 5 Replies

Hi,
I m not sure that I understand what you said but in order to display the corrasponding data, all you need is to add value to the input tag

<label>Log Month
    <input type="text" name="logmonth" size="25" value="<?php echo $LogMonth ?>" />
</label>
		
<label>Log Year
    <input type="text" name="logyear" size="25" value="<?php echo $LogYear ?>" />
</label>

to recieve the edited data you need to add the fellowing code

if( isset($_POST['image'])){
	$newLogMonth = $_POST['logmonth'];
	$newLogYear = $_POST['logyear'];
	// add update query...
}

PS: I assume both code are in the same page

If I'm understanding you correctly, you want the form to populate with data as soon as the user selects an item from the drop-down menu. There's two ways of doing this that I can think of, but either way requires JavaScript.

Method 1:
This method uses JS to go to another page when the selected item in the drop-down is changed. You can add variables into the URL and use PHP's $_GET superglobal to pull them from the URL, telling the page what information to load into the form.

<form action="../">
<select onchange="location.href=(this.options[this.selectedIndex].value)">
    <option value="">Choose an item...</option>
    <option value="same/page/url?entry_id=<?php echo $identifier; ?>&action=edit" >An Item</option>
    <option value="same/page/url?entry_id=<?php echo $identifier; ?>&action=edit" >Another Item</option>
</select>
</form>

Your second option would use AJAX or some other JS method to pull information straight from the database without having to reload the page. But I'm not versed in such solutions, so I can't help you with that, I just know it's possible.

- EF

EvolutionFallen

your r the real programmer man
u rocks

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.