|
25 Jun 2003, 12:39
|
#1
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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> </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.
|
|
|
25 Jun 2003, 13:23
|
#2
|
Let battle commence
Join Date: Feb 2002
Location: England
Posts: 732
|
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
|
|
|
25 Jun 2003, 14:20
|
#3
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
25 Jun 2003, 14:32
|
#4
|
Let battle commence
Join Date: Feb 2002
Location: England
Posts: 732
|
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
|
|
|
25 Jun 2003, 14:47
|
#5
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
25 Jun 2003, 15:38
|
#6
|
Let battle commence
Join Date: Feb 2002
Location: England
Posts: 732
|
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
|
|
|
25 Jun 2003, 15:51
|
#7
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
25 Jun 2003, 17:05
|
#8
|
Let battle commence
Join Date: Feb 2002
Location: England
Posts: 732
|
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
|
|
|
25 Jun 2003, 17:38
|
#9
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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.
|
|
|
26 Jun 2003, 10:00
|
#10
|
peon
Join Date: Mar 2001
Posts: 163
|
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
|
|
|
26 Jun 2003, 10:07
|
#11
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
26 Jun 2003, 10:20
|
#12
|
peon
Join Date: Mar 2001
Posts: 163
|
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
|
|
|
26 Jun 2003, 10:48
|
#13
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
26 Jun 2003, 10:59
|
#14
|
peon
Join Date: Mar 2001
Posts: 163
|
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
|
|
|
26 Jun 2003, 11:41
|
#15
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
26 Jun 2003, 17:11
|
#16
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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?
|
|
|
26 Jun 2003, 17:35
|
#17
|
Darling
Join Date: Dec 2000
Location: Edinburgh
Posts: 890
|
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
|
|
|
26 Jun 2003, 18:46
|
#18
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
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.
|
|
|
26 Jun 2003, 18:56
|
#19
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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?
|
|
|
27 Jun 2003, 10:02
|
#20
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
27 Jun 2003, 13:34
|
#21
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
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>
|
|
|
28 Jun 2003, 02:38
|
#22
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
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.
|
|
|
29 Jun 2003, 20:02
|
#23
|
Forever Delayed
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
|
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"
|
|
|
|
All times are GMT +1. The time now is 20:22.
| |