Jump to content

Taking 3 days off the input date of the user


Recommended Posts

Hi, 
 
Can someone point me in the right direction on how to do this 
 
I have a form (I have condensed it down to stop complicating things)

<input type="text" name="regid"/>
<input type="text" name="controlno"/>
<input type="text" name="hoverdate"/>
<button>My Submit Button</button>

The php function 

public function Addnew($regid, $controlno, $hoverdate)
    {
        try {
            $query = $db->prepare("INSERT INTO addnew(regid, controlno, hoverdate) 
            VALUES (:regid,:controlno,:hoverdate)");
            $query->bindParam("regid", $regid, PDO::PARAM_STR);
            $query->bindParam("controlno", $chassis, PDO::PARAM_STR);
            $query->bindParam("hoverdate", $hoverdate, PDO::PARAM_STR);
	    $query->execute();
            return $db->lastInsertId();
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

And form data

if (!empty($_POST['btnAddNew'])) {
    if ($_POST['regid'] == "") {
        $add_error_message = 'Registration ID is required!';
    } else if ($_POST['controlno'] == "") {
        $add_error_message = 'A control number is required!';
	} else if ($_POST['hoverdate'] == "") {
        $add_error_message = 'When is the product being handed over';			
    } else if ($app->isReg($_POST['regid'])) {
        $add_error_message = 'That Registration ID is already being processed!';
    } else {
   $user_id = $app->Addnew($_POST['regid'], $_POST['controlno'], $_POST['hoverdate']);

What I am trying to do is insert a 'pickingdate' into the database when the user submits the form

The date has to be 3 days prior to the "hoverdate" 

 

I am not sure where to start on this one and been scratching my head for ages trying to think of a solution but cannot for the life of me get my head round it...Any pointers would be much appreciated

 

Thanks in advance for any help

Link to post
Share on other sites

Are you planning on putting the picking date into that same table with the hoverdate? If so, why? You can just

SELECT regid
     , controlno
     , hoverdate
     , hoverdate - INTERVAL 3 DAY as pickingdate
FROM addnew
You can calculate the picking date when required - you shouldn't store derived values.
Link to post
Share on other sites

Hi, 

 

Can someone point me in the right direction on how to do this 

 

I have a form (I have condensed it down to stop complicating things)

<input type="text" name="regid"/>
<input type="text" name="controlno"/>
<input type="text" name="hoverdate"/>
<button>My Submit Button</button>

 

The form is all working and adding to the database, or should I say it was before I tried doing this 

 

I just put an example dummy form up :o) so there wasn't a mass of unneeded html

Link to post
Share on other sites

Right OK this is the full code with forms etc etc

 

The date format I am using is yyyy-dd-mm I have to use this else Bootstraps Date picker fails and doesnt insert date into DB

 

First the complete form

<?php
include "includes/header.php"; 
include "includes/sidebar.php";
$addnew_error_message = '';
$addnew_added_message = '';
if (!empty($_POST['btnAddNew'])) {
    if ($_POST['reg'] == "") {
        $addnew_error_message = 'Registration is required!';
    } else if ($_POST['chassis'] == "") {
        $addnew_error_message = 'Chassis No is required!';
	} else if ($_POST['handover'] == "") {
        $addnew_error_message = 'Handover Date is required!';
	} else if ($_POST['dbright'] == "") {
        $addnew_error_message = 'Handover Date is required!';			
    } else if ($app->isReg($_POST['reg'])) {
        $addnew_error_message = 'That Reg No is being processed!';
    } else {
   $user_id = $app->Addnew($_POST['reg'], $_POST['chassis'], $_POST['handover'], $_POST['pdi'], $_POST['tlplates'], $_POST['aplates'], $_POST['dbright'], $_POST['lshine'], $_POST['smats'], $_POST['lmats'], $_POST['mflaps'], $_POST['other']);
		  
    
    }
	$addnew_added_message = 'The vehicle has been added.';
            }

?>

<div class="page-header">
	<h1>New Car Prep</h1>
</div><!-- /.page-header -->

	<div class="row">
		<div class="col-xs-12">
<div class="col-md-6 col-md-offset-3 well">
            <h4><strong>Add New Car Prep</strong></h4>
			<br>
            <?php
            if ($addnew_error_message != "") {
                echo '<div class="alert alert-danger"><strong>Error: </strong> ' . $addnew_error_message . '</div>';
            }
			else if ($addnew_added_message != "") {
                echo '<div class="alert alert-success" id ><strong>Thank You: </strong> ' . $addnew_added_message . '</div>';
            }
			?>
            <form action="" method="post">
                <div class="form-group">
                    <h4>Reg</h4>
                    <input type="text" name="reg" class="form-control"/>
                </div>
                <div class="form-group">
                    <h4>Chassis (Last Six)</h4>
                    <input type="text" name="chassis" class="form-control"/>
                </div>
				
				<div class="form-group">
				<h4>Handover Date</h4>
				<div class="row">
					<div class="col-xs-8 col-sm-11">
						<div class="input-group">
							<input type="text" class="form-control date-picker" name="handover" id="id-date-picker-1" data-date-format="yyyy-dd-mm" />
							<span class="input-group-addon">
							<i class="fa fa-calendar bigger-110"></i>
							</span>
						</div>
					</div>
				</div>	
				</div>

<br>

<h4><strong>Work Required:-</strong></h4>

<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Carry put PDI</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="pdi" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		
		<label class="btn btn-success">
	        <input name="pdi" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="pdi" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>
	</div>
	
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">T L Plates</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="tlplates" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="tlplates" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="tlplates" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
	</div>	
	</div>
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Agency Plates</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="aplates" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="aplates" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="aplates" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>	
	</div>
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Diamond Bright</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="dbright" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="dbright" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="dbright" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>	
	</div>
<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Lifeshine</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="lshine" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="lshine" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="lshine" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>	
	</div>
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Standard Floor Mats</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="smats" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="smats" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="smats" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>	
	</div>
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Luxury Floor Mats</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="lmats" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="lmats" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="lmats" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
				

		</div>
		
	</div>	
	</div>
	<br>
	<div class="row">
	<div class="col-md-9"><h4 class="spacedout">Mudflaps</h4></div>
	<div class="col-md-3">
	<input type="hidden" name="mflaps" value="Not Required" checked />
		<div class="dlk-radio btn-group">
		<label class="btn btn-success">
	        <input name="mflaps" class="form-control" type="radio" value="Yes">
	        <i class="fa fa-check glyphicon glyphicon-ok"></i>
			</label>
				<label class="btn btn-danger">
				<input name="mflaps" class="form-control" type="radio" value="No">
				<i class="fa fa-close glyphicon glyphicon-remove"></i>
			</label>
			
		</div>
		
	</div>	
	</div>
	<br>
	<div class="form-group">
  <h4>Other:</h4>
  <textarea class="form-control" rows="5" name="other"></textarea>
</div>
	
<div class="form-group">
                    <input type="submit" name="btnAddNew" class="btn btn-primary btn-lg btn-block" value="Add New Vehicle"/>
                </div>

                                  
            </form>
        </div>
        

							
		</div><!-- /.col -->
	</div><!-- /.row -->

Here is the complete function

	public function Addnew($reg, $chassis, $handover, $pdi, $tlplates, $aplates, $dbright, $lshine, $smats, $lmats, $mflaps, $other)
    {
        try {
            $query = $db->prepare("INSERT INTO addnewvehicle(reg, chassis, handover, pdi, tlplates, aplates, dbright, lshine, smats, lmats, mflaps, other) VALUES (:reg,:chassis,:handover, :pdi, :tlplates, :aplates, :dbright, :lshine, :smats, :lmats, :mflaps, :other)");
            $query->bindParam("reg", $reg, PDO::PARAM_STR);
            $query->bindParam("chassis", $chassis, PDO::PARAM_STR);
            $query->bindParam("handover", $handover, PDO::PARAM_STR);
			$query->bindParam("pdi", $pdi, PDO::PARAM_STR);
			$query->bindParam("tlplates", $tlplates, PDO::PARAM_STR);
            $query->bindParam("aplates", $aplates, PDO::PARAM_STR);
            $query->bindParam("dbright", $dbright, PDO::PARAM_STR);
			$query->bindParam("lshine", $lshine, PDO::PARAM_STR);
			$query->bindParam("smats", $smats, PDO::PARAM_STR);
            $query->bindParam("lmats", $lmats, PDO::PARAM_STR);
            $query->bindParam("mflaps", $mflaps, PDO::PARAM_STR);
			$query->bindParam("other", $other, PDO::PARAM_STR);
            $query->execute();
            return $db->lastInsertId();
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

Currently with the above everything is working fine 

 

 

Link to post
Share on other sites

OK - now you have given us the supposed code. My, my, my - you do like divs!

 

Anyway - Where is this 'hoverdate'? I did a search of both blocks of code and don't find it anywhere?

 

Plus - I don't see where you are doing any of the suggestions that you were given to do this task.

Link to post
Share on other sites

i have some recommendations about the way you are implementing this, that will reduce and simplify the code, making it easier to write, test, and debug what you are doing.

 

1) use type='date' for the hoverdate form field. this will make use of the html5 datepicker.

 

2) use meaningful names for form fields, php variables, database columns, function names, ... and use the same name through out for each item. this will allow anyone, now and when looking at the code at some point in the future, to more easily deduce what it is doing. if a name is made up of multiple words, separate them with underscores _

 

3) the form processing code should go above the start of the html document.

 

4) your validation logic is only detecting and reporting the first validation error. if there are multiple errors, the user will have to keep resubmitting the form to see each one. if you use an array to hold the validation error messages, you can easily validate all the input data at once and report all the errors at once. the error array is also an error flag. if the array is empty(), there are no errors. after the end of the validation, you can just test if the array is empty() to decide if you are going to use the submitted data.

 

5) you need to repopulate the form fields with any submitted data, so that the user doesn't need to keep filling in the same information when there are validation errors.

 

6) make your Addnew() and any other functions, general purpose, by passing an array of input data to them, so that you don't need to keep changing the list of call-time parameters in the function definition and in the main code that's calling the function.

 

