Jump to content

if statement for checkbox in forms for insert into db


Unholy Prayer

Recommended Posts

Javascript form validation. It's usually a pretty good idea to follow-up with PHP form validation [code=php:0]if(!$_POST['checkbox']) echo "you didn't check the box!";[/code]on the other side (just incase they don't have javascript turned on).

[url=http://www.phpfreaks.com/quickcode/JavaScript-Simple-Form-Validation-Script/276.php]Javascript form validation tutorial[/url]
Ok, I got it to display if the checkbox is not checked, but it still inserts the info into the database.  This is my code:
[code]<?
include('styles/default/header.tpl');
include('styles/default/navigation.tpl');

?>

<td class="content"><table align="center" cellspacing="1" cellpadding="1" border="0">
<tr>
<form action="hosting.php" method="POST">
<td align="center" colspan="2">Apply for Hosting</td>
</tr><tr>
<td align="right">Your Username:</td>
<td align="left"><input type="text" name="username" size="30">
</tr><tr>
<td align="right">Email Address:</td>
<td align="left"><input type="text" name="email" size="30">
</tr><tr>
<td align="right">Site Name:</td>
<td align="left"><input type="text" name="site_name" size="30"></td>
</tr><tr>
<td align="right">Site Topics:</td>
<td align="left"><input type="text" name="topics" size="30"></td>
</tr><tr>
<td align="center" colspan="2">Do you agree to the <a href="termsofservice.php">Terms of Service</a>?<input type="checkbox" name="tos" value="yes">Yes</td>
</tr><tr>
<td align="center" colspan="2"><input type="submit" value="submit" name="submit"></form></td>
</tr><tr>
<?php

require_once('config.php');
//initilize PHP

if(isset($_POST['submit']))

  {

  $username = $_POST['username'];
  $email = $_POST['email'];
  $site_name = $_POST['site_name'];
  $topics = $_POST['topics'];
  $tos = $_POST['tos'];
 
  MYSQL_QUERY("INSERT INTO hosting_apps (username,email,site_name,topics)".
      "VALUES ('$username', '$email', '$site_name', '$topics')") or die (mysql_error());

  echo "<td align='center' colspan='2'>Thank you for your application.  We will review it and will make a decision within the next couple of days.</td></tr>";

if(empty($tos) || trim($tos) == "")
 
    {
  echo "You must agree to the Terms of Service before you can apply for hosting.  If you have not read them, you can do so by clicking <a href='termsofservice.php'>here</a>.";

          }


}

include('styles/default/footer.tpl');
?>

How do I make it so that the checkbox must be checked in order to insert the info into the database?[/code]
you're just echoing text -- and you're doing it after the mysql query. how about this:

[code]<?php
if($tos) {
MYSQL_QUERY("INSERT INTO hosting_apps (username,email,site_name,topics)".
      "VALUES ('$username', '$email', '$site_name', '$topics')") or die (mysql_error());
} else {
echo "You forgot to accept the terms!";
}
?>[/code]
[code=php:0]if($tos) { //if the checkbox returns a value (checked)
mysql_query // and everything else you want to happen if they check the box
} else { //if the checkbox does not return a value (not checked)
echo "you forgot to check the box"; //and anything else the scipt should do if the box is not checked
}[/code]
Where is the thankyou message kept?

What you are trying to do is this... Im not going to give you the whole code, just the steps you want to take:

[code]if ($tos) // Box is checked, all good
{
    do the mysql command
    show the thank you message
    whatever else you want to do ONLY IF THE BOX IS CHECKED
} else
{ // Box not checked... bad person... BAD!
    Show message saying they didn't check the box, please press back... or whatever
}

See how the thank you message is only shown IF the check the box...
 
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.