Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to correctly positionate a jQuery code that must to be fired only in case of new records?

Hi Experts

Could you point how to correctly positionate a jQuery code that must to be fired only in case of new records?

Since the datetime column represents when the record is created it must not to be actualized when editions are done.

Coding this way when editing the Data is changing too (it must only bring the date creation value - no changes allowed)

<input value="" type='text'id='data' name='data' class="form-control gui-input br-light light" placeholder="" readonly=""/>
   <script type="text/javascript">

			 jQuery(function() {
					  var d2 = function(n) { return n>9?n:"0"+n;  };
					  setInterval(function() {
						var d = new Date();
						var dd = d2(d.getDate());
						var mm = d2(1+d.getMonth());
						var yyyy = d.getFullYear();
						var hh = d2(d.getHours());
						var mi = d2(d.getMinutes());
						var ss = d2(d.getSeconds());        
						$("#edit #data").val( dd + "-" + mm + "-" + yyyy + " " + hh + ":" + mi + ":" + ss );
					}, 200); 
			 });
	</script>   

Open in new window



Since the same modal is used for insertions and editions -  a jQuery code already exists to diferentiate:

function titulo_modal() {

	if($("#idinterface_users").val() > 0) {

		$(".titulo-mutavel").html("Editar ");
	}
	else if($("#idinterface_users").val() == "") {
		$(".titulo-mutavel").html("Novo ");
	}
	else if($("#id").val() == "") {
		$(".titulo-mutavel").html("Novo  ");
		
		// - The jQuery code must come here to be fired just when a new register is started (?)
		
		// ?????
		
	}
	else {
		$(".titulo-mutavel").html("Editar ");
	}
}

Open in new window


Since the edition button is part of "Datatables plugin" structure, it must to be defined just here to work out....

User generated image
Thanks in advance.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Maybe I am missing something, but if you want to mark the date/time when a new record is added to the database, you don't need jQuery.  Just add a column to the table of type TIMESTAMP.  MySQL will handle everything for you!
https://dev.mysql.com/doc/refman/5.5/en/datetime.html
https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
Avatar of Eduardo Fuerte

ASKER

@Ray

This case is much more a case of datetime presentation. If starting a new record the datetime of server (or even local machine) must be presented. If editing a record the datetime of the record creation must to be presented.

I tryed to use your code from precedent question too:

jQuery code adapted:

function titulo_modal() {

	if($("#idinterface_users").val() > 0) {

		$(".titulo-mutavel").html("Editar ");
	}
	else if($("#idinterface_users").val() == "") {
		$(".titulo-mutavel").html("Novo ");

	}
	else if($("#id").val() == "") {
		$(".titulo-mutavel").html("Novo  ");

		 // --- Precedent question: loads when modal is presented - I guess
		 //$("#data").click(function(){
		   loadDateBox($("#data"));
		//});
		 //-----------------------

	}
	else {
		$(".titulo-mutavel").html("Editar ");
	}
}

//--- Precedent question:
function loadDateBox(element){
	 $.get("temp_eduardo_server.php", function(response){
	 $(element).val(response);
  });

Open in new window

SOLUTION
Avatar of Ray Paseur
Ray Paseur
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland 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
@Ray

What you post is correct but my issue here is to actualize the datetime textbox between insertions  by using server side or browser side functionality - every insertion has its own datetime. A page reload for that purpose is not accepted here.

@Chris

Very logical. I'm searching where is the correct place.
Really

Following your instructions:

This code trigger "New" (Novo)  button:
<div class="col-xs-12 pln">
            <button 
                class="btn btn-primary btn-gradient btn-alt btn-block item-active save_button" 
                id="create" 
                style="width:100%" 
                type="submit">Novo</button>
</div>

Open in new window



And then in jQuery code
 $('#create').click(function () {
	//console.log('Botao Novo');
	loadDateBox($("#data"));
});

 function loadDateBox(element){
	$.get("temp_eduardo_server.php", function(response){
	$(element).val(response);
	});
}

Open in new window


Since I'm using Codeigniter I'm going to put the "temp_eduardo_server.php" code in controller and call it using AJAX then...

So, it runs... changing datetime between insertions without reloads. Accordingly to:

User generated image
User generated image
Just one thing more... this re-configuration of date format apparently has no effects(?!)

<?php 

error_reporting(E_ALL);

//echo date('c');
echo date("d-m-Y H:i:s");

Open in new window


What is missing?
re-configuration of date format apparently has no effects...
Maybe you are testing the wrong script?  When we are working with tiny code fragments it is hard to know what else might be in play.  Suggest you spin each of the issues out into one SSCCE for each of the parts that you want to isolate and test.  Greater isolation will give you better individual test cases.
http://sscce.org/
Thank you for the very good orientations!
Application is perfectly running. Thank you one more time!