Jump to content

auto fill the form from selection box


lipa

Recommended Posts

I am creating a simple web application that allows the user to update information in a database. I have a selector that allows the user to select what record they want to update and then a form field to insert the data to be updated. I want the form to to be passed the data from the selected entry "when selected" to auto-complete the default values of the text fields so the user can see the information right in the text fields.

 

This is the form

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <title>Tom's Culture Database</title>
  <meta name="GENERATOR" content="Quanta Plus">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<style>

body {
  }


h3 {
    color: #585858;
  }


h4 {
    color : #c00000;
  }


th {
    background : #000000;
    border : #ffffff solid 1px;
    color : #ff0000;
    text-align : left;
  }


.form_wrapper {
    background : #000000;
    padding : 10px;
    border : solid 2px;
  }


#form_inner {
    padding : 0 0 0 5px;
    display : inline-block;
  }


td {
    border : #000000 solid 1px;
  }

</style>
<body>

<?php
//CONNECT INCLUDE
require("connect.php");


//SELECT DATABASE
$sql_user_info = mysql_query("SELECT * FROM collections ORDER BY id ASC");

//BEGIN FORM
echo "<h3>Select an entry to update or delete!</h3>
<form action='mysql_update_action.php' method='POST'><select name='name_select'>";


	while ($row = mysql_fetch_assoc($sql_user_info))

	{
		$id = 		$row['id'];
		$genus =	$row['genus'];
		$species = 	$row['species'];
		$strain_name=  	$row['strain_name'];




		echo "
			<option value = '$id'>$id : $genus $species $strain_name</option>
		";

	}

echo "</select> <br><br><br>
<DIV class='form_wrapper'>
<DIV  id='form_inner' align=\"left\"><h4>Genus Name:</h4><INPUT type=\"text\" name=\"to_change_genus_name\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Species Name:</h4><INPUT type=\"text\" name=\"to_change_species_name\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Strain Name:</h4><INPUT type=\"text\"name=\"to_change_strain_name\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>P-Value:</h4><INPUT type=\"text\" name=\"to_change_p_value\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Origin:</h4><INPUT type=\"text\" name=\"to_change_origin\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Get From:</h4><INPUT type=\"text\" name=\"to_change_get_from\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Qty:</h4><INPUT type=\"text\" name=\"to_change_qty\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>Created:</h4><INPUT type=\"text\" name=\"to_change_date_created\" value=\"M/D/Y\"></DIV><br>
<DIV  id='form_inner' align=\"left\"><h4>Working Collection:</h4><INPUT type=\"checkbox\" name=\"to_change_work_collection\"></DIV>
<DIV  id='form_inner' align=\"left\"><h4>DNA Check:</h4><INPUT type=\"checkbox\" name=\"to_change_dna_check\">
</DIV>



<br><br>	


<INPUT type='submit' name='change' value='Update'>
<INPUT type='submit' name='name_delete' value='Delete'>
</form></DIV>";
//END FORM



?>
<?php

//SELECT DATABASE
$sql_user_info = mysql_query("SELECT * FROM collections ORDER BY id");


while ($row = mysql_fetch_assoc($sql_user_info))

{
$id = 			$row['id'];
$genus = 		$row['genus'];
$species = 		$row['species'];
$strain_name =		$row['strain_name'];	
$p_value =		$row['p_value'];
$origin =		$row['origin'];
$get_from =		$row['get_from'];
$qty =			$row['qty'];
$work_collection =	$row['work_collection'];
$dna_check =		$row['dna_check'];
$date_created =		$row['date_created'];
$last_modified =	$row['last_modified'];


	if 	($work_collection == "0"){
	 	$work_collection = "no";
	}
	else {
		$work_collection = "yes";
	}

// Define $color=1

echo "<br>";
echo "<table width='400' border='1' align='left' cellpadding='2' cellspacing='0'>";
echo "<tr><th>ID</th><th>Genus</th><th>Species</th><th>Strain Name</th><th>P-Value</th></tr>";
$color= 1;

// If $color==1 table row color = #FFC600
if($color==1){
echo "<tr bgcolor='#808080'><td>".$id."</td><td>".$genus."</td><td>".$species."</td></tr>";
// Set $color==2, for switching to other colors
$color=2;
}

// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#ffffff'><td>".$id."</td><td>".$genus."</td><td>".$species."</td></tr>";
// Set $color back to 1
$color='1';
}


echo "</table>";

}


?>
</body>
</html>

 

This is the update actions

<?php

//CONNECT INCLUDE
require("connect.php");


//GRAB DATA
$name_select 		=  $_POST['name_select'];
$name_delete 		=  $_POST['name_delete'];

$to_change_genus_name  		=  $_POST['to_change_genus_name'];
$to_change_species_name  	=  $_POST['to_change_species_name'];
$to_change_strain_name  	=  $_POST['to_change_strain_name'];
$to_change_p_value  		=  $_POST['to_change_p_value'];
$to_change_origin  		=  $_POST['to_change_origin'];
$to_change_get_from  		=  $_POST['to_change_get_from'];
$to_change_qty  		=  $_POST['to_change_qty'];
$to_change_work_collection  	=  $_POST['to_change_work_collection'];
$to_change_dna_check  		=  $_POST['to_change_dna_check'];
$to_change_date_created  	=  $_POST['to_change_date_created'];


$date = strtotime("now");
$current_date = date("m/d/y", $date);

if 	($name_select&&$to_change_genus_name&&$_POST['change'])

	{

	$change = mysql_query("UPDATE collections SET genus='$to_change_genus_name', species='$to_change_species_name' WHERE id='$name_select'");
	echo "<br><h2>Data updated</h2><br>";
	header( 'Location: http://localhost/sam/Mycelium.eu/mysql_update.php' ) ;
	}


elseif 	($_POST['name_delete'])

	{
	$delete = mysql_query("DELETE FROM collections WHERE id='$name_select'");
	echo "<br><h2>Data deleted</h2><br>";
	header( 'Location: http://localhost/sam/Mycelium.eu/mysql_update.php' ) ;
	}


else
echo "Please enter some Information";
header( 'Location: http://localhost/sam/Mycelium.eu/mysql_update.php' ) ;
?>




Link to comment
Share on other sites

Your current code for the form page is problematic. You are wanting to allow the user to update ALL the records in one page. That is possible, but it is problematic. The reason is that you can't "know" which values have been updated and which ones haven't, so you would have to update ALL the records whenever that page is submitted. Plus, right now the code you have doesn't format the field names as arrays with an ID reference to each record. So, you would only be updating the last record (when there are input fields with the same name only the last one is sent in the POST data - unless it is configured as an array).

 

A better approach is to provide a page where all the records are listed with a link for each record to go to an edit page. The link will pass the id of the record. Then you want to query the database for the values associated with that record then populate the form fields as appropriate.

 

So, first change your current form page to simply display the data and for each record add an edit link that looks something like this

echo "<a href=\"edit.php?id={$_row['id']}\">Edit</a>";

 

Then create an edit.php page where you query the data for the passed ID and create the form using the db results to populate the values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.