php code for check box

Need PHP code for checkbox help

Page 1 of 1

5 Replies - 2222 Views - Last Post: 08 August 2008 - 01:58 AM Rate Topic: -----

#1 nicoscode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 08

php code for check box

Posted 07 August 2008 - 08:36 PM

I'm trying to figure this out and pulling my hair out. I'm a graphic designer: I don't know PHP code, and need that info for a check box on a site I'm creating. Any help would be seriously appreciated! Below is the HTML code:

			<tr>
			  <td>Shirt Size(check one):</td>
			  <td><label>
				<input type="checkbox" name="Adult: Small" id="Adult: Small" />
			  Adult: Small<br />
			  <input type="checkbox" name="Adult: Medium" id="Adult: Medium" />
			  Adult: Medium<br />
			  <input type="checkbox" name="Adult: Large" id="Adult: Large" />
			  Adult: Large<br />
			  <input type="checkbox" name="Adult: X-Large" id="Adult: X-Large" />
			  Adult: X-Large<br />
			  <input type="checkbox" name="Adult: XX-Large" id="Adult: XX-Large" />
			  Adult: XX-Large<br />
			  <input type="checkbox" name="Youth: Small" id="Youth: Small" />
			  Youth: Small<br />
			  <input type="checkbox" name="Youth: Medium" id="Youth: Medium" />
			  Youth: Medium<br />
			  <input type="checkbox" name="Youth: Large" id="Youth: Large" />
			  Youth: Large</label></td>
			</tr>
			<tr>
			  <td colspan="2"><p>Waiver<br />
			  I know that walking is a potentially hazardous activity.</p>
			  <p>
				<label>
				<input type="checkbox" name="Agree to Waiver" id="Agree to Waiver" />
				I Agree to Waiver</label>
			  </p></td>
			</tr>
			<tr>
			<td>Code <br/><font size="1">(You must type the code in the box to submit the form)</font></td>
			<td><input type="text" name="secure" id="text1" size="7" maxlength="7"/>
			 <img src="images/code01.jpg" width="162" height="40" /></td>
			</tr>
		  </table>
		  <div align="left">
			<blockquote>
			  <blockquote>
				<blockquote>
				  <blockquote>
					<p>
					  <input type="submit" value="Submit ">
					  <input type="reset" value="Clear Form">
					</p>
				  </blockquote>
				</blockquote>
			  </blockquote>
			</blockquote>
		  </div>
		</div>
	</dd></dl>
  </form>




Seriously frustrated, and need info in a hurry.

-n

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: php code for check box

#2 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: php code for check box

Posted 07 August 2008 - 08:48 PM

View Postnicoscode, on 7 Aug, 2008 - 11:36 PM, said:

I don't know PHP code


What's important to remember is that PHP will execute server side, offering the html code to the browser.
Javascript, on the other hand, works on the client side. So you'll need to parse the variables back to your server in order to read the values with PHP.

Think of it like this.

Web Server--->---->---->---->---->--->Clients Browser
PHP variable = 0 -> INTERNET -> Javascript variable value changed to 1

You can check all day long, but on the PHP side, that variable is still going to hold it's value. You'll need to send them back in a $_POST or $_GET variable, or use AJAX that works as a middleman.
Was This Post Helpful? 0
  • +
  • -

#3 nicoscode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 08

Re: php code for check box

Posted 07 August 2008 - 09:02 PM

I hate to ask, but can you give me a bit more help? Maybe an example? Here's what I'm thinking you mean.


<?

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['UserEmail'];
$gender = $_POST['UserSex'];
$birthdate = $POST['UserBirthdate'];
 



I don't know what to put right here so that it will email me what they check. Are you saying just put
$_POST "checkbox"


} 

