Dropdown calculates date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • watdaduck
    New Member
    • Apr 2008
    • 7

    Dropdown calculates date

    Can someone guide me in the right direction? I have a drop down with values of 1 through 14 (1 = 1 day, 2 = 2 days, 3 = 3 days, ect). The onchange the drop down should calculate the days starting from today. If today is 4/1/08 and the user selects 3 then the output should show 4/4/08. However, my problem is that the date is not changing.

    [HTML]<select name="lstPrpTyp eDesc" id="lstPrpTypeD esc" onblur="validat e(this.value, this.id)" onchange="????" >[/HTML]

    [PHP]<?php
    $PostOptions = array("0" => "[Select]",
    "1" => "3",
    "2" => "4",
    "3" => "5",
    "4" => "6",
    "5" => "7",
    "6" => "14");

    $current = time();
    $postdays=$_SES SION['values']['lstPost'];
    $exp_date = time() + (86400 * $postdays);
    $expdate = date('l, m-d-Y H:i:s A', $exp_date);
    echo $expdate;?>[/PHP]
    Code:
    function ajaxtime(val) {var req;try{req = new XMLHttpRequest();} catch (e){try{req = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{req = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){alert("Please Update Your Browser");return false;}}}req.onreadystatechange = function(){if(req.readyState == 4){document.getElementById('response').innerHTML = req.responseText;}}req.open("GET", "time.php", true);req.send(null);}
  • JazzSingerNL
    New Member
    • Apr 2008
    • 4

    #2
    Some small changes made, both to script and PHP file.
    This works for me (IE and FF):

    PHP:

    Code:
    <?php
    $postdays = $_REQUEST['lstPost'];
    $exp_date = time() + (86400 * $postdays);
    $expdate = date('l, m-d-Y H:i:s A', $exp_date);
    echo $expdate;
    ?>
    HTML / JS:

    Code:
    <select name="lstPrpTypeDesc" id="lstPrpTypeDesc" onchange="ajaxtime(this.value)">
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="14">14</option>
    </select>
    
    <div id="response">&nbsp;</div>
    
    <script type="text/javascript" language="JavaScript">
    
      function ajaxtime(val) {
    	  var req;
    	  try{
    		req = new XMLHttpRequest();
    	  } catch (e) {
    		try {
    		  req = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    		  try{
    			req = new ActiveXObject("Microsoft.XMLHTTP");
    		  } catch (e) {
    			alert("Please Update Your Browser");
    			return false;
    		  }
    		}
    	  }
    	  req.onreadystatechange =  function() {
    								  if(req.readyState == 4){
    									document.getElementById('response').innerHTML = req.responseText;
    								  }
    								}
    	  req.open("POST", "time.php?lstPost="+val, true);
    	  req.send(null);
    	}
    
    </script>

    Comment

    • watdaduck
      New Member
      • Apr 2008
      • 7

      #3
      Thanks JazzSingerNL I will give that a shot

      Comment

      • watdaduck
        New Member
        • Apr 2008
        • 7

        #4
        JazzSingerNL it woks like a charm. However, I have 1 more question. How would I store the value of div id="response" into a database?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Make it into a form field and submit the form to a server-side script which saves the data to the database. Alternatively, use a hidden field.

          You could also do this via Ajax or even when making the original date request (if appropriate).

          Comment

          • watdaduck
            New Member
            • Apr 2008
            • 7

            #6
            Originally posted by acoder
            Make it into a form field and submit the form to a server-side script which saves the data to the database. Alternatively, use a hidden field.

            You could also do this via Ajax or even when making the original date request (if appropriate).
            Acoder - I'm not sure what you mean.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Which part do you not understand?

              With your current code, an easy solution would be to set a hidden field to the value of the response, so that it can be submitted.

              Comment

              • watdaduck
                New Member
                • Apr 2008
                • 7

                #8
                Sorry I don't get it?

                [HTML]input name="txtExp" id="txtExp" value="<div id="response"> </div>"[/HTML]

                Comment

                • watdaduck
                  New Member
                  • Apr 2008
                  • 7

                  #9
                  I have the following code but it seems the hidden field is always 1 step behind. How do I go about getting the current value?

                  [HTML]<div id="response">< input type="hidden" name="txtExp" value="<?php echo $_SESSION['txtExp']; ?>"/></div>[/HTML]

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by watdaduck
                    Sorry I don't get it?

                    [HTML]input name="txtExp" id="txtExp" value="<div id="response"> </div>"[/HTML]
                    No, the value should be equal to the content of the div, e.g. its innerHTML, not the div itself.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by watdaduck
                      I have the following code but it seems the hidden field is always 1 step behind. How do I go about getting the current value?
                      [code=javascript]document.getEle mentById("respo nse").innerHTM L[/code] would give the current value of the response (after the Ajax call). When should this be updated in the database?

                      Comment

                      • watdaduck
                        New Member
                        • Apr 2008
                        • 7

                        #12
                        Originally posted by acoder
                        [code=javascript]document.getEle mentById("respo nse").innerHTM L[/code] would give the current value of the response (after the Ajax call). When should this be updated in the database?
                        After the user hits submit. Also, how would I get it to update the database onblur?

                        [HTML] <div id="response">P lug expiration date by selecting an active days. <input type="hidden" name="txtExp" id="txtExp" value="<?php echo $_SESSION['txtExp']; ?>"/> [/HTML]

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by watdaduck
                          After the user hits submit.
                          Then set the hidden field like this:[code=javascript]document.getEle mentById("txtEx p").value = req.responseTex t;[/code]i.e. when you set the div, also set the hidden field value.
                          Originally posted by watdaduck
                          Also, how would I get it to update the database onblur?

                          [HTML] <div id="response">P lug expiration date by selecting an active days. <input type="hidden" name="txtExp" id="txtExp" value="<?php echo $_SESSION['txtExp']; ?>"/> [/HTML]
                          onblur on which element? Rather than onblur, onchange is more reliable. Call the form's submit() method to submit the form.

                          Comment

                          Working...