Computing.Net > Forums > Web Development > php help: submitting txt fields

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

php help: submitting txt fields

Reply to Message Icon

Name: AshTheIdiot
Date: August 31, 2006 at 12:01:17 Pacific
OS: Win XP Pro SP2
CPU/Ram: P4 3.2GHz, 1Gb Ram
Comment:

Hi,
I'm helping a friend out with his tab site and trying to get some practice in with php and I think I've come a little unstuck...

Basically, I have a form for adding an album by a band, you select the band name from a drop-down list (called from the db) and type in the album name, select the year it's released and select the no. of songs on the album.
On the next step of the form, a number of text fields are created from the no. of songs selected for you to enter the song names, and a checkbox is placed after each of them to check whether or not a tab is required for that particular song.

The problem I'm having is when the songs list is submitted to the database. How do I ensure that each text field adds a seperate entry to the database since each text field has the same name?

If it helps, I could upload the site so far. I've not actually written the code that submits data to the database yet, the forms are just retreiving data first to help the user fill everything in.

Any help would be great!



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: August 31, 2006 at 13:52:01 Pacific
Reply:

You might have a couple of options. I *believe* that when you submit fields with the same name, the values of the fields are concatenated together comma separated.

For example:
[input type=text name=song value=song1]
[input type=text name=song value=song2]

When submitted you will get:
$_POST[song] = "song1,song2"

You could test this out to see if I am correct. you could then see if you can access the values as an array, but I am doubtful - I think it is received as a single string. the problem with that is if the data has a comma in it!

What you should do (IMHO), is use a loop to create the text fields with unique names like this:
For example:
[input type=text name=song1 value=song1]
[input type=text name=song2 value=song2]

Then on the page that handles the submission you could insert each song using a similar loop:

--------EXAMPLE-----------
$i = 0;
while (isset($_POST["song".strval($i)])) {

//get the value of $_POST["song".strval($i)]
//and put into database, using $i as
//the song order value

$i ++
}


I have not tested that code, but you should get the idea.

Michael J


0

Response Number 2
Name: Laler
Date: August 31, 2006 at 17:57:55 Pacific
Reply:

in the element naming, if you submit 2 form elements with the same name, the last one's value is taken. You need to add [] after the element names, to get all values in an array.

---

<input type="text" name="test[]" />
<input type="text" name="test[]" />
<input type="submit" name="Submit" />

<?php
echo '<pre>';
echo print_r ($_POST, true);
echo '</pre>';
?>

---

:-)
---


0

Response Number 3
Name: AshTheIdiot
Date: September 1, 2006 at 02:49:09 Pacific
Reply:

That makes much more sense, thanks!

Now, I'm having a problem actually submitting them to the database...

I need the page that processes all the form information to add a different song value into the database for every song on the album. Which I'm struggling with for 2 reasons...

1. There are two tables that need to be written to when a new album is created: The 'album' table and the 'song' table. The problem is, when I add the songs to the song table I need the 'album_id' from the album table and that is created when the album data is added, which is at the same time. I know I could perform a small query to do this but is there a shorter way of doing it?

and...
2. I think this is just a syntax problem but I need to submit song data for every song on the album, so I need a loop that will do it but I can't seem to get it right. So far, I have (ignore the album_id part, I just placed that in for the time being):

for($i=1; $i <= $_POST['song_no']; $i++)
{
$sql = "INSERT INTO
song
(song_name,
band_id,
required)
VALUES
('".$_POST[song_name[$i]]."',
'".$_POST['album_id']."',
'".$_POST[required[$i]]."')";
}

I'm fairly sure that's just a syntax error, but I'm not sure how to get it working...

Thanks, again!


0

Response Number 4
Name: Laler
Date: September 1, 2006 at 05:14:12 Pacific
Reply:

I need the 'album_id' from the album table and that is created when the album data is added

The album_id is an auto (increment) value right? So you can use mysql_insert_id() . It'll get auto increment values from the last insert query done.


In the 2nd question, based on what you typed above, I think it should goes like this:

---
in the form:

[ form element, name="album_id" ]

[loop start]
[ form element, name="song_name[]" ]
[ form element, name="required[]" ]
[loop end]

---
Then in the processor:

for ($n = 0 ; $n < count ($_POST['song_name']) ; $n++)
{

...
VALUES (

'" . $_POST['song_name'][$n] . "',
'" . $_POST['required'][$n] . "'

)

}

---

Is there any reason why you started the loop index from 1 and not 0? because the first auto index is 0 (the first form element value in the submitter page), unless you explicitly define the element name like name="song_name[1]", name="song_name[5]", etc...

---
Fubar


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: php help: submitting txt fields

How to use PHP Nuke? www.computing.net/answers/webdevel/how-to-use-php-nuke/2094.html

PHP help... $_POST[var] www.computing.net/answers/webdevel/php-help-postvar/1589.html

how upload image into mysql with php www.computing.net/answers/webdevel/how-upload-image-into-mysql-with-php/4060.html