LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2003, 05:01 AM   #1
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
HTML, PHP, MySQL sets


question 1:

i have list, where the user can choose 0-30 options.

when i "POST" that data, how does the webserver see it if im using PHP.

it's a list of College Majors and the form will be handled by PHP.
I know there is a $_POST['variable_name']
how do i store that (i'm a little new to php and have never handled this type of form field before ever).


question 2:

is there anything special i need to know about gettin the choices into a MySQL database. i've created the database and have a field called MAJORS which is a set with the 30 majors listed.
 
Old 09-03-2003, 02:24 PM   #2
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Original Poster
Rep: Reputation: 47
ok how abou this then,

a quick howto on php arrays. i'll also go look at the php website for help on this.
 
Old 09-03-2003, 02:36 PM   #3
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Since nobody has answered you yet, I'll give it a shot. I don't have access to a webserver that can process PHP at the moment, so I can't check what I'm about to say - it's just theory.

I'm guessing that when you submit a list on a form with multiple options selected, php will send an array. So let's say that the name of your list is "majors", php will receive $majors[].

You can handle this array as in any other language basically, just try this:

$i = 0;

while ($i < len($majors)) {
echo $majors[$i];
$i++;
}

As I said, no guarantees about this bit of code, just see if it helps you any further.
 
Old 09-03-2003, 02:42 PM   #4
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
I just realized I didn't address your second question. To insert the data in a MySQL db, just do the following:

Code:
$host="your_host";
$username="your_user";
$password="your_password";
$database="your_db";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "<h1><center>could not connect to db</center></h1>");

$sql="INSERT INTO your_table(col1,col2,...) VALUES('$value1',$value2,...)";
$result = mysql_query($sql);
In this case, value1 is a varchar (alfanumeric), so it is surrounded by quotes. Value2 is numeric so there are no quotes.
 
Old 09-03-2003, 03:42 PM   #5
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Original Poster
Rep: Reputation: 47
well here is what i know.

if your form's method=POST then the stuff is stored in the $POST[] array. it doesnt seem to send an array.

when i do this:

$myarray = $_POST['states']; (i did a test form with a few state)

i get an error in the foreach because myarray isnt really an array.

if i try:

$myarray = explode($_POST['state]);

i only get 1 elements in the array and it prints the last optiong in the list that the user selects

you can actually go see what i have done:

http://gigahurts.net/practice/scroll-menu.html

i cant seem to get all of the states selected into an array using GET or POST. If i can do that, the SQL part wont be so hard.
 
Old 09-03-2003, 05:09 PM   #6
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Original Poster
Rep: Reputation: 47
it took some digging but i found it.

incase anyone else runs up on this issue:


4. How do I get all the results from a select multiple HTML tag?

The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. ie. <select name="var" multiple>

Each selected option will arrive at the action handler as: var=option1
var=option2
var=option3

Each option will overwrite the contents of the previous $var variable. The solution is to use PHP's "array from form element" feature. The following should be used: <select name="var[]" multiple>

This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.

Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example: variable = documents.forms[0].elements['var[]'];

so basically, add the [] to the name of the field
Code:
<select name="states[]" multiple>
and then in php just do this:

Code:
$array = $_POST['states'];
and it works

there is a note there about JavaScript, but i dont use JavaScript so i didnt pay it any attention.

Last edited by Robert0380; 09-03-2003 at 05:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 03:27 PM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 03:01 PM
html, xml, php, mysql atheist Programming 8 06-07-2004 01:28 PM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM
character sets in Linux, MySql, PHP, Apache prague_expat Linux - General 1 09-21-2003 05:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration