Solved

radio buttons mysql and php : how-to

Posted on 2009-05-17
12
310 Views
Last Modified: 2013-12-12
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
Comment
Question by:yamya
  • 6
  • 3
  • 3
12 Comments
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24407461
I can help you.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24407494
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
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24407503
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24408098
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
 

Author Comment

by:yamya
ID: 24408406
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
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24409854
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 250 total points
ID: 24409926
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24412202
@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
 

Author Comment

by:yamya
ID: 24437109
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
 

Author Comment

by:yamya
ID: 24437158
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
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24438101
Knowledge is power. Use it wisely. Live long and prosper...

0
 
LVL 108

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now