Link to home
Start Free TrialLog in
Avatar of jack-lindsay
jack-lindsayFlag for United Kingdom of Great Britain and Northern Ireland

asked on

insert data into textfield using checkbox

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
Avatar of Bobaran98
Bobaran98
Flag of United States of America image

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

Sorry, missed my closing php tag (?>), but you get the idea. :-)
Avatar of NerdsOfTech
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

ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial