User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 25 Jun 2003, 12:39   #1
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
PHP/Javascript: Fill boxes with dynamic data

Hi, I get three things from the DB: ID, Name, Summary.

The Name is displayed in the drop-down, the ID is the value. I'm banging my head over how to get a textarea (called details) to display the Summary.

edit: I want the "details" textarea to update, every time I select a project from the drop-down (i.e. no need to reload the page)

Here's a code snippet:

Code:
<td>
	$query = "SELECT name, id, summary FROM projects";
	$result	= mysql_query ($query);
	?>
	<select name="user" onchange="this.form.details.value=this.form.user.value">
	<option value="0">Choose a project</option>
	<?
	while (list($name, $id, $summary) = mysql_fetch_row($result))
			{
				echo "<option value=\"".$id."\">".$name."</option>";
			}
	?>
	</select>
</td>
</tr>
<tr> 
	<td>&nbsp;</td>
	<td> <input name="SubmitEd" type="image" style="border: 1px solid #00584A;" value="Submit" src="img/form-edit-project.gif" width="70" height="20"> </td>
</tr>
<tr> 
	<td colspan=2><br /><textarea name="details" cols="40" rows="5" wrap="VIRTUAL" width=30></textarea></td>
</tr>
Cheers!

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"

Last edited by Mong; 25 Jun 2003 at 14:21.
Mong is offline   Reply With Quote
Unread 25 Jun 2003, 13:23   #2
Mit
Let battle commence
 
Mit's Avatar
 
Join Date: Feb 2002
Location: England
Posts: 732
Mit is a jewel in the roughMit is a jewel in the roughMit is a jewel in the rough
Re: PHP/Javascript: Fill boxes with dynamic data

Quote:

...
Code:
<textarea name="details" cols="40" rows="5" wrap="VIRTUAL" width=30></textarea>
...
Code:
<textarea name="details" cols="40" rows="5" wrap="VIRTUAL" width=30>Stuff to display here...</textarea>
If i understand what ur after, that is the solution. if not, i'll just go hide somewhere.
__________________
Mit
http://tim.igoe.me.uk - Development Blog
Whats on TV now - UK TV Guide

<Mendosa> mit is a cute cudlly toy that will be in the shops by christmas
<mig-work> ur now my eternal fav pa god
<Squiz> i name thee, Sir Mit
<Zeus> u my friend are a true gamer I knew u were
Mit is offline   Reply With Quote
Unread 25 Jun 2003, 14:20   #3
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Re: Re: PHP/Javascript: Fill boxes with dynamic data

Quote:
Originally posted by Mit
If i understand what ur after, that is the solution. if not, i'll just go hide somewhere.
Sorry Mit (but thanks), I didn't explain myself properly (but I editted my post now).

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 25 Jun 2003, 14:32   #4
Mit
Let battle commence
 
Mit's Avatar
 
Join Date: Feb 2002
Location: England
Posts: 732
Mit is a jewel in the roughMit is a jewel in the roughMit is a jewel in the rough
Re: Re: Re: PHP/Javascript: Fill boxes with dynamic data

Quote:
Originally posted by Mong
Sorry Mit (but thanks), I didn't explain myself properly (but I editted my post now).

M.
ah, makes more sense now... erm, a javascript on 'select' thingy would do it, i'm no javascript expert but...

Code:
<script language="JavaScript" type="text/javascript">
<!--
function Change(Text)
{
   details.Value = Text
}
-->
</script>
and in the drop down list put onchange="Chagne('Text Description');"

if that isn't perfect, its close, its been a while since i used JavaScript.
__________________
Mit
http://tim.igoe.me.uk - Development Blog
Whats on TV now - UK TV Guide

<Mendosa> mit is a cute cudlly toy that will be in the shops by christmas
<mig-work> ur now my eternal fav pa god
<Squiz> i name thee, Sir Mit
<Zeus> u my friend are a true gamer I knew u were
Mit is offline   Reply With Quote
Unread 25 Jun 2003, 14:47   #5
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Cheers Mit, but I still don't know how to get the "Summary" info from the query into there...

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 25 Jun 2003, 15:38   #6
Mit
Let battle commence
 
Mit's Avatar
 
Join Date: Feb 2002
Location: England
Posts: 732
Mit is a jewel in the roughMit is a jewel in the roughMit is a jewel in the rough
Quote:
Originally posted by Mong
Cheers Mit, but I still don't know how to get the "Summary" info from the query into there...

M.
i suppose, dump an array of summary's, the when u call the function, use the ID in the array?
__________________
Mit
http://tim.igoe.me.uk - Development Blog
Whats on TV now - UK TV Guide

<Mendosa> mit is a cute cudlly toy that will be in the shops by christmas
<mig-work> ur now my eternal fav pa god
<Squiz> i name thee, Sir Mit
<Zeus> u my friend are a true gamer I knew u were
Mit is offline   Reply With Quote
Unread 25 Jun 2003, 15:51   #7
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by Mit
i suppose, dump an array of summary's, the when u call the function, use the ID in the array?
So in the PHP while loop, I should output some Javascript?

Eeeep!

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 25 Jun 2003, 17:05   #8
Mit
Let battle commence
 
Mit's Avatar
 
Join Date: Feb 2002
Location: England
Posts: 732
Mit is a jewel in the roughMit is a jewel in the roughMit is a jewel in the rough
Quote:
Originally posted by Mong
So in the PHP while loop, I should output some Javascript?

Eeeep!

M.
u'd have to do 2 loops, one to put out the JS, one to do the page. Cos u can't have the function spread throughout the page...
__________________
Mit
http://tim.igoe.me.uk - Development Blog
Whats on TV now - UK TV Guide

<Mendosa> mit is a cute cudlly toy that will be in the shops by christmas
<mig-work> ur now my eternal fav pa god
<Squiz> i name thee, Sir Mit
<Zeus> u my friend are a true gamer I knew u were
Mit is offline   Reply With Quote
Unread 25 Jun 2003, 17:38   #9
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Why aren't you using htmlentities? Are you sure those fields don't contain special characters? If so, writing out JS won't be too hard. It's only when you have to worry about text containing "</script>" that things get hard.

Is this supposed to be a nice interface to the data? If you use JS to write the original summary whenever someone clicks an option any changes they made to the old one (say they change their mind) will be lost - this would be horrible interface design. Perhaps you could manage storing and unstoring them but still, things like the a textarea's context menu "Undo" wouldn't work nicely. A different scheme is to output several textareas and make most hidden, this has the added advantages that it could be useable without JS, and only requires HTML-quoting instead of HTML-JS-quoting. It might mean the user submits a load of redundant data but that's rarely important and could be avoided with JS if necessary.
queball is offline   Reply With Quote
Unread 26 Jun 2003, 10:00   #10
kaos
peon
 
Join Date: Mar 2001
Posts: 163
kaos is an unknown quantity at this point
Code:
<?
	$query = "SELECT name, id, summary FROM projects";
	$result	= mysql_query ($query);
	// may not work, didn't test it, and never did such a strange thing :D
	while($projects[] = mysql_fetch_assoc($result))
		;
?>	
<html>
	<head>
	</head>
	<script type="text/JavaScript">
		var summaries = Array();
<?
		foreach($projects as $project)
		{
			echo 'summaries['.$project['id'].'] = "'.str_replace('"', '\"', $project['summary']).'";';
		}

		function setSummary(elem, id)
		{
			elem.value = summaries[id];
		}
	</script>
	<body>
[snip]

	<select name="user" onchange="setSummary(this.form.details, this.form.user.value)">
	<option value="0">Choose a project</option>
		<?
		// your other loop
		?>
	</select>

<textarea name="details" cols="40" rows="5" wrap="VIRTUAL" width=30></textarea>
...
this should get you a grip how you could do it
__________________
Elysium / patools
kaos is offline   Reply With Quote
Unread 26 Jun 2003, 10:07   #11
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by queball
[b]Why aren't you using htmlentities? Are you sure those fields don't contain special characters? If so, writing out JS won't be too hard. It's only when you have to worry about text containing "</script>" that things get hard.
I'm not using HTMLEntities atm cos I'm just testing, atm "summary" is just one or two words. You say the JS "won't be too hard", but my prob is that I hardly know any, and was asking for an example if possible

Quote:
Originally posted by queball
... this would be horrible interface design ...
No it wouldn't! All I'm doing is having a select list, when a user highlights an item, the text in a textarea changes to display a summary about that item.

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 26 Jun 2003, 10:20   #12
kaos
peon
 
Join Date: Mar 2001
Posts: 163
kaos is an unknown quantity at this point
Quote:
Originally posted by Mong

No it wouldn't! All I'm doing is having a select list, when a user highlights an item, the text in a textarea changes to display a summary about that item.

M.
looky looky 1 post up
__________________
Elysium / patools
kaos is offline   Reply With Quote
Unread 26 Jun 2003, 10:48   #13
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by kaos
Code:
	while($projects[] = mysql_fetch_assoc($result))
		;
This makes no sense to me...

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 26 Jun 2003, 10:59   #14
kaos
peon
 
Join Date: Mar 2001
Posts: 163
kaos is an unknown quantity at this point
Quote:
Originally posted by Mong
This makes no sense to me...

M.
Well
$foo[] = "bar";
adds "bar" as last element to the array $foo

so this:
while($projects[] = mysql_fetch_assoc($result))
;

fills $projects with all data from query, the only thing is (i just tested it) is, that it adds an empty entry at the end of it
so better change it to:

while($project = mysql_fetch_assoc($result))
{
$projects[] = $project;
}
__________________
Elysium / patools
kaos is offline   Reply With Quote
Unread 26 Jun 2003, 11:41   #15
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Superb! After mucho tweaking, it works! Thanks Kaos.

See it here!

Here's the code:

Code:
<?
include ("common_db.php");
$query = "SELECT name, id, summary FROM projects";
$result= mysql_query ($query);
// may not work, didn't test it, and never did such a strange thing 
while($project = mysql_fetch_assoc($result))
{
	$projects[] = $project;
}

?>
<html>
<head>

<script language="JavaScript" type="text/javascript">
var summaries = Array();
<?
foreach($projects as $project)
{
echo 'summaries['.$project['id'].'] = "'.str_replace('"', '\"', $project['summary']).'";';
}
echo "function setSummary(elem, id)";
echo '{';
echo "elem.value = summaries[id];";
echo '}';
?>
</script>
</head>
<body>

<form name="form1" method="post" action="<? echo $SERVER['PHP_SELF']; ?>">
<select name="user" onchange="setSummary(this.form.details, this.form.user.value)">
<option value="0">Choose a project</option>
<?
	$result= mysql_query ($query);
	while (list($name, $id, $summary) = mysql_fetch_row($result))
	{
		echo "<option value=\"".$id."\">".$name."</option>";
	}
?>
</select>
<textarea name="details" cols="40" rows="5" wrap="VIRTUAL" width=30></textarea>
</form>
</body>
</html>
Cheers!

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 26 Jun 2003, 17:11   #16
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Mong

No it wouldn't! All I'm doing is having a select list, when a user highlights an item, the text in a textarea changes to display a summary about that item.
Why use a textarea if it's not to be edited?
queball is offline   Reply With Quote
Unread 26 Jun 2003, 17:35   #17
BesigedB
Darling
 
BesigedB's Avatar
 
Join Date: Dec 2000
Location: Edinburgh
Posts: 890
BesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of lightBesigedB is a glorious beacon of light
Quote:
Originally posted by queball
Why use a textarea if it's not to be edited?
if you make it have no borders, transparant etc you can use it to display changing text in a nifty way
__________________
..
BesigedB is offline   Reply With Quote
Unread 26 Jun 2003, 18:46   #18
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by BesigedB
if you make it have no borders, transparant etc you can use it to display changing text in a nifty way
You can do the same in Flash, and it would look even better. Once youve decided that aestethics > useability, you might as well go all the way.
Nodrog is offline   Reply With Quote
Unread 26 Jun 2003, 18:56   #19
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by BesigedB
if you make it have no borders, transparant etc you can use it to display changing text in a nifty way
Explain. How is it any more nifty than using a <div> or something?
queball is offline   Reply With Quote
Unread 27 Jun 2003, 10:02   #20
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by queball
Explain. How is it any more nifty than using a <div> or something?
Because I only know how to do it with a textarea :-)

Nod: Flash would be an excessive option here, as it would require all users to have it, plus all my work must be 100% accessible by disabled ppl.

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 27 Jun 2003, 13:34   #21
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by Mong
Because I only know how to do it with a textarea :-)

Nod: Flash would be an excessive option here, as it would require all users to have it, plus all my work must be 100% accessible by disabled ppl.

M.
Flash has decent accessibility features. Having to switch back and forth from a textarea with a webpage reader doesn't sound usable.

Simple solution:
Code:
<h1>queball's Plans</h1>
<p>These are my projects for world domination.
<p>Select a project from the list to make it happen.
<h2><a name="list">List of Projects</a></h2>
<form action="execute.php" method="post">
<input type="radio" name="project" value="1">
<a href="#proj1">Project 1</a><br>
<input type="radio" name="project" value="2">
<a href="#proj2">Project 2</a><br>
<input type="radio" name="project" value="3">
<a href="#proj3">Project 3</a><br>
<input type="submit">
</form>
<h2>Descriptions</h2>
<h3><a name="proj1">Project 1</a></h3>
<p>Eat loads of chocolate.
<p><a href="#list">Back to List of Projects</a>
<h3><a name="proj2">Project 2</a></h3>
<p>Shoot Nelson Mandela.
<p><a href="#list">Back to List of Projects</a>
<h3><a name="proj3">Project 3</a></h3>
<p>Pretend to be disabled and form an army of loyal paraplegics.
<p><a href="#list">Back to List of Projects</a>
queball is offline   Reply With Quote
Unread 28 Jun 2003, 02:38   #22
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by Mong
Because I only know how to do it with a textarea :-)

Nod: Flash would be an excessive option here, as it would require all users to have it, plus all my work must be 100% accessible by disabled ppl.

M.
But lots of people have javascript disabled in their browsers because it is pretty much useless and 99% of its uses are abuses.

Having vital parts of your site's navigation depend on stuff like java/JS/flash/whatever is exceptionally bad design.
Nodrog is offline   Reply With Quote
Unread 29 Jun 2003, 20:02   #23
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Quote:
Originally posted by Nodrog
But lots of people have javascript disabled in their browsers because it is pretty much useless and 99% of its uses are abuses.
I used to respect you, but now you're talking crap. Nice stats, source?

Quote:
Originally posted by Nodrog
Having vital parts of your site's navigation depend on stuff like java/JS/flash/whatever is exceptionally bad design.
It's not dependant on it. It's an added feature, the site works totally perfectly without this. It's just an added feature.

I know that all the machines this will run on will have IE5 or better, and should all have Javascript enabled.

Designing for usability and accessibility is a major part of my job. So without sounding too BOFHish; I know what I'm on about

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 22:47.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018