7) for most database errors, there's no good reason to catch them yourself. you also shouldn't output the raw database errors onto a web site. if you remove the try/catch logic from your code, php will catch the exception and use its error_reporting, display_errors, and log_errors settings to control what happens with the actual error information. when learning, developing, and debugging code, you should display all errors. when on a live/public server, you should log all errors.

 

8) in most cases, there's no good reason to explicitly bind input parameters/values for a prepared query. just supply an array of data to the execute() method call.

 

9) the repeated sections in the form, that just differ in the title/label and field names, should be dynamically produced using php code, so that you don't have to sit banging on a keyboard doing work that you can get the computer to do for you. if you were already doing this, it would make it easier to change the form so that it repopulates the fields with the previously submitted data, because you would only need to add code for each different type of field in the dynamic code, not for each and every field in the form.

 

lastly, i'll call your attention to reply #5 in this thread. there is no good reason to store derived data. if these dates are all calculated from the hand over or other submitted date, don't store them in the database table. you can query at any point using any date/time offset relative to the submitted date, to find matching data.

Link to post
Share on other sites

Sorry, I am not the worlds best at explaining something I am a novice at but I will try and explain it a little better

 

"The Format is yyyy-mm-dd" 

 

I used the same format on the datepicker that Barand gave me of Y-m-d to try and help me and I changed it here(in red) 

 

