Codeigniter using input datepicker cannot post to database - php

I am using a Codeigniter script purchased in Codecanyon. I need add the input datepicker but in Controller cannot get from UI picked date.
UI:
<input type="date" class="form-control datetimepicker" value="<?php echo date('Y-m-d'); ?>" name="deadline" id="deadline">
Controller :
$deadline = date("Y-m-d", strtotime($this->input->post('deadline')));
I have test to show before update database but it's display current time:
var_dump($deadline);
return;

The modal you're dealing with is outside the form. I made it that - using jquery - when the date field is changed, it updates the value of its hidden field which is in the form. So when the modal triggers form submission, the value of its date field is also submitted to the controller.
HTML:
<input type="hidden" name="deadline" id="deadlinehidden" value="" />
JS:
$("#deadline").on("change paste keyup", function() {
$("#pos-sale-form input[name='deadline']").val($(this).val());
});

you can use this:
<input name="deadline" type="text" value="2017-07-08" class="form-control datepicker" id="datepicker">
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
$date=$this->input->post('deadline');

Related

How To attach Automated Events to jquery cloned elements which classes are auto incremented

I am building a itirnary bilder where when i click add day one form appear with three button those three buttons also containe separated attached forms on click, i can clone successfuly add event listner also by .on method but my issue is end user may submit 100 forms using add new button so i need a trick to auto attach events to incremented classes as i incremented classes automatically in my program. (notice that budget accomdation transist buttons are also forms )
i tried to increment the count
and further i want to show search result in budget accomdation transist form and submit the data to mysql.
this all must happen in clean manner is it possible or i must do this in reactjs
https://codepen.io/praveenshivashakti/pen/YzKoaye
<h2>DAy 1</h2>
<div class="job-content">
<form class="form1">
search: <input type="text" name="search"><br>
Last name: <input type="text" name="lname"><br>
First name: <input type="text" name="fname"><br>
Dest name: <input type="text" name="destname"><br>
Agent name: <input type="text" name="agname"><br>
other details: <input type="text" name="ot">
<input type="Button" value="Budget" name="company" class="fClick" />
<input type="button" value="Accomdation" name="course" class="xClick" />
<input type="button" value="Transist" name="date" class="rClick" />
</form>
</div>
<h1>ADD New Day </h1>
$(function () {
var duplicates = 0,
$original = $('.job-content').clone(true,true);
function DuplicateForm () {
var newForm;
duplicates++;
newForm = $original.clone(true).insertBefore($('h1'));
$.each($('input', newForm), function(i, item) {
$(item).attr('class', $(item).attr('class') + duplicates);
});
$('<h2>Day ' + (duplicates + 1) + '</h2>').insertBefore(newForm);
}
$('a[href="add-new-form"]').on('click', function (e) {
e.preventDefault();
DuplicateForm();
});
$(document).on('click', '.fClick', function(){
alert("hey!");
});
$(document).on('click', '.xClick', function(){
alert("hey22!");
});
$(document).on('click', '.rClick', function(){
alert("hey33!");
});
});
I want every button trigger separate event that i can do but i need any automated process to do this like i do not want to sit
$(document).on('click', '.fClick', function(){
alert("hey!");
});
doing like this to every button because i have a single page which generates 100 forms may be before submitting
Hey you can attach the event to all classes beginning with one similar name
$(document).on("click", "[class*='rClick']", function() {
console.log("hey 3");
});

How to pass the jquery datepicker value in form POST method?

