Jump to content


Photo

How do you make javascript POST


  • Please log in to reply
23 replies to this topic

#21 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 17 October 2006 - 01:41 PM

Your post got me thinking..

I do something like this for the get and post method for my php scripts so I thought why couldn't it be done with javascript..

I have only tested this is IE6 I am not sure about FF or Opera.

function something(form) {
    var fld = document.forms[form].elements;
    //this will get the total number of fields in the form
    var fmax = fld.length;
    var vals;
    var error = new Array();
    //now we loop thorough the fields.
    for (var i = 0; i < fmax; i++) {
          //this is going to assume that you have a name for the submit button
         if (fld[i].name !== 'submit') {
             if (fld[i].value == '') {
                  error[] = fld[i].name;
             }
             if (i == 0) {
                 vals += fld[i].name + '=' + fld[i].value;
             } else {
                 vals += '&' + fld[i].name + '=' + fld[i].name;
             }
         }
     }
     if (error.length !== 0) {
         var errors;
         for (var i = 0; i < error.length; i++;) {
                errors += error[i] + ', ';
         }
         var val = errors.split('undefinded');
         alert('You did not enter the following fields ' + val);
     } else {
         var param = vals.split('undefined');
         //now we return the second part of the split
         return param[1];
     }
}                

now you can do this in the function that sends the results to the php file.

function yourfunction(form) {
    var params = something(form);
    first.open('POST', 'youscript.php');
    first.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    first.send(params);
    first.onreadystatechange = somethingElse;
}


now you call this function like this onclick="yourfunction(this.form.name);" You could also add the name of the script that you want to send it to by doing this onclick="yourfunction(this.form.name, script);" and adding that to that to the yourfunction  function like this.

function yourfunction(form, script) {
    var params = something(form);
    first.open('POST', script + '.php');
    first.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    first.send(params);
    first.onreadystatechange = somethingElse;
}


You will also have to have a name for the form and a name for the fields.

hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#22 aleX_hill

aleX_hill
  • Members
  • PipPipPip
  • Advanced Member
  • 143 posts

Posted 18 October 2006 - 12:18 AM

Tom, your a legend...

I havent tested it yet, but it looks like that will do exactly what I'm looking for.

Thanks for all your help, and be assure, if it doeant work, I will be back :)

Alex

#23 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 18 October 2006 - 12:37 AM

Np.. I am here to help

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#24 aleX_hill

aleX_hill
  • Members
  • PipPipPip
  • Advanced Member
  • 143 posts

Posted 18 October 2006 - 02:15 PM

I had a couple of problems with the error reporting, especially the error[] = ... line.

After omitting this it worked, with the exception of the use of document.forms[form].

The forms.[form] expects an integer, but seeing as my page has theoretically infinate forms, with a loop to create them, i simply added another argument for the form number, which is the same as the loop variable in my PHP script.

Alex




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users