javascript - Why C++ CGI form_iterator values not getting in XMLHttpRequest asynchronous Ajax request? -


i wrote sample html page ajax script . have .cgi file in cpp accept values ajax , send message html page . facing problem didn't values in cgi script . code :

html & ajax script :

<html> <head>  <script type = "text/javascript">  var xmlhttp;  if(navigator.appname == "microsoft internet explorer") {          xmlhttp = new activexobject("microsoft.xmlhttp");  } else {         xmlhttp = new xmlhttprequest(); }  function sentdata () {      var name = document.getelementbyid('name').value ;     var postdata;      xmlhttp.open("post", "simplecgi.cgi", true);      postdata = "";     postdata += name;      xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");     xmlhttp.setrequestheader("content-length", postdata.length);      xmlhttp.send(postdata);      xmlhttp.onreadystatechange = function() {     document.getelementbyid('area').innerhtml = xmlhttp.responsetext;         }  } </script>  <h1>simple application</h1>      <form id="newform">         enter name <input onkeyup = "javascript: sentdata()" name ="name" id="name">     </form>  <div id = "area"> </div>  </body> </html>  

here sentdata function textarea value , append postdata , send via xmlhttprequest .

and cpp cgi script :

include <unistd.h> #include <iostream> #include <vector> #include <string>  #include "cgicc/cgicc.h" #include "cgicc/httphtmlheader.h" #include "cgicc/htmlclasses.h"  #include <stdio.h> #include <string.h>  using namespace std; using namespace cgicc;  int main(int argc, char **argv) {  cgicc cgi;    try {        // send http header       cout << httphtmlheader() << endl;        // set html document       cout << html() << head(title("cgi sample")) << endl;       cout << body() << endl;        form_iterator name = cgi.getelement("name");        if( name != cgi.getelements().end()){          cout << "content-type: text/plain\n\n"<< **name << "sucess"<<endl;         cout << body() << html();      }     catch(exception& e) { }  return 0 ;  } 

here want html text area value , sending value sucess message html .

the problem facing didn't form_iterator name = cgi.getelement("name");value . empty ? why ?

but working fine when use

<form id="newform" action="simplecgi.cgi" method="post">             enter name <input onkeyup = "javascript: sentdata()" name ="name" id="name">         </form> 

i don;t want redirect html page .cgi page . using ajax . why error ? suggestions ?

if didn't use <form > text area out form tag . can pass textarea values .cgi , message html ?

application/x-www-form-urlencoded content, definition, has encoded using convention.

your javascript code appears blindly put bare string value in body of post. that's not application/x-www-form-urlencoded format.

the format, summarizing, list of "name=value" pairs, separated ampersands, each value %-encoded in url, hence "urlencoded". javascript code sends value part, , fails %-encode altogether.

fix javascript post-ed content correctly encoded. in general, it's going easier use 1 of freely available javascript libraries work you, jquery, instead of reinventing wheel yourself.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -