dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: pass value javascript variable to ajax variable??

  1. #1
    Join Date
    Mar 2008
    Posts
    2

    pass value javascript variable to ajax variable??

    Code:
    <script>
    var time=1;
    
    function timeHere() {
    time = time + 1;
    finalTime = time / 10;
    
    }
    
    function sayTime() {
    finalTime = time / 10;
    
    }
    </script>
    
    <body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()">
    
    </body>
    I want to get the time i.e. value of finalTime in ajax varable.

    Not using cookie, becoz it gives value on same page but after reloading page am using this to javaScript to php

    How i get this without reloading or refreshing page...
    so question is How to save value of variable in javaScript to ajax variable??

    means save value of finalTime in ajax variable
    How to do this??

  2. #2
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    AJAX is java-script, what do u mean exactly? do u want to pass the "finalTime" to a php ajax request?

    u can simply "get" it in the requested php url like :

    link = 'file.php?value=' + finalTime
    //and then set the href to the value of "link" ...

    another method if u'd like to "post" the value:
    <input type="hidden" value=0 id="someID" />
    //and in the AJAX before the request:
    document.getElementById('someID').value=finalTime
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  3. #3
    Join Date
    Feb 2009
    Posts
    2

    Help posting value from ajax call

    Your recent comment is exactly what I need to figure out. Please take a look.

    I have a form that generates a part number based upon a series of selection boxes.

    http://brinsterinc.com/east/inventory_test.php

    I want to pass the part number as a hidden field in a second form that I am going to add to the page. That form will email the part number along with name, email address, etc.

    How can I access the part number so that it can be passed as an invisible field?

    The code that displays the part number is...


    Code:
    <span name="myspan" id="myspan" style="border:1px solid #000000; padding:2px;"></span>
    and the ajax script is...

    Code:
    <script type="text/javascript" language="javascript">
       var http_request = false;
       function makePOSTRequest(url, parameters) {
          http_request = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             http_request = new XMLHttpRequest();
             if (http_request.overrideMimeType) {
                // set type accordingly to anticipated content type
                //http_request.overrideMimeType('text/xml');
                http_request.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          }
          if (!http_request) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }
         
          http_request.onreadystatechange = alertContents;
          http_request.open('POST', url, true);
          http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          http_request.setRequestHeader("Content-length", parameters.length);
          http_request.setRequestHeader("Connection", "close");
          http_request.send(parameters);
       }
    
       function alertContents() {
          if (http_request.readyState == 4) {
             if (http_request.status == 200) {
                //alert(http_request.responseText);
                result = http_request.responseText;
                document.getElementById('myspan').innerHTML = result;           
             } else {
                alert('There was a problem with the request.');
             }
          }
       }
       
       function get(obj) {
          var poststr = "s=" + document.getElementById("s").value + "&it=" + document.getElementById("it").value + "&id=" + document.getElementById("id").value + "&ot=" + document.getElementById("ot").value + "&od=" +  document.getElementById("od").value + "&l=" + document.getElementById("l").value ;makePOSTRequest('post.php', poststr);
       }
    
    </script>

Similar Threads

  1. Replies: 5
    Last Post: 07-24-2009, 09:25 PM
  2. Replies: 5
    Last Post: 04-17-2008, 01:29 PM
  3. Replies: 0
    Last Post: 08-28-2006, 04:05 AM
  4. Replies: 0
    Last Post: 11-14-2001, 08:02 AM
  5. Pass asp variable to javascript
    By suresh in forum ASP.NET
    Replies: 9
    Last Post: 06-16-2000, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center