Reply
Pulling different values from radio buttons
Old 05-10-2007, 11:09 AM Pulling different values from radio buttons
Average Talker

Posts: 26
Name: Darren Driver
Hey all, first time poster, long time coder.

I'm almost going bald from pulling my hair out for this script. I'm trying to make an online form that will send info to my SQL database and update the tables on it. I've had success so far, however, I want it to leave certain amount of cells NULL depending on which radio button is checked.

I have two PHP pages in this process: animation_submit.php (form) and animation_insert.php (updater).

The radio buttons (and it's following selections) in animation_submit are coded as follows:

Code:
<strong>Number of Authors: </strong><br />
    <strong>
      <input name="authors" type="radio" id="1" value="author1" checked="checked"/>
  </strong>1 <strong>
  <input name="authors" type="radio" id="2" value="author2"/>
  </strong>2<strong>
  <input name="authors" type="radio" id="3" value="author3"/>
  </strong>3<strong>
  <input name="authors" type="radio" id="4" value="author4"/>
  </strong>4<strong>
  <input name="authors" type="radio" id="5" value="author5"/>
  </strong>5<strong>
  <input name="authors" type="radio" id="6" value="author6"/>
  </strong>6</p>
  <p><strong> 
    <label></label>
    Author Name:</strong>
    <input name="membername1" type="text" id="membername1" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid1" type="text" size="2" />
</p>
  <p><strong>
    Co-Author 1 Name:</strong>
    <input name="membername2" type="text" id="membername2" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid2" type="text" id="memberid2" size="2" />
</p>
  <p><strong>
    Co-Author 2 Name:</strong>
    <input name="membername3" type="text" id="membername3" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid3" type="text" id="memberid3" size="2" />
</p>
  <p><strong>
    Co-Author 3 Name:</strong>
    <input name="membername4" type="text" id="membername4" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid4" type="text" id="memberid4" size="2" />
</p>
  <p><strong>
    Co-Author 4 Name:</strong>
    <input name="membername5" type="text" id="membername5" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid5" type="text" id="memberid5" size="2" />
</p>
  <p><strong>
    Co-Author 5 Name:</strong>
    <input name="membername6" type="text" id="membername6" size="25" maxlength="25" />
    <strong>ID:</strong>
    <input name="memberid6" type="text" id="memberid6" size="2" />
</p>
When the submit query button is pressed, it then goes to animation_insert.php - what I want to happen is for certain fields to be left NULL depending on the selection, so if number of authors is set to value "author1", membername2-6 and memberid2-6 should be left NULL.

Here's my coding so far for animation_insert:

Code:
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db("database", $con);

if ( $authors.value == "author1" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[type]')";
  }
elseif ( $authors.value == "author2" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, MemberID2, MemberName2, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[memberid2]','$_POST[membername2]','$_POST[type]')";
  }
elseif ( $authors.value == "author3" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, MemberID2, MemberName2, MemberID3, MemberName3, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[memberid2]','$_POST[membername2]','$_POST[memberid3]','$_POST[membername3]','$_POST[type]')";
  }
elseif ( $authors.value == "author4" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, MemberID2, MemberName2, MemberID3, MemberName3, MemberID4, MemberName4, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[memberid2]','$_POST[membername2]','$_POST[memberid3]','$_POST[membername3]','$_POST[memberid4]','$_POST[membername4]','$_POST[type]')";
  }
elseif ( $authors.value == "author5" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, MemberID2, MemberName2, MemberID3, MemberName3, MemberID4, MemberName4, MemberID5, MemberName5, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[memberid2]','$_POST[membername2]','$_POST[memberid3]','$_POST[membername3]','$_POST[memberid4]','$_POST[membername4]','$_POST[memberid5]','$_POST[membername5]','$_POST[type]')";
  }
elseif ( $authors.value == "author6" )
  {
$sql="INSERT INTO animation (Name, ShortName, Width, Height, Comment, MemberID1, MemberName1, MemberID2, MemberName2, MemberID3, MemberName3, MemberID4, MemberName4, MemberID5, MemberName5, MemberID6, MemberName6, Type)
VALUES
('$_POST[name]','$_POST[shortname]','$_POST[width]','$_POST[height]','$_POST[comment]','$_POST[memberid1]','$_POST[membername1]','$_POST[memberid2]','$_POST[membername2]','$_POST[memberid3]','$_POST[membername3]','$_POST[memberid4]','$_POST[membername4]','$_POST[memberid5]','$_POST[membername5]','$_POST[memberid6]','$_POST[membername6]','$_POST[type]')";
  }
else
  {
  die('Error, error, Darren code me properly');
  }

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
Obviously, "username", "password" and "database" have been modified above for security reasons. I'm pretty sure this code is wrong, but I have no idea what it should be:

Code:
if ( $authors.value == "author1" )
I've been racking my brains over this for 2-3 days, any help would be appreciated!

Thanks in advance!
A17roolz is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-10-2007, 12:17 PM Re: Pulling different values from radio buttons
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Quote:
$authors.value == "author1"
this is more like JavaScript

for php :
$_POST['authors'] == "author1"

but i would advice to use switch :
Code:
switch ( $_POST['authors']) {         
         case 'author1' : insert for author1 
                              break; 
         case 'author2' : insert for author2
                              break;
         case 'author3' : insert for author3 
                              break;

}
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog

Last edited by solomongaby : 05-10-2007 at 12:19 PM.
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-10-2007, 02:00 PM Re: Pulling different values from radio buttons
Average Talker

Posts: 26
Name: Darren Driver
That code works perfectly! I can't thank you enough, I think my blood pressure's dropped, haha.
A17roolz is offline
Reply With Quote
View Public Profile
 
Old 05-10-2007, 02:30 PM Re: Pulling different values from radio buttons
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
some TK would do
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-10-2007, 03:05 PM Re: Pulling different values from radio buttons
Average Talker

Posts: 26
Name: Darren Driver
Actually I'm new here, how do I give TK? I would Private message and ask but I can't private message yet...
A17roolz is offline
Reply With Quote
View Public Profile
 
Old 05-10-2007, 03:20 PM Re: Pulling different values from radio buttons
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
there's a buton near my username on the left called Add to my Talkupation!
click on it ... and write a coment
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 05-10-2007, 04:01 PM Re: Pulling different values from radio buttons
Average Talker

Posts: 26
Name: Darren Driver
Wasn't there before, but either way, TK'd! Thanks alot!
A17roolz is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Pulling different values from radio buttons
 

Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16918 seconds with 13 queries