jQuery(function($) {
 
$('.date-picker').datepicker({
 
startDate: '+10d',
            format: 'yyyy-mm-dd',
            autoclose: true,
})
//show datepicker when clicking on the icon
.next().on(ace.click_event, function(){
$(this).prev().focus();
});
 
But it gave me a bad output so changed it to yyyy-mm-dd so I could get the date into the database without throwing up an error.
 
When the user posts the form I want the other dates calculated and stored in said DB, the user has no control over these dates the only date they can select is the "handover" which as you can see the first date they can select is 10days after todays date
 
So what I need is 
 
date1 = 7days before "handover" date
date2 = 5days before "handover" date
 
and so on down to 1day before "handover" date
 
 
 
Link to post
Share on other sites

 

 

lastly, i'll call your attention to reply #5 in this thread. there is no good reason to store derived data. if these dates are all calculated from the hand over or other submitted date, don't store them in the database table. you can query at any point using any date/time offset relative to the submitted date, to find matching data.

 

Right Ok, Thank you so much for that I will look into validating the form later and the other parts left me scratching my head, going back to the quote above and #5

 

The end result is that I have a calendar and in the calendar I want to put events using the "handover" date as the Primary Date 

 

5 days Prior to handover date I would like to put an event in the calendar for "Vehicle Booked for PDI"

2 Days Prior to handover date I would like to put an event in the calendar for "Vehicle Booked for Valet"

 

So you are saying this would be better using queries and not storing the dates in the DB

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.