p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: How do you send info from checkboxes on forms to mysql database?


Message #1 by "Debra Scott" <hlawebservice@h...> on Thu, 14 Feb 2002 20:51:02
Hi,

I am trying to send information using php from a form to a mysql 

database.  The form info was formerly emailed using the following:



<input type="checkbox" name="$Option1" value="on">



There are 6 options and they are listed as answers to the question,"What 

is your profession?"



The answer can be 1 or more of these options which would be indicated by 

placing a check in the box.



How do I send the info that a  box has been checked to a database?



Do I need to use an array such as:

<input type="checkbox" name="visitor_profession[]" value="on">

In which case do I then add one column to the database as: 

visitor_profession



Any advice would be appreciated,

Debra
Message #2 by "Hermawan Haryanto" <hermawan@h...> on Fri, 15 Feb 2002 05:38:20 +0700
Dear sir,

What are u trying to do,

do you want to your user select 1 of 6 or more then 1 of 6 ?, if I can see

from the question "What  is your profession?" I assume that you want the

user is answering a single answer, and I believe the solution is not

CHECKBOX, but the solution is using RADIO BUTTON and/or TEXT

here is the example :



--- input ---

<form method=post>

<input type="radio" name="visitor_proffesion" value="Web Developer">Web

Developer<br>

<input type="radio" name="visitor_proffesion" value="Web Programmer">Web

Programmer<br>

<input type="radio" name="visitor_proffesion" value="Web Designer">Web

Designer<br>

<input type="submit" value="Submit">

</form>

---end of input--



---simple processor--

if(strlen(trim($HTTP_POST_VARS[visitor_proffesion]))>0){

  $sql="INSERT INTO table (visitor_proffesion) VALUES

('$HTTP_POST_VARS[visitor_proffesion]')";

  ....//execute your query here

}



Thanks



Hermawan Haryanto

hermawan@h...



----- Original Message -----

From: "Debra Scott" <hlawebservice@h...>

To: "beginning php" <beginning_php@p...>

Sent: Thursday, February 14, 2002 8:51 PM

Subject: [beginning_php] How do you send info from checkboxes on forms to

mysql database?





> Hi,

> I am trying to send information using php from a form to a mysql

> database.  The form info was formerly emailed using the following:

>

> <input type="checkbox" name="$Option1" value="on">

>

> There are 6 options and they are listed as answers to the question,"What

> is your profession?"

>

> The answer can be 1 or more of these options which would be indicated by

> placing a check in the box.

>

> How do I send the info that a  box has been checked to a database?

>

> Do I need to use an array such as:

> <input type="checkbox" name="visitor_profession[]" value="on">

> In which case do I then add one column to the database as:

> visitor_profession

>

> Any advice would be appreciated,

> Debra




$subst('Email.Unsub').

>



Message #3 by "Krister" <krister@m...> on Thu, 14 Feb 2002 23:37:46 +0100
Hi



Use PHP only inside PHP tags. The names should go like "Option1" and

"visitor_profession". Like in following:



<html>

<body>

<form action="<?php echo $PHP_SELF; ?>">

<input name="option1" type="text"><br>

<input type="submit">

</form>

<?php echo $option1; ?>

</body>

</html>



Krister



----- Original Message -----

From: "Debra Scott" <hlawebservice@h...>

To: "beginning php" <beginning_php@p...>

Sent: Thursday, February 14, 2002 8:51 PM

Subject: [beginning_php] How do you send info from checkboxes on forms to

mysql database?





> Hi,

> I am trying to send information using php from a form to a mysql

> database.  The form info was formerly emailed using the following:

>

> <input type="checkbox" name="$Option1" value="on">

>

> There are 6 options and they are listed as answers to the question,"What

> is your profession?"

>

> The answer can be 1 or more of these options which would be indicated by

> placing a check in the box.

>

> How do I send the info that a  box has been checked to a database?

>

> Do I need to use an array such as:

> <input type="checkbox" name="visitor_profession[]" value="on">

> In which case do I then add one column to the database as:

> visitor_profession

>

> Any advice would be appreciated,

> Debra




$subst('Email.Unsub').

>



Message #4 by "Nikolai Devereaux" <yomama@u...> on Thu, 14 Feb 2002 15:38:27 -0800

Heya there troopers,



Krister fixed a syntactic issue, but that wasn't really a solution to the

problem.



Hermawan's radio suggestion was valid, if not for this line in the original

post:



> The answer can be 1 or more of these options which would

> be indicated by placing a check in the box.



The problem I have with the original post is that the database is what's

important here -- NOT the php code.



You can name the input variables whatever you want, it's easy enough to get

them from the target page of the form.



Let's say you have a database table with a column "Profession".  This column

only holds an id of a profession.



The profession ID's are 0 - 5 (or 1 - 6, whatever) corresponding to the 6

profession options you have.



<form action="target.php" method="get">

  <input type="checkbox" name="profession[]"> Web Developer <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>



Message #5 by "Nikolai Devereaux" <yomama@u...> on Thu, 14 Feb 2002 15:39:13 -0800

ARGH -- hold on, the post got sent prematurely.  lemme finish and repost in

its entirety.




  Return to Index