How to show mysqldata in textbox from selected option value?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ankungen
Forum Newbie
Posts: 4
Joined: Wed Dec 14, 2011 3:24 am

How to show mysqldata in textbox from selected option value?

Post by ankungen »

I have a table in my mysql database called element and I am working on a script to use for updating the description data for the existing elements and to add new elements with description. An example of the table.

id name description
1 a Description for a
2 b Description for b
3 c Description for c

My php-file has a select box, a textfield, a textarea box and a submit button. In the textfield I put new elements that I want to add and the select box lists the elements that already exists in the table. When I press the submit buttom a new element with description is added if I have written something in the textfield. If the textfield is empty and I select an element in the select box instead, the description for the element is updated. If both the select box and the textfield is empty, a message says: "No element chosen"

All this is working, but I also want the description for every element to show up in the textarea box, so I can edit it, not write a new one and erase the text already written. I have found out how to retrieve description data from the database to the textbox and this is the code I use:

Code: Select all

	function description(){
		set_utf8();
		$result=mysql_query("SELECT * from element WHERE name='a'") or die(mysql_error());	//onselect $name
		$row=mysql_fetch_array($result);
			echo $row['description'];
			}
If I only had the three elements in the example table above I could use this function with an if-statement, but since I am adding new elements all the time it won't work. I need a function that puts the element I select from the select box in a variable, so I can use that variable instead of 'a' in the above function. The problem is that I don't know how to do that. I usually figure out how to solve things like this or find a way around it, but this time I am totally lost.

I have a couple of files, but to make it simple here, I have put everything in the same page. Here is the code:

Code: Select all

			<?php 

/*##############################################################

    Connects to the database. The configuration file only
	holds the variables for the connection.  

##############################################################*/

	include('config.php');
	mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
	mysql_select_db($db_database) or die(mysql_error());

	
	
/*##############################################################

	To get rid of the strange characters when å, ä and ö is
	used.

##############################################################*/

	function set_utf8(){
		mysql_query("SET NAMES 'utf8'") or die(mysql_error());
		mysql_query("SET CHARACTER SET 'utf8'") or die(mysql_error()); 
	}
	
	
	
	
/*##############################################################

	Updating or adding and element with description.

##############################################################*/

	//Update
	if($_POST['submit'] && !$_POST['name'] && $_POST['element_retrieve']){
		$name = $_POST['element_retrieve'];
		$description = nl2br($_POST['description']);
		set_utf8();
		mysql_query("UPDATE element SET description='$description' WHERE name='$name'") or die(mysql_error());
		echo "Data inserted";
	}

	
	//Add
	else if($_POST['submit'] && $_POST['name']){
		$name = $_POST['name'];
		$description = $_POST['description'];
		set_utf8();
		mysql_query("INSERT INTO element (name, description) VALUES('$name', '$description')") or die(mysql_error());
		echo "Data inserted";
	}

	
	//Both the textfield and the selectbox is empty.
	else if($_POST['submit'] && !$_POST['name'] && !$_POST['element_retrieve']){
		echo 'No element chosen';
	}

	
	

/*##############################################################

	Trying to retrieve the description from the elements and
	echo it in the textarea box.

##############################################################*/

	function description(){
		set_utf8();
		$result=mysql_query("SELECT * from element WHERE name='a'") or die(mysql_error());	//onselect $name
		$row=mysql_fetch_array($result);
			echo $row['description'];
			}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lägga till data i databas</title>
</head>
<body>

<form method="post" action="test.php">

	<?php 
		//A selectbox with elements in the table element i database;
		echo 'Choose an element: <select name="element_retrieve"><option value="" selected="selected"></option>';
		$result=mysql_query("SELECT * from element") or die(mysql_error());	
		while($row=mysql_fetch_array($result)){
			echo '<option>';
			echo $row['name']."<br>";
		}
		echo "</option></select>";
	?>

or create a new element: <input type="text" name="name"><br><br>

Description:<br>
<textarea name="description"><?php description(); ?></textarea><br>
<input type="submit" value="Send" name="submit">
</form>

</body>
</html>	
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to show mysqldata in textbox from selected option va

Post by Celauran »

Assuming you want to update these fields without having to submit the form and refresh the page, you'll need an AJAX solution. Start by giving your form items IDs so they're easily selectable. Then you'll want to listen for the onchange event for the select list, and the onkeyup event for the text field. Grab their values, send them to another PHP page to pull the appropriate data from the DB, and have your callback function populate the textarea with the appropriate data.
Post Reply