Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

insert data into textfield using checkbox

Posted on 2009-05-20
4
Medium Priority
?
924 Views
Last Modified: 2013-12-12
Hi guys,

what i have is a textfield which would have a date put in it.  i also have a check box which will represent todays date  <?php date("d/m/y "); ?>

what i would like is if the text field is blank and the checkbox is checked when the submit button is pressed, todays date is submitted to a mysql table.  if the text field is blank and the check box is unchecked, i would like an error to appear.  

any ideas where i can find something like this?

all advice is appreciated

kind regards

jack
0
Comment
Question by:jack-lindsay
[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
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24436552
The following code will get you started.  Let me know what more you need from there!

Note:  No need to pass the current date as the value of your checkbox since you can just compute it when you process the form data.

<input type="checkbox" id="noDate" value="1"/>
<input type="checkbox" id="myDate" />
 
<?php
 
if(isset($_REQUEST['noDate'])) {
   $myDate = date("m/d/y");
} elseif(isset($_REQUEST['myDate']) && $_REQUEST['myDate']<>"") {
   $myDate = $_REQUEST['myDate'];
   //error checking here to make sure
   //  submission is valid date
} else {
   //give them an error message
}
 
//insert $myDate into your database

Open in new window

0
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24436559
Sorry, missed my closing php tag (?>), but you get the idea. :-)
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24436743
First, change your checkbox value to a "constant value" such as "1" for example. We will do the date derivation in the processing page.

I have two strategies for you:
// REPLACE
// $_POST['textdate'] with your textbox variable
// $_POST['checkdate'] with your checkbox variable
 
// strategy 1 - check both values
// only make date today only if checkbox is checked only if textbox is blank
if ($_POST['checkdate'] == "1"){
 if(isset($_POST['textdate'])){
 
 }
 else
 {
  $zdate = date("d/m/y ");
 }
}
else 
{
 if(isset($_POST['textdate'])){
  $zdate = $_POST['textdate'];
 }else{
  // error both inputs are blank
 }
}
 
 
 
 
 
// strategy 2 - overwrite
// overwrite when checkbox is "checked" no matter what is in textbox
if ($_POST['checkdate'] == "1"){
 $zdate = date("d/m/y ");
else {
 if(isset($_POST['textdate'])){
  $zdate = $_POST['textdate'];
 }else{
  // error both inputs are blank
 }
}

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 375 total points
ID: 24436753
Oops use this instead. Don't forget to change your checkbox value to "1"

 
 
// REPLACE
// $_POST['textdate'] with your textbox variable
// $_POST['checkdate'] with your checkbox variable
 
// strategy 1 - check both values
// only make date today only if checkbox is checked only if textbox is blank
if ($_POST['checkdate'] == "1"){
 if(isset($_POST['textdate'])){
  $zdate = $_POST['textdate'];
 }
 else
 {
  $zdate = date("d/m/y ");
 }
}
else 
{
 if(isset($_POST['textdate'])){
  $zdate = $_POST['textdate'];
 }else{
  // error both inputs are blank
 }
}
 
 
 
 
 
// strategy 2 - overwrite
// overwrite when checkbox is "checked" no matter what is in textbox
if ($_POST['checkdate'] == "1"){
 $zdate = date("d/m/y ");
else {
 if(isset($_POST['textdate'])){
  $zdate = $_POST['textdate'];
 }else{
  // error both inputs are blank
 }
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

610 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