• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

radio buttons mysql and php : how-to

I cannot seem to make the radio button value pass through. i don't want this field  to have errors,
One script that i tried used a combination of Javascript and name/value/checked settings for the HTML input tag. I either got a blank page or radio buttons that did nothing.

then i thought to try and make an array but i don't have that skill set, yet. so, i'm back here hoping for help.

I commented out stuff that didn't work.

here is the code i am using that works, albeit somewhat clumsy. maybe there is a validation expression that could be written on the field?

all help appreciated.
thank you
amy



FIRST SNIP 
<form action="2addApplication.php" method="post" name="addForm2" >
<?php
//$chemNum=array("1","2","3","4","5","6","7","8");
//$chemType=array("AA","CA","TA","ET","UV","ES","TM","PU");
//$chemID=array($chemNum[],$chemType[]);
?>
Chemistry: Enter as a number value - pick from list below:<br/> 
<input type="text" name="chem_id" />
 
<ul><li> value="1" AA</li>
 <li>value="2"/>CA</li>
 <li>value="6"/>ES</li>
 <li>value="4"/>ET</li>
 <li>value="5"/>UV</li>
 <li>value="8"/>PU</li>
 <li>value="3"/>TA</li>
 <li>value="7"/>TM</li>
 </ul>
 
Application: Provide initial details <br />
 
<textarea cols="100" rows="10" name="application"></textarea> <br />
 
 
<br />
RD_Prod_Num: Enter development product number > <input type="text" name="RD_Prod_Num" />
<br /><br />
 
 <input type="hidden" name="RD_Prod_Num2" value="RD_Prod_Num2" /> 
Company Name: Supply enduser name, if known, or distributor > <input type="text" name="company" /><br />
<br />
Date Opened: Product development start date > <input type="text" name="Date_Opened" /><br />
<br />
Sales_Summary: > <input type="text" name="SalesSummary" />
 
Date_Closed: > <input type="text" name="Date_Closed" /><br /><br />
 
ProductManager: > <input type="text" name="ProductManager" />
 
Country: > <input type="text" name="Country" />
<input type="submit" name="add" value="addNew" />
<input type="reset" value="Clear Form"/><br>
 
</form>
 
<hr />
 
<?php
if($add) {
	
mysql_connect("host","user","pw");
mysql_select_db("joe");
 
$query="INSERT INTO RD_Product_Application (id, application, RD_Prod_Num, company, Date_Opened, SalesSummary, Date_Closed, ProductManager, Country)";
$query.="VALUES (NULL, '$application','$RD_Prod_Num','$company','$Date_Opened','$SalesSummary','$Date_Closed','$ProductManager','$Country')";
$result=mysql_query($query);
$result="SELECT * FROM notes WHERE RD_Prod_Num_id LIKE '$RD_Prod_Num2'";
$r2=mysql_query($r2);
$r2="INSERT INTO notes (id, prodNumber) ";
$r2.="VALUES (NULL, '$RD_Prod_Num_id')";
if ($r2) 
 
		  echo "Joe has added " . $RD_Prod_Num . " to the database and opened a lab notebook for you ";
 
 
 
}
?>

Open in new window

0
yamya
Asked:
yamya
  • 6
  • 3
  • 3
1 Solution
 
NerdsOfTechTechnology ScientistCommented:
I can help you.
0
 
NerdsOfTechTechnology ScientistCommented:
Use <input> radio buttons named chem_id with corresponding "values" and eliminate the textbox
FIRST SNIP 
<form action="2addApplication.php" method="post" name="addForm2" >
<?php
//$chemNum=array("1","2","3","4","5","6","7","8");
//$chemType=array("AA","CA","TA","ET","UV","ES","TM","PU");
//$chemID=array($chemNum[],$chemType[]);
?>
Chemistry: pick from list below:<br/> 
 
<p>
<input name="chem_id" type="radio" value="1"> AA<br />
<input name="chem_id" type="radio" value="2">CA<br />
<input name="chem_id" type="radio" value="3">ES<br />
<input name="chem_id" type="radio" value="4">ET<br />
<input name="chem_id" type="radio" value="5">UV<br />
<input name="chem_id" type="radio" value="6">PU<br />
<input name="chem_id" type="radio" value="7">TA<br />
<input name="chem_id" type="radio" value="8">TM<br />
</p>
 
Application: Provide initial details <br />
 
<textarea cols="100" rows="10" name="application"></textarea> <br />
 
 
<br />
RD_Prod_Num: Enter development product number > <input type="text" name="RD_Prod_Num" />
<br /><br />
 
<input type="hidden" name="RD_Prod_Num2" value="RD_Prod_Num2" /> 
Company Name: Supply enduser name, if known, or distributor > <input type="text" name="company" /><br />
<br />
Date Opened: Product development start date > <input type="text" name="Date_Opened" /><br />
<br />
Sales_Summary: > <input type="text" name="SalesSummary" />
 
Date_Closed: > <input type="text" name="Date_Closed" /><br /><br />
 
ProductManager: > <input type="text" name="ProductManager" />
 
Country: > <input type="text" name="Country" />
<input type="submit" name="add" value="addNew" />
<input type="reset" value="Clear Form"/><br>
 
</form>
 
<hr />
 
 
 
 
 
 
 
<?php
if($add) {
        
mysql_connect("host","user","pw");
mysql_select_db("joe");
 
$query="INSERT INTO RD_Product_Application (id, application, RD_Prod_Num, company, Date_Opened, SalesSummary, Date_Closed, ProductManager, Country)";
$query.="VALUES (NULL, '$application','$RD_Prod_Num','$company','$Date_Opened','$SalesSummary','$Date_Closed','$ProductManager','$Country')";
$result=mysql_query($query);
$result="SELECT * FROM notes WHERE RD_Prod_Num_id LIKE '$RD_Prod_Num2'";
$r2=mysql_query($r2);
$r2="INSERT INTO notes (id, prodNumber) ";
$r2.="VALUES (NULL, '$RD_Prod_Num_id')";
if ($r2) 
 
echo "Joe has added " . $RD_Prod_Num . " to the database and opened a lab notebook for you ";
 
}
?>

Open in new window

0
 
NerdsOfTechTechnology ScientistCommented:
Minor cosmetic fix:

Also, if chem_id is mandatory then prevalidate that chem_id is not null

=NerdsOfTech
FIRST SNIP 
<form action="2addApplication.php" method="post" name="addForm2" >
<?php
//$chemNum=array("1","2","3","4","5","6","7","8");
//$chemType=array("AA","CA","TA","ET","UV","ES","TM","PU");
//$chemID=array($chemNum[],$chemType[]);
?>
Chemistry: pick from list below:<br/> 
 
<p>
<input name="chem_id" type="radio" value="1">AA<br />
<input name="chem_id" type="radio" value="2">CA<br />
<input name="chem_id" type="radio" value="3">ES<br />
<input name="chem_id" type="radio" value="4">ET<br />
<input name="chem_id" type="radio" value="5">UV<br />
<input name="chem_id" type="radio" value="6">PU<br />
<input name="chem_id" type="radio" value="7">TA<br />
<input name="chem_id" type="radio" value="8">TM<br />
</p>
 
Application: Provide initial details <br />
 
<textarea cols="100" rows="10" name="application"></textarea> <br />
 
 
<br />
RD_Prod_Num: Enter development product number > <input type="text" name="RD_Prod_Num" />
<br /><br />
 
<input type="hidden" name="RD_Prod_Num2" value="RD_Prod_Num2" /> 
Company Name: Supply enduser name, if known, or distributor > <input type="text" name="company" /><br />
<br />
Date Opened: Product development start date > <input type="text" name="Date_Opened" /><br />
<br />
Sales_Summary: > <input type="text" name="SalesSummary" />
 
Date_Closed: > <input type="text" name="Date_Closed" /><br /><br />
 
ProductManager: > <input type="text" name="ProductManager" />
 
Country: > <input type="text" name="Country" />
<input type="submit" name="add" value="addNew" />
<input type="reset" value="Clear Form"/><br>
 
</form>
 
<hr />
 
 
 
 
 
 
 
<?php
if($add) {
        
mysql_connect("host","user","pw");
mysql_select_db("joe");
 
$query="INSERT INTO RD_Product_Application (id, application, RD_Prod_Num, company, Date_Opened, SalesSummary, Date_Closed, ProductManager, Country)";
$query.="VALUES (NULL, '$application','$RD_Prod_Num','$company','$Date_Opened','$SalesSummary','$Date_Closed','$ProductManager','$Country')";
$result=mysql_query($query);
$result="SELECT * FROM notes WHERE RD_Prod_Num_id LIKE '$RD_Prod_Num2'";
$r2=mysql_query($r2);
$r2="INSERT INTO notes (id, prodNumber) ";
$r2.="VALUES (NULL, '$RD_Prod_Num_id')";
if ($r2) 
 
echo "Joe has added " . $RD_Prod_Num . " to the database and opened a lab notebook for you ";
 
}
?>

Open in new window

0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Ray PaseurCommented:
You can set a default in the form side of the script, and/or check to see if the field isset() in the action script.  Install this and run it - it's a simplified teaching example showing what comes through in the $_POST array.

Then change it to remove the word "checked" from the Maybe line and run it again without checking any of the fields.  You will see that the "radio_button" variable is not present in $_POST at all.

Hope that helps clear up your understanding of radio buttons in PHP forms.  BTW, the same sort of thing happens with checkboxes.

Best regards to all, ~Ray
<?php // RAY_radio_yes.php
if (!empty($_POST))
{
	var_dump($_POST); // SHOW THE POST DATA
}
?>
<form action="<?=$PHP_SELF?>" method="post">
<input type="radio" name="radio_button" value="yes" />Yes<br/>
<input type="radio" name="radio_button" value="no" />No<br/>
<input type="radio" name="radio_button" value="maybe" checked />Maybe (this is the default)<br/>
<input type="submit" name="submit" value="Go!" />
</form>

Open in new window

0
 
yamyaAuthor Commented:
i can not get past how the declare the variable to use in a query. no matter that i can see the output of the query in your echo, i am unable to replace it with anything more sophisiticated.


<?php // RAY_radio_yes.php
$result=mysql_query($var_dump);

if (!empty($_POST))
{
        $result=($_POST);

                  if ($result)
                  
            echo "$result";
            }
?>

this and

<?php // RAY_radio_yes.php
$result=mysql_query($var_dump);

if (!empty($_POST))
{
        $result=($_POST);

                  if ($result)
                  
            echo "$result";
            }
            // SHOW THE POST DATA
}
?>
this both

echo  "array".


i've typed that wretched bullet list four times and no matter what it only makes pretty buttons and labels with values that I cannot grab onto.

thanks for your continued help.

kind regards,
amy
0
 
NerdsOfTechTechnology ScientistCommented:
We are here to help. Can you post the link to this page we might be able to work faster if we can see the output.

Thanks
0
 
NerdsOfTechTechnology ScientistCommented:
Did you try my solution yet? Here is the copy
FIRST SNIP 
<form action="2addApplication.php" method="post" name="addForm2" >
<?php
//$chemNum=array("1","2","3","4","5","6","7","8");
//$chemType=array("AA","CA","TA","ET","UV","ES","TM","PU");
//$chemID=array($chemNum[],$chemType[]);
?>
Chemistry: pick from list below:<br/> 
 
