Results 1 to 6 of 6

Thread: need help with showing previous address fields

  1. #1
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default need help with showing previous address fields

    Hi

    I have developed a finance site but the form needs altering, I need the form to display previous address fields if their current address is less than 3 years

    I have put together some coding but don't seem to be working

    Code:
    <div class="row-single">
    	<label>Moving In Date</label>
         <select name="moveindate1day">
      <option value="">---Select date---</option>
      <?php for ($i = 1; $i <= 31; $i++) : ?>
      <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
         
         <select name="moveindate1month">
      <option value="">---Select month---</option>
      <?php for ($i = 1; $i <= 12; $i++) : ?>
      <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
         
        <select name="moveindate1year" id="year1" onchange = "three()">
      <option value="">---Select year---</option>
      <?php for ($i = 1930; $i < date('Y'); $i++) : ?>
      <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
    
         </div>
    
    <div id="extra" name="extra" style="display: none">
         <div class="row-single">
         <label>Address 2</label>
         </div>
         
         <div class="clearfix"></div>
         
         <div class="row-double">
         <div class="leftcol">
         <label>Flat</label>
         <input name="flat2" type="email" value="" maxlength="50" />
         </div>
         <div class="leftcol">
         <label>Property Name</label>
         <input name="propertyname2" type="email" value="" maxlength="50" />
         </div>
         </div>
         
         <div class="clearfix"></div>
         
         <br />
         
         <div class="row-double">
         <div class="leftcol">
         <label>Property Number</label>
         <input name="propertynumber2" type="email" value="" maxlength="50" />
         </div>
         <div class="leftcol">
         <label>Street Name</label>
         <input name="streetname2" type="email" value="" maxlength="50" />
         </div>
         </div>
         
         <div class="clearfix"></div>
         
         <br />
         
         <div class="row-double">
         <div class="leftcol">
         <label>Town</label>
         <input name="town2" type="email" value="" maxlength="50" />
         </div>
         <div class="leftcol">
         <label>Postcode</label>
         <input name="postcode2" type="email" value="" maxlength="50" />
         </div>
         </div>
         
         <div class="clearfix"></div>
         
         <br />
         
         <div class="row-single">
         <label>Moving In Date</label>
         <select name="moveindate2day">
      <option value="">---Select date---</option>
      <?php for ($i = 1; $i <= 31; $i++) : ?>
      <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
         
         <select name="moveindate2month">
      <option value="">---Select month---</option>
      <?php for ($i = 1; $i <= 12; $i++) : ?>
      <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
         
    	<select name="moveindate2year">
      <option value="">---Select year---</option>
      <?php for ($i = 1930; $i < date('Y'); $i++) : ?>
      <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
      <?php endfor; ?>
    </select>
         </div>
         </div>
    
    <script>    
    function three() {
    var yr = new Date().getFullYear();  // current year
    var dv = Number(document.getElementById("year1").value);
    if ((dv == yr) || dv == yr-1) || dv == yr-2) || dv = yr-3)) {
    document.getElementById("extra").style.display = "block";
    }
    else {
    document.getElementById("extra").style.display = "none";
    }
    }
    </script>
    Also be good to use jquery datepicker but I couldn't get that to work for some reason, it showed the calendar but did not seem to have any css styling to it and would not enter the date into the input text field so wondering if it was a jquery issue somewhere, I left that for now and gone with select dropdowns if someone could take a look at my coding to help me get it working first then look at the datapicker

    Thank you in advance

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Syntax errors/typos of omission (add the 3 red highlighted ( characters):

    Code:
    <script>    
    function three() {
    var yr = new Date().getFullYear();  // current year
    var dv = Number(document.getElementById("year1").value);
    if ((dv == yr) || (dv == yr-1) || (dv == yr-2) || (dv = yr-3)) {
    document.getElementById("extra").style.display = "block";
    }
    else {
    document.getElementById("extra").style.display = "none";
    }
    }
    </script>
    Once that's taken care of, you may well be able to use other scripts (like jq date-picker), but there could also be other problems. Also, this is not a form, though that might only be because you haven't shown the full code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    Thank you so much, is working, I will try and integrate the datepicker now and see if that works

  4. #4
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    Sorry back to this issue, I have got the datepicker working now bit if I select the year as 2005 the second address fields show, is there a way that they don't show if the first address year is more than 3 years from current date, I only want the second address fields to show if the first address year is within 3 years of the current date, below is the updated code

    Code:
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      
      <script>
      $(function() {
        $("#midone").datepicker({
    	changeMonth: true,
        changeYear: true,
    	showButtonPanel: true,
    	dateFormat: "yy/mm/dd",
    	showOn:"both"
    	});
    });
      </script>
    
    <form action="unsecuredformsubmit.php" method="post" class="loan-form">
    <input type="text" name="midone" id="midone" onchange = "three()" />
    </form>
    
    <script>    
    function three() {
    var yr = new Date().getFullYear();  // current year
    var dv = Number(document.getElementById("midone").value);
    if ((dv == yr) || (dv == yr-1) || (dv == yr-2) || (dv = yr-3)) {
    document.getElementById("extra").style.display = "block";
    }
    else {
    document.getElementById("extra").style.display = "none";
    }
    }
    </script>

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Two problems:

    1)
    Code:
    var dv = Number(document.getElementById("midone").value);
    no longer gets the year, because that field is now in the format: yyyy/mm/dd. As long as it remains that way, you could use:

    Code:
    var dv = parseInt(document.getElementById("midone").value);
    or any other method that isolates the year and converts it to a number. If the format changes though, a regular expression might be needed or at least a substring() method employed to extract the year portion, and that would then need to be type converted to a number.


    2) In this line:

    Code:
    if ((dv == yr) || (dv == yr-1) || (dv == yr-2) || (dv = yr-3)) {
    the last condition is an assignment, not a comparison, because it has only one = sign. Because of that it will always be true as long as yr-3 is something that evaluates to true, like any positive number, any non-empty string, almost any object, etc. It needs another = sign:

    Code:
    if ((dv == yr) || (dv == yr-1) || (dv == yr-2) || (dv == yr-3)) {
    The whole line could be simplified to:

    Code:
    if (yr - dv < 3) {


    However, taking into consideration that the format could change, but assuming the year (if there at all) will always be the only 4 digit string in the value, I would go with something more like so:

    Code:
    <script>    
    function three() {
    var yr = new Date().getFullYear();  // current year
    var dv = document.getElementById("midone").value.match(/\d{4}/);
    if (dv && dv[0] && dv[0].length == 4 && yr - +dv[0] < 3) {
    document.getElementById("extra").style.display = "block";
    }
    else {
    document.getElementById("extra").style.display = "none";
    }
    }
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    May 2012
    Posts
    216
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi jscheuer1

    Thank you so much for your help, I used the following coding of yours and works perfect, thank you so much

    Code:
        <script>    
    function three() {
    var yr = new Date().getFullYear();  // current year
    var dv = document.getElementById("midone").value.match(/\d{4}/);
    if (dv && dv[0] && dv[0].length == 4 && yr - +dv[0] < 3) {
    document.getElementById("extra").style.display = "block";
    }
    else {
    document.getElementById("extra").style.display = "none";
    }
    }
    </script>

Similar Threads

  1. Dynamic fields not showing
    By dgotti007 in forum PHP
    Replies: 1
    Last Post: 12-30-2010, 01:56 AM
  2. Required fields based on other fields
    By junestag in forum JavaScript
    Replies: 0
    Last Post: 06-03-2008, 07:17 PM
  3. Req fields script limited to 6 fields?
    By dgarzoli in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 01-13-2008, 04:10 AM
  4. Fields are white; refresh = fields are yellow
    By mototrance in forum Other
    Replies: 3
    Last Post: 05-17-2007, 09:16 PM
  5. Adding 2 Fields Multiply 3 Fields
    By NXArmada in forum JavaScript
    Replies: 6
    Last Post: 04-12-2006, 12:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •