[PHP] How to add a button that updates the content of a text box

by 7 replies
9
How can I add a button that updates the content of a text box?

I have a text box that fetch it's content from a database. If I want to modify it's values, all I have to do is to modify the content of the textbox manually and press the "save" button. It's a textbox that contains date value.

I want that improved. Instead of modifying the value or the date manually, I wanted to just add a button or a link that when pressed, automatically updates the contect of the textbox.

Here's the current code of the text box:
Code:
<tr>
	<td width="24%" valign="top" class="form_title" >Date Confirmed Working:</td>
	<td width="76%" valign="top"><input type="text" name="DateTested_Setting_" size="10" maxlength="10" class="form_text" value="<?php echo $form_data["DateTested"] ?>" /> Format: YYYY-MM-DD
		<img src="images/icons/help.png" onclick="showHelp(this)" style="cursor:pointer" />							
		<div class="div_help"> Date in which the coupon is last tested and confirmed working. </div></td>
</tr>
How can I insert a button or a link that updates the content of the text box? In which case, when it's pressed, update date value of the text box to the current date.
#programming #add #button #php #updates
  • Here ya go:

    <a href="#" onclick="javascriptfunction(){document.getElemen tsByName('DateTested_Setting_')[0].value = (new Date()).toISOString().split('T')[0];})()">Click Me</a>

    Cheers,
    Michael
    • [1] reply
    • I tested this and it doesn't work. Instead, it will reload the page and add "#" at the end of the URL.

      I tried
      Code:
      <a href="javascript:void(0);" onclick="javascriptfunction(){document.getElemen tsByName('DateTested_Setting_')[0].value = <?php echo date( "Y-m-d" ) ?>">Click Me</a>
      I replace the assignment value to <?php echo date( "Y-m-d" ) ?> hoping it will assign or replace it with current date but still doesn't work, and href to javascript:void(0);

      Im hoping if there's a simpler way to just assign the value of <?php echo date( "Y-m-d" ) ?> to the text box.
  • Complete code of the php file can be find here:
    https://gist.github.com/anonymous/0ff6b49e01a890fc8cc0

    Line 173
  • Ahh - it looks as though a space snuck in there somehow in the getElementsByName function name - remove that and you'll be fine. I've tested the code and it works as expected..

    Cheers,
    Michael
    • [ 1 ] Thanks
  • Thanks. It's now working.
  • It looks like your code returns a date value of UTC date, not my local date or timezone.
    I made a few modification. I made a function on my header file:
    Code:
    	function update_to_current_date() {
    		var today = new Date();
    		var dd = today.getDate();
    		var mm = today.getMonth()+1; 
    		var yyyy = today.getFullYear();
    
    		if(dd<10) {
    			dd='0'+dd;
    		} 
    
    		if(mm<10) {
    			mm='0'+mm;
    		} 
    
    		today = yyyy+'-'+mm+'-'+dd;
    		return today;
    	}
    And modify your code to
    Code:
    <a href="#" onclick="javascript:(function(){document.getElementsByName('DateTested_Setting_')[0].value = update_to_current_date()})()">Update To Current Date</a>
    So far, it works.

    Thanks again emptee, for a new lesson learned from you.
    I appreciate it.
    • [ 1 ] Thanks
    • [1] reply
    • Good catch mate

      It can be tricky figuring out the best "date" to use when going between browser <-> server if you aren't dealing with the time zone of the browser - I usually end up just using +0 (ISO) so I don't need to worry about time zones everywhere

      If everyone using the site your working on is in the same time zone you should be fine though!

      Cheers,
      Michael
      • [ 1 ] Thanks

Next Topics on Trending Feed