I have a input text in the form for date and i use JQuery datepicker to select date.
<input type="text" id="datepicker" name="datepicker" value="Date"/>
when i use form post to post the values into another php for calculation. i don't get the values from this input text alone. i get an empty value instead of selected date.
$date=$_POST['datepicker'];
i'm not sure of how to pass the datepicker value in form POST. Can someone help me out?
jQuery's datepicker has nothing to do with posting your form. If your form is set to "post" then whatever is left in that input field after the user has selected a date will be sent with the form.
<form action="process.php" method="post">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
$date = $_POST['datepicker'];
However if your form is being sent as a "get" request you'll need to access it as such.
<form action="process.php" method="get">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
</form>
Gives you:
$date = $_GET['datepicker'];
$( "#currentDate" ).datepicker("setDate", new Date("<?php echo $_POST['myCurrentDate'] ?>"));
works for me, read at least 10 very complicated ways of doing it and then realised this works. You are instantiating a new value each time. you store the date somewhere in your php scripts, using form input or some other technique and then recycle it back into the above statement. So reloading doesn't affect the result.
If myCurrentDate is unset it will set it by default. Getting it to be set to today's date requires a php if statement to set it the first time (or reload the page) to new Date() and the else statement can be used to include the above code.
That's a simple form with a datepicker
<form id="myform" name="myform">
<input type="text" id="datepicker" name="datepicker" value="Date"/>
<input type="button" id="submitMe">
</form>
Now your jquery,we are going to use some AJAX stuff:
//init datepicker
$(function() {
$("#datepicker").datepicker({
showButtonPanel: true
});
});
//pass deatepicker to php
$("#submitMe").click(function() {
$.ajax({
type: 'POST',
url: "pass.php",
dataType: "json",
data: $('#myform').serialize(),
success: function(data) {
console.log("Done");
}
});
return false;
});​
And finally your pass.php file
<?php
//note that date may have slashes or dots so we url decode it
$date = urldecode ($_GET['datepicker']);
echo "chosen date is: ".$date;
?>

Jquery datepicker date format

How do I get my date to display like this: "yy-mm-dd" inside my date input textbox after I have selected a date?
Information is reading from a database.
So I need it saved in a database again.
JavaScript:
<script>
$(document).ready(function() {
$("#from").datepicker();
});
</script>
Html code:
<div class="field">
<label>Date</label>
<input type="text" name="date" value="<?php value('date'); ?>" id="from" />
<?php if(isset($errors['date'])) { ?>
<div class="error"><?php echo $errors['date']; ?></div>
<?php } ?>
</div>
Things that I have tried:
http://docs.jquery.com/UI/Datepicker#option-dateFormat
http://docs.jquery.com/UI/Datepicker/formatDate
jQuery UI DatePicker - Date Format
jquery ui datepicker date format
Define the format during initialization
$("#from").datepicker({
dateFormat: 'yy-mm-dd'
});
The value is updated as well. See an example.

Update text field border using Jquery and Ajax

I'm trying to build a nice contact form, from where a user can send an e-mail. I have 3 input fields (1. name, 2. e-mail address, 3. telephone number).
<p>
<label for="name"><?php echo $label_name; ?></label>
<input type="text" name="name" id="name" size="30" />
</p>
<p>
<label for="email"><?php echo $label_email; ?></label>
<input type="text" name="email" id="email" size="30" />
</p>
<p>
<label for="tel"><?php echo $label_tel; ?></label>
<input type="text" name="tel" id="tel" size="30" />
</p>
The styling of the fields is made with the following JQUERY script:
$(document).ready(function() {
$('input[type="text"]').addClass("idle");
$('input[type="text"]').focus(function() {
$(this).removeClass("idle").addClass("focus");
this.select();
});
$('input[type="text"]').blur(function() {
$(this).removeClass("focus").addClass("idle");
});
});
Basically what I want is to style the fields with RED border, if the server would return false (using AJAX and PHP). For this, I searched the net and tried to implement a few scripts, but none worked. Here's what I tried:
$(document).ready(function() {
$('input[type="text"]').blur(function() {
$.ajax({url:"php/verify_field.php", success:function(result){
$(this).removeClass("idle").addClass(result);
}});
});
});
Thank you all for answering!
The reply of Omid Amraei seems to suit my project so far. But could we add some spice to all this script: I would like to transmit 2 parameters with the Jquery-Ajax script to the server-side PHP file:
the ID of the text input
the value of the text input.
All this with POST method!
This way I could parse the information in the PHP file using $_POST method and check first the ID, then run the verification criteria on the value of the input.
Many thanks for all!
Happy Saturday :)
Because using this in Ajax callback does not point to your input, so try this :
$(document).ready(function() {
$('input[type="text"]').blur(function() {
var $input = $(this);
$.ajax({url:"php/verify_field.php", success:function(result){
$input.removeClass("idle").addClass(result);
}});
});
});
this in this case isn't the textbox it's the ajax jquery object
Change the context to this.
$('input[type="text"]').blur(function() {
$.ajax({
url:"php/verify_field.php",
context: this
success:function(result){
$(this).removeClass("idle").addClass(result);
}});
});
docs:
contextObject
This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax).