<p>
<input name="chem_id" type="radio" value="1">AA<br />
<input name="chem_id" type="radio" value="2">CA<br />
<input name="chem_id" type="radio" value="3">ES<br />
<input name="chem_id" type="radio" value="4">ET<br />
<input name="chem_id" type="radio" value="5">UV<br />
<input name="chem_id" type="radio" value="6">PU<br />
<input name="chem_id" type="radio" value="7">TA<br />
<input name="chem_id" type="radio" value="8">TM<br />
</p>
 
Application: Provide initial details <br />
 
<textarea cols="100" rows="10" name="application"></textarea> <br />
 
 
<br />
RD_Prod_Num: Enter development product number > <input type="text" name="RD_Prod_Num" />
<br /><br />
 
<input type="hidden" name="RD_Prod_Num2" value="RD_Prod_Num2" /> 
Company Name: Supply enduser name, if known, or distributor > <input type="text" name="company" /><br />
<br />
Date Opened: Product development start date > <input type="text" name="Date_Opened" /><br />
<br />
Sales_Summary: > <input type="text" name="SalesSummary" />
 
Date_Closed: > <input type="text" name="Date_Closed" /><br /><br />
 
ProductManager: > <input type="text" name="ProductManager" />
 
Country: > <input type="text" name="Country" />
<input type="submit" name="add" value="addNew" />
<input type="reset" value="Clear Form"/><br>
 
</form>
 
<hr />
 
 
 
 
 
 
 
<?php
if($add) {
        
mysql_connect("host","user","pw");
mysql_select_db("joe");
 
$query="INSERT INTO RD_Product_Application (id, application, RD_Prod_Num, company, Date_Opened, SalesSummary, Date_Closed, ProductManager, Country)";
$query.="VALUES (NULL, '$application','$RD_Prod_Num','$company','$Date_Opened','$SalesSummary','$Date_Closed','$ProductManager','$Country')";
$result=mysql_query($query);
$result="SELECT * FROM notes WHERE RD_Prod_Num_id LIKE '$RD_Prod_Num2'";
$r2=mysql_query($r2);
$r2="INSERT INTO notes (id, prodNumber) ";
$r2.="VALUES (NULL, '$RD_Prod_Num_id')";
if ($r2) 
 
echo "Joe has added " . $RD_Prod_Num . " to the database and opened a lab notebook for you ";
 
}
?>

Open in new window

0
 
Ray PaseurCommented:
@yamya: I can tell from your questions and teh code posted above that you need a good starting education in PHP and MySQL.  This book will help with that - it is useful as a tutorial and a reference, and you can get it right now in PDF format so you can begin learning the right ways to do things.  It is a permanent part of my professional library.

http://www.sitepoint.com/books/phpmysql1/

Best of luck with your project, ~Ray
0
 
yamyaAuthor Commented:
NoT - thank you yes that works. i studied your solution to see where my gap of understanding was. It was the array of arrays, as I am going to call it for personal imprinting!

Ray - I have a huge stack of books, well thumbed, but sometimes it just wants for a certain way of looking at a concept to get it and move on. Arrays have been a stickler for me from day one. they are the grubbiest pages in my books!! I think the real trick here was seeing the solution with data and labels that mean something to me in the context of my project. then it's all a big voila! from here.

 what i have been doing is hoarding the bits that do work and creating my own library of snippets.  As well as browsing the web and forums for brains to pick.

you folks are great, i;m glad i found the site, and hope to be a contributor in my own way some day.

cheerio!
0
 
yamyaAuthor Commented:
ok wait now i'm confused again -  the arrays are specced out as comments in the php section and have nothing to do with the success. they are leftover from my original code.

I really did type that same list of radio buttons four times, with no success , before copying and pasting NoT's snippet, so i can only say there was a misplaced character somewhere.

oh well it works and i can reuse it and , off to the next phase. you're all still great and i'm getting smarter every time.
0
 
NerdsOfTechTechnology ScientistCommented:
Knowledge is power. Use it wisely. Live long and prosper...

0
 
Ray PaseurCommented:
An excellent book of snippets (and more) is "PHP5 in Practice" by Eli White.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now