if ($_POST['secure'] == '1978')
{
	$message = "Mailing Information for $firstname<br>
		 Name   $firstname, $lastname<br>
		 Address   $address<br>
		 City, State, Zip   $city, $state $zip<br><br><br>
		 Phone   $_POST[phone]<br>
		 Email   $_POST[UserEmail]<br>
		 Gender   $_POST[UserSex]<br>
		 Birthdate   $_POST[UserBirthdate]<br>";
		 
		 
	echo '<h1>Step 1: complete Registration Sent</h1>
		  <h1>Step 2: <a href="donate2.html" title="Registration Fee Payment">Click here to pay registration fee</a></h1>
	';
	// set up the mail
	$recipient = "*****@gmail.com";
	$subject = "Registration";
	$mailheaders = "MIME-Version: 1.0\r\n"; // line of code for HTML mail
	$mailheaders .= "Content-type: text/html; charset=ISO-8859-1\r\n"; // HTML
	$mailheaders .= "From: webserver@*****.org\n";
	
	// send the mail
	mail($recipient, $subject, $message, $mailheaders);

}
else
{
	echo '<h1>No Code was entered</h1>';
	echo '<a href="mailing.html">Go back and try again</a>';
}
?>




** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: php code for check box

Posted 07 August 2008 - 10:12 PM

Previously provided by MitkOK : Found here
If you have multiple checkboxes :

<form name="myform" action="" method="POST">
<input type="checkbox" name="options[]" value="Mac OS X">Mac OS X
<input type="checkbox" name="options[]" value="Linux">Linux
<input type="checkbox" name="options[]" value="Windows"> Windows
<input type="submit" value="Send me your name!">
</form>

You can get the count using this :

$boxcount = isset($_POST['options']) ? count($_POST['options']) : 0;

echo $boxcount;

Was This Post Helpful? 0
  • +
  • -

#5 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: php code for check box

Posted 08 August 2008 - 01:38 AM

Or alternatively you can collect the data as follows:


<?php

$chkbox = $_POST["checkbox"]; // assuming you have named your checkboxes 'checkbox' you can collect and treat them as an array

if($chkbox <> ''){
		foreach($chkbox as $iid) { 
			$sql = "INSERT INTO mytable (checkboxvalue) VALUES (".(int)$iid.")";
			mysql_query($sql);

// or echo to see what it's doing
// echo $iid."<br />";

		}	
	}

?>



This post has been edited by pemcconnell: 08 August 2008 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: php code for check box

Posted 08 August 2008 - 01:58 AM

On review of the scenario, it doesn't look like you need multiple checkbox values to be returned as only one size can be selected, so here is the code you need in a basic scenario.

<html>
....
<body>
....
<?php

/*I've changed the checkboxes to radio buttons simply because they suit the purpose better. 
All radio button names are the same (not the id's) meaning it's still valid xhtml, and even better, it means you can only select one radio 
button at a time. The size / value of the it that you need returned should always be in the value="" of an element.*/

$shirtsize = $_POST['shirtsize_chk'];

echo $shirtsize;

?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <table>
 <tr>
			  <td>Shirt Size(check one):</td>
			  <td>
				<input type="radio" name="shirtsize_chk" value="adultsmall" />
			  Adult: Small<br />
			  <input type="radio" name="shirtsize_chk" value="adultmedium" />
			  Adult: Medium<br />
			  <input type="radio" name="shirtsize_chk" value="adultlarge" />
			  Adult: Large<br />
			  <input type="radio" name="shirtsize_chk" value="adultxlarge" />
			  Adult: X-Large<br />
			  <input type="radio" name="shirtsize_chk" value="adultxxlarge" />
			  Adult: XX-Large<br />
			  <input type="radio" name="shirtsize_chk" value="youthsmall" />
			  Youth: Small<br />
			  <input type="radio" name="shirtsize_chk" value="youthmedium" />
			  Youth: Medium<br />
			  <input type="radio" name="shirtsize_chk" value="youthlarge" />
			  Youth: Large</td>
			</tr>
</table>
<input type="submit" name="sub_btn" id="sub_btn" value="Send!" />
</form>
....
</body>
</html>


This post has been edited by pemcconnell: 08 August 2008 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1