Re: Posting a list
From: L'Angel Admirable (jamesjiao_at_paradise.net.nz)
Date: 11/28/03
- Next message: L'Angel Admirable: "Re: How to get the data of the multiple select from html in the php code."
- Previous message: Jon King: "help with Log() function"
- In reply to: Guapo: "Posting a list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 29 Nov 2003 11:15:34 +1300
Hi,
The simplest way i can think of is to add a multi-select list box to the
form, so that whenever you click on the add button, the content of the text
box is saved to the list box. (because remember PHP is a server-side
language, it's not aware of any changes you make while you are on the client
side before you submit them). You can achieve this with Javascript.
So basically create a new multiple select list box in the form and name it
something like 'listbox[]', NOTE that the name itself is not salient, but
you WILL need the square brackets after it, because of this stupid thing in
PHP that, if you don't name a multi-select list box with the array form,
then it will only submit the last selected value in the list instead of an
array of all the selected values. Anyway, whenever you click the add button,
it calls the addContent() javascript function, which in turn adds the
textbox value to the list box. Once you are done with adding values, you can
click on the submit button to submit all the SELECTED values to the server
to be processed. So here, the form calls itself (PHP_SELF). Note that with
php, you don't get to access the submitted values from a multiple select
list box through the $_REQUEST global array, you simply treat the NAME of
the list box 'listbox[]' as the variable name you wish to refer to (as
$listbox in this case). the rest is straight forward, you should use the php
count() function to obtain the number of elements in an array... and so on..
If there is any obscurity that baffles you in my code, just give me a
buzz... either reply to this newsgroup or to me personally. I tested the
code, it works :p.
==James=
Do the following:
-----------------
<?php
if ($_REQUEST['submit']) {
// this should retrieve all the values as an array that have been SELECTED
in the list box.
$multiselectvalues = $all_contents;
// now print everything.
if (is_array($multiselectvalues)) {
print "Values you have added to the multiselect list box: <p><br>";
for ($i = 0; $i < count($multiselectvalues); $i++) {
print $multiselectvalues[$i] . "<br>";
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Add to a multiselect box</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
<!--
function addContent(stuff) {
/* don't add to the list if stuff is null or is empty */
if (stuff!=null && stuff.length > 0) {
/* add the item to the end of the multiple select list */
newOption = new Option(stuff);
path2listbox = document.da_form.elements['all_contents[]'];
path2listbox.options[path2listbox.length] = newOption;
// you have to select the new item in the list if you want this value to
be passed to php to be processed.
path2listbox.options[path2listbox.length - 1].selected = true;
}
}
-->
</script>
</head>
<body>
<form name="da_form" enctype="application/x-www-form-urlencoded"
method="post" action="<?php print $_SERVER['PHP_SELF'];?> ">
<input type="text" name="da_content"><input type="button" name="add"
value="Add" onClick="addContent(document.da_form.da_content.value);">
<p>
<select name="all_contents[]" multiple>
</select>
<p>
<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
-- ==================================================== "Have a care of the manner in which you turn towards the dead. Think not of that which perishes. Gaze steadily. You will perceive the living light of your well-beloved dead in the depths of heaven." -- M. Myriel "Guapo" <bhca90210@hotmail.com> wrote in message news:3601318d.0311281139.268e2286@posting.google.com... > Let's say I have a simple form with a TEXTBOX, an ADD button next to > it and a SUBMIT button at the bottom. > > I want to be able to type something in the textbox, click add, then > type something else, click add, etc until I am ready to submit. > > When I click submit I want to be able to pass each and every 'name' I > have added to another form. Does anyone have any idea on how to > implement this in a simple way? > > I have been scratching my head with this for a while and I have not > fond a solution yet. > > Thanks.
- Next message: L'Angel Admirable: "Re: How to get the data of the multiple select from html in the php code."
- Previous message: Jon King: "help with Log() function"
- In reply to: Guapo: "Posting a list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]