Jquery Validation not preventing form submission

I am using jquery validation 1.9.0. I have used the latest Jquery and on back to 1.6.0. with no change in result
My problem is this: When I deliberately put in the wrong values (not enough characters etc) the validation script rightfully shows the errors yet allows the script to be submitted anyways.
I have tried methods of validation including add rules, rules and the very simple form type below.
<script>
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
</script>
<script>
$(function() {
$("#comm").validate();
});
</script>
the form
<form action="comm.php" method="post" name="comm" id="comm">
Name: <input type="text" name="name" id="name" minlength="3" class="required" />
Email:<input type="email" name="email" id="email" class="required email" /> <br />
<label for="comment"> Comment </label> <br />
<textarea name="comment" id="comment" cols="80" rows="5" minlength="6" class="required" /></textarea>
<?php
date_default_timezone_set('America/New_York');
$date = date("Y-n-j h:i:s");?>
<input type="hidden" name="date" id="date" value="<?php echo $date; ?>" />
<?php $post_id = $id;?>
<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" />
<?php $ip = $_SERVER["REMOTE_ADDR"]; ?>
<input type="hidden" name="ip" id="ip" value="<?php echo $ip; ?>" />
<input type="submit" value="Post" name="post" id="post" /> <input type="reset" value="Reset Form" name="reset" id="reset" />
</form>
Very simple stuff.
Submission happens on all my forms on the net, and all of them on localhost. I can always detect errors but never stop them. What am I doing wrong?
Firebug shows me submitting properly and no script errors. Also all jquerys are connected. Firebug also shows me this After submission novalidate="novalidate" in the form html. using onsubmit=return false does not change anything
I am using ajax to submit the form, works flawlessly
$(function() {
$("#comm").submit(function() {
var data = $('#comm').serialize();
alert (data); return false;
$.ajax({
url: "comm.php",
data: data,
type: "POST",
success: function(msg){
if(msg){
$('.comme').prepend().html(msg).show();
}else{
$('.comme').text("nothing came back");
}
}
});
return false;
});
});
Thank you
Try to do your Ajax handling after clicking the submit button instead of doing with jQuery form submission perhaps you're submitting the form data through Ajax only.
$(function() {
$("#post").click(function() {
var data = $('#comm').serialize();
// TODO: validate your data - $("#comm").validate();
// TODO: submit your form though ajax
// Other operations on form data
return false;
});
});
Note: If the form submission is happening with page redirection then try with 'onsubmit=return false;'.
Update:
I seems you've to submit the FORM from the validate function's submit callback handler(submitHandler) to avoid the redirection after submission. Please try to check this demo example which is working fine with Ajax form submission, review the source code of this example page and then adjust your code accordingly.
var v = jQuery("#form").validate({
submitHandler: function(form) {
jQuery(form).ajaxSubmit({
target: "#result"
});
}
});
If you're usig jquery to receive the form when it is submitted, then then you should specify the php file containing the jquery code as the action of the form, instead of "listening" the submit event. Another option would be letting the 'action' parameter empty. The point here is that you're sending the form twice: when you click summit, the form is automatically sent to the file specified in the 'action' parameter, (that is the submission that is taking place, because it has no validation); and, at the same time when you click submit, it also triggers the ajax request, which will perform the validation and in case of success do the submission again.

Categories