1

HTML:

<form action="add.php" method="get" id="form">
  <tr>
    <td>SALE ITEM</td>
    <td>
      <input type="checkbox" name="saleItem" value="n" <?php if(isset($_GET['saleItem'])) { ?> checked="checked" value="y" <?php } ?> />
    </td>
  </tr>
</form>

PHP (add.php)

$sql=("INSERT INTO table (sale) VALUES ('" . $_GET['saleItem'] . "')");
if (!(mysql_query($sql))) { die ("could not query: " . mysql_error()); }

It always inserts an N regardless if it is checked. I would love if it inserted y based on if the checkbox is checked. I also tried to put the value="n" into an else after the if, but it always put N regardless as well (i removed the value="n" previous to the if when I did this), here is what I meant:

<input type="checkbox" name="saleItem" <?php if(isset($_GET['saleItem'])) { ?> checked="checked" value="y" <?php } else { ?> value="n" <?php } ?> />
2
  • The browser will not send the input if it wasn't checked. Commented Mar 16, 2012 at 20:47
  • I want to it to send an N into my database if it is not checked, and a Y if it is checked. Commented Mar 16, 2012 at 20:48

2 Answers 2

2

You would change your html to:

<input type="checkbox" name="saleItem" value="1" <?php echo (isset($_GET['saleItem']) ? 'checked="checked"' : '')?> />

Then change the PHP to:

$is_checked = (isset($_GET['saleItem']) ? 'y' : 'n');

and then your SQL statement should look like:

$sql=("INSERT INTO table (sale) VALUES ('" . $is_checked . "')"); 

If a checkbox is not checked when the form is submitted, the browser doesn't send it's value to the server. So changing it's value to 1 and then checking server-side if it exists at all, is the best way to determine the state of the checkbox.

Sign up to request clarification or add additional context in comments.

2 Comments

And your sql statement to $sql=("INSERT INTO table (sale) VALUES ('" . $is_checked . "')");
Thank you, I added that to my answer.
1

A checkbox can have only one value. Your attempt to change the value attribute depending on whether it is checked or not won't work.

Here's updated HTML for your checkbox:

<input type="checkbox" name="saleItem" value="Y" <?php if( isset( $_GET['saleItem'] ) ){ ?> checked="checked" <?php } ?> />

This will return a "Y" to the server if it is checked, but it will not return at all if it is not checked (this is the way the checkbox element is designed to work). So you need to have your PHP code explicitly check to see if anything was returned:

$saleItemValue = "N";
if( array_key_exists( 'saleItem', $_GET ) && $_GET['saleItem'] == 'Y' ){
    $saleItemValue = "Y";
}

$sql = "INSERT INTO table (sale) VALUES ( '$saleItemValue' )";
if( !mysql_query($sql) ){
    die( "could not query: " . mysql_error() );
}

This way, PHP will place an 'N' in the database if nothing comes back -- i.e. if the checkbox was not checked.

1 Comment

Absolutely perfect, worked. Thank you very much! I understand it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.