Solved

Insert Value of one checkbox into MySQL

Posted on 2009-06-30
4
285 Views
Last Modified: 2013-12-13
Simple and easy for you guys and gals I'm sure.

I have a form. I have a checkbox that if it is checked a hidden <div> is displayed with additional form fields. If the user unchecks it, the <div> hides and the form fields are not accessible.

When I fill in the form and leave the checkbox unchecked, it gives me the error, "
Notice: Undefined index: location_have_address in C:\wamp\www\sha2\members\add_game.php on line 57".

I believe this is because the checkbox doesn't have a value when writing to the DB since it was not checked.

In my DB, I am storing the value of the checkbox as 0 or 1. The datatype in the DB is TINYINT.

This checkbox is not required and if left unchecked, needs to write '0' in the DB. I would rather not use a radio button as the user would have to refresh the page should they click on it by accident.

How can I accomplish this?

The form input code for this is below with the hidden div below it.

BUT the error occurs while setting the POST variables after checking if(isset($_POST['submit'])) {.....}

Any ideas?

iShiva
FORM FIELD CODE          
 
<input name="location_have_address" id="location_have_address" type="checkbox" value="1" onclick="$(this).is(':checked') && $('#checked').slideDown('slow') || $('#checked').slideUp('slow');" /> Check to enter ACTUAL address for your event (this will activate the new 'Get Directions' feature for our visitors. 
          
          <div id="checked" style="display: none; background-color: #f5f5f5;" >
          
          Street Number: <input type="text" name="location_street_number" id="location_street_number" size="10" />
          Street Name: <input type="text" name="location_street_name" id="location_street_name" size="30" />
          City: <input type="text" name="location_city" id="location_city" size="20" />
          State: <input type="text" name="location_state" id="location_state" size="5" />
          Zipcode: <input type="text" name="location_zipcode" id="location_zipcode" size="5" />
           </div>
 
SET VARIABLES CODE
 
	if(isset($_POST['submit'])) {
 
 
     // We are here, means that form was submitted
    $sport_id = $_SESSION['SESS_SPORT_ID'];
    $sport_name = $_SESSION['SESS_SPORT_NAME'];
    $level = mysql_real_escape_string($_POST['level']);
    $gamedate = mysql_real_escape_string($_POST['gamedate']);
    $opponent = mysql_real_escape_string($_POST['opponent']);
    $league_game = mysql_real_escape_string($_POST['league_game']);
    $location = mysql_real_escape_string($_POST['location']);
    $location_name = mysql_real_escape_string($_POST['location_name']);
    $location_have_address = mysql_real_escape_string($_POST['location_have_address']);
    $location_street_number = mysql_real_escape_string($_POST['location_street_number']);
    $location_street_name = mysql_real_escape_string($_POST['location_street_name']);
    $location_city = mysql_real_escape_string($_POST['location_city']);
    $location_state = mysql_real_escape_string($_POST['location_state']);
    $location_zipcode = mysql_real_escape_string($_POST['location_zipcode']);
    $gametime = mysql_real_escape_string($_POST['gametime']);
    $ampm = mysql_real_escape_string($_POST['ampm']);
    $processed = mysql_real_escape_string($_POST['processed']);

Open in new window

0
Comment
Question by:IShiva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:IShiva
ID: 24751833
Also, the php error I am getting says its on line 57. That line in my posted code above is line 28. (location_have_address)
0
 
LVL 5

Accepted Solution

by:
yauhing earned 500 total points
ID: 24752005
You can use the function isset() to check the value is exists or not. If 'location_have_address' not exists in $_POST, just use '0' instead.
$location_have_address = mysql_real_escape_string(isset($_POST['location_have_address']) ? $_POST['location_have_address'] : '0');

Open in new window

0
 
LVL 5

Expert Comment

by:yauhing
ID: 24752037
Are you missing "}" at the end of your code? or can you post the error message?
 if(isset($_POST['submit'])) {

Open in new window

0
 

Author Comment

by:IShiva
ID: 24752096
Hi yauhing

Just tested your code out and it works perfectly! Both states (checked with additional fields and not checked) write to the DB without error now.

Was it not working because the value of that variable was not set yet?

Thanks so much :)

iShiva
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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