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

asked on

Could you point a way to make PHP server values to be actualized between modal calls?

Hi Experts

Could you point a way to make PHP server values to be actualized between modal calls?

Considering this modal code:
<!-- Begin: Modal Edit -->
<div id="edit" class="modal fade" tabindex="-1" role="dialog">
	...
	<div class="col-xs-4 mb15">
		<label for="data" class="field-label text-muted mb10">Data</label>
		<label for="data" class="validar field select">                                                     
			<input value="<?php echo date("d-m-Y H:i:s");?>" type='text'id='data' name='data' class="form-control gui-input br-light light" placeholder="" readonly=""/>
		</label>
	</div>

Open in new window


After the data is saved, when a new modal is called, the date is exactly the same....

User generated imageThanks in advance
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

That's because the PHP doesn't run again after a modal.  You would have to reload the page from the server to get new values from PHP.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe 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
To Dave's point, here is the two-part answer.  First, the server-side of the operation that responds with the date in ISO-8601 format.  If you want a different date format, just change the pattern in the date() function call.
<?php // demo/temp_eduardo_server.php
/**
 * https://www.experts-exchange.com/questions/28988982/Could-you-point-a-way-to-make-PHP-server-values-to-be-actualized-between-modal-calls.html
 */
error_reporting(E_ALL);

echo date('c');

Open in new window

Second, here is the client-side of the operation that requests the date and puts it into the datebox div.  The only important part is the JavaScript and the datebox div.
<?php // demo/temp_eduardo_client.php
/**
 * https://www.experts-exchange.com/questions/28988982/Could-you-point-a-way-to-make-PHP-server-values-to-be-actualized-between-modal-calls.html
 */
error_reporting(E_ALL);

// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    loadDateBox($("#datebox"));
});

function loadDateBox(element){
    $.get("temp_eduardo_server.php", function(response){
        $(element).html(response);
    });
}
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<div id="datebox" />

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window

Avatar of Eduardo Fuerte

ASKER

Thank you for replies.

@leakim971
It seens your code produces a "live" date, isn't it?
When the modal data is saved it uses the most recent value?
As has been pointed out already and we've discussed previously, the PHP date is set when you first load the page, so if you set the DATA field using that method it will only change on each page load. What you need to do is refersh that property every time you call your javascript EDIT method (or fire the EDIT event).

You have 2 ways to do this - client side with Javascript (leakim has given you a method for doing that - just wrap it in a function and call it when needed) or server-side - Ray has given you the code for the method you'd need to call along with the server side script you'd need.

Which method you choose is up to you but bear in mind this: the client side code will give you the local time of the PC, whilst the server side code will give you the server's time (maybe localised if that's what you need - think timezones!) but will require a round trip to the server (probably negliible on performance)
@leakim971
It seens your code produces a "live" date, isn't it?
When the modal data is saved it uses the most recent value?

Yes. But you should not use client side trusting its time and date.
PHP, MySQL, and the client browser all observe time independently.  You want to choose the one that most nearly suits your application requirements.
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
Hi

And thank you again.

@Leakim971's solution is very elegant andI'm checking if the client side datetime is sufficient good for our purposes.

@Ray
It's not clear to me in your server side solution what you gave using html element (not a div)

<input value="<?php echo temp_teste_eduardo("d-m-Y H:i:s");?>" type='text'id='data' name='data' class="form-control gui-input br-light light" placeholder="" readonly="">

Open in new window

Hi, Eduardo.  Here's a new take on it.
https://iconoun.com/demo/temp_eduardo_client.php

When the browser initially loads the page, it will call the server-side script for a new date/time value.  Every time you click on the text, it will call the server for another new date/time value.  The new date/time value will be inserted into the value= attribute of the input control with the id="data" attribute.  For convenience of the demonstration, we're using a click event, but it could be anything else that qualifies as a DOM event, such as an onLoad, or a mouseMove or similar things.
<?php // demo/temp_eduardo_client.php
/**
 * https://www.experts-exchange.com/questions/28988982/Could-you-point-a-way-to-make-PHP-server-values-to-be-actualized-between-modal-calls.html
 */
error_reporting(E_ALL);

// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    loadDateBox($("#data"));

    $("#newdate").click(function(){
        loadDateBox($("#data"));
    });
});

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

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<input value="" type="text" id="data" name="data" class="form-control gui-input br-light light" placeholder="" readonly="">
<div id="newdate">Click Here to Update the Input Control Date/Time Value</div>

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window

@Ray


The textbox isn't being actualized when clicked, could you check it?
only a page reload actualizes it
SOLUTION
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
Sorry, @Ray.
Thank you for the replies.

This question has to derivate to another.
No apology needed at all, Eduardo!

You can attach event handlers to all sorts of things - focus, blur, keyPress, keyUp, click, mouseOver, etc.  Several event handlers can trigger activities.  Here's an example that fires the event handler when the mouse passes over a page element.
<?php // demo/temp_eduardo_client.php
/**
 * https://www.experts-exchange.com/questions/28988982/Could-you-point-a-way-to-make-PHP-server-values-to-be-actualized-between-modal-calls.html
 */
error_reporting(E_ALL);

// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
    loadDateBox($("#data"));

    $("#newdate").mousemove(function(){
        loadDateBox($("#data"));
    });

    $("#data").click(function(){
        loadDateBox($("#data"));
    });

});

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

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<input value="" type="text" id="data" name="data" class="form-control gui-input br-light light" placeholder="" readonly="">
<div id="newdate">MouseOver Here to Update the Input Control Date/Time Value</div>

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window