www.webdeveloper.com
Results 1 to 2 of 2

Thread: need help with this little ajax

  1. #1
    Join Date
    Jun 2012
    Posts
    2

    need help with this little ajax

    i have copy and pasted this and it is working perfectly.
    but i need to do this for multiple forms on one page.
    as you can see i tried to do this by using
    <?php echo $form_name; ?>
    but since this value is done within a loop it does not work
    Code:
    <script language="Javascript">
    function xmlhttpPost(strURL) {
        var xmlHttpReq = false;
        var self = this;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatepage(self.xmlHttpReq.responseText);
            }
        }
        self.xmlHttpReq.send(getquerystring());
    }
    
    function getquerystring() {
        var form     = document.forms['f1'];
     
    	var formdata = '';
    for (i = 0; i < <?php echo $form_name; ?>.elements.length; i++) { // loop through all the form elements
      formElem = <?php echo $form_name; ?>.elements[i];
      switch (formElem.type) {
        case 'text':
        case 'hidden':
        case 'password':
        case 'textarea':
          formdata += formElem.name + "=" + escape(formElem.value) + '&';
          break;
        case 'checkbox':
        case 'radio':
          if (formElem.checked===true) {
          formdata += formElem.name + "=" + formElem.value + '&';
          } // end of checked box or selected radio button
          break;
        case 'select-one':
          formdata += formElem.name + "=" + escape(formElem.options[formElem.selectedIndex].value) + '&';
          break; // it is possible to do this more succinctly, but then it does not work with older browsers
        default:
          formdata += formElem.name + "=" + escape(formElem.value) + '&'; // setting a default is all-important
      } // end switch
    } // end for loop
    formdata = formdata.substr(0,(formdata.length -1)); // snip off extra ampersand at the end of the string
        qstr = formdata ;  // NOTE: no '?' before querystring
        return qstr;
    }
    
    function updatepage(str){
        document.getElementById("result").innerHTML = str;
    }
    
    </script>
    and here is my html
    HTML Code:
    <input type="checkbox" name="sendto" value="true" /> Send a copy to my personal email address
    <input value="Respond" type="button" onclick='JavaScript:xmlhttpPost("test.php")'>
    <br/>Once posted can't be edited
    </form>

  2. #2
    Join Date
    Jun 2012
    Posts
    2

    i have made some progress

    i have made some progress and found the breakdown point
    i do not get the value for "form_name"
    here is my button
    HTML Code:
    <input value="Respond" type="button" onclick='JavaScript:xmlhttpPost("test.php","c385982294011B")'>
    and here is my function
    Code:
    function xmlhttpPost(strURL,form_name) {
        var xmlHttpReq = false;
        var self = this; 
    
    
    
        // Mozilla/Safari
    formname=form_name;
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        self.xmlHttpReq.open('POST', strURL, true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() {
            if (self.xmlHttpReq.readyState == 4) {
                updatepage(self.xmlHttpReq.responseText);
            }
        }
    
        self.xmlHttpReq.send(getquerystring(form_name));
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles