Main Site Documentation

How can I write value of parameter from webpage to EMX?

@ Architect -

thank you for your example 247. I have read it long time ago. I don’t know how to explain my confuse…

I haven’t lerned much what I need from the example 247. It seems that I can’t understand it. sorry… I need the real html file about writting txt file.

My webserve is actually from WouterH’s example 186 (linke: http://www.tinyclr.com/codeshare/entry/186).

I add event handler for every files (html, CSS, Js, txt…).

I have tried a simple example about writting something to a txt file, but it doesn’t work after loading the project to EMX


<HTML>
<HEAD >

<SCRIPT type ="text/jscript" language="JavaScript">

    function WriteFile() {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var fh = fso.open("parameter_file.txt", true);
        fh.WriteLine("Some text goes here...");
        fh.Close();
    }

</SCRIPT>
</HEAD>

<BODY>
<P>
<SCRIPT type ="text/jscript" language="JavaScript">    WriteFile(); </SCRIPT>
</P>
</BODY>
</HTML>

The first parameter of the follow function need a file path, I just give it my txt file, which saved in the resources folder.


 var fh = fso.open"parameter_file.txt", true);

Is there some similar example for my webserver?

That javascript is running on the CLIENT computer. So any file it creates will be on the CLIENT computer not on your machine.

Here’s the basics of how a form (using POST) works.

You create a HTML form entry page, that has a submit button and points to a different page.

User browses to that page; that’s a GET request type.
The user enters info into the form and hits submit.

The different page is requested, but as a POST, and the parameters are visible in the request.

Now, here’s what you need to do. You don’t need to process the .txt file type, so you don’t need an event handler. You need to capture the POST request, and because it’s a POST request handle it differently and then create a text file within the code that handles that.

@ workhard10 -

Have a look on my codeshare that gives a complete example project

http://www.tinyclr.com/codeshare/entry/676

Thank you Brett!!!

which is “That javascript …”?

which basics is “Here’s the basics of how…”?

which is my need?

@ LouisCpro -

Thank you for codeshare!!

I am reading it.

your post #27

Those steps. I’ve added the *'s in front so they look like bullet points better.

Sorry, you will need to read that and what your original code does, and interpret. @ Louis’s code should help you

Thank you for your help!! After reading postes I am stil confused… sorry…

I want to repeat my destination.

I want to use the content of form in a thread loop of the cs file. but I don’t know how to get the form-content from html file to cs file.

After clicking the “send” buttom, how can I get the content of form (for example 123.4), and the value 123.4 can be used in a function “Thread_Task”. (see picture)

You have to send POST or GET request from your page using javascript or using forms, back to your server.
After that you will be able to use the data in any thread you want.

@ Architect -

I am trying the example http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml

I seems that what I want for getting back to my server. but I have sever questions for this example.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Get Test</title>
<script type="text/javascript">

    function ajaxRequest()
            {
              var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
              if (window.ActiveXObject)
                 { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
                     for (var i = 0; i < activexmodes.length; i++)
                     {
                         try
                          {
                           return new ActiveXObject(activexmodes[i])
                          }
                       catch (e)
                           {
                             //suppress error
                           }
                      }
                  }
             else if (window.XMLHttpRequest) // if Mozilla, Safari etc
             return new XMLHttpRequest()
             else
             return false
          }

    var mygetrequest=new ajaxRequest();
    mygetrequest.onreadystatechange = ajaxget()
    {
        if (mygetrequest.readyState == 4)
          {
              if (mygetrequest.status == 200 || window.location.href.indexOf("http") == -1)
                  {
                     document.getElementById("result").innerHTML=mygetrequest.responseText
                  }
             else
              {
                   alert("An error has occured making the request")
              }
          }
    }
    var namevalue=encodeURIComponent(document.getElementById("name").value)
    var agevalue=encodeURIComponent(document.getElementById("age").value)
    mygetrequest.open("GET", "basicform.php?name="+namevalue+"&age="+agevalue, true)
    mygetrequest.send(null)
</script>
</head>

<body>
<form method="get" action="">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your age: <input type="text" id="age" name="age" size="25" /> <br />
<input type="button" value="submit" onClick="ajaxget()" />
</form>

<div id="result"> </div>
</body>
</html>


I can run the webpage in my webserver. A “basicform.php” file is used in this example, but I can’t use php file in my webserver.

Because I don’t have php file, After clicking buttom “submit”, I can’t see the “result”. but I don’t care the result, I want to know how I can write the second parameter
for the function:


mygetrequest.open("GET", ???????????????????????, true)

So that I can get the value of name and age from form to my cs file for my thread function to be used? Please give me some help…

You are getting closer.

mygetrequest.open("GET", "basicform.php?name="+namevalue+"&age="+agevalue, true)

the “basciform.php” can be anything you want. You just need to add handler for that request on you server now and the request should have a the name and the age request parameters in one form or another. You can always get the raw query string in that handler and parse the values yourself as well.

I’m not very clear about what you said with quote.

I have a txt file and its name is “parameter_file.txt”. Can I write like this as follow?


mygetrequest.open("GET", "parameter_file.txt?name="+namevalue+"&age="+agevalue, true)

I have added handler for that request on my server:


            using (HttpServer server = new HttpServer())
            {
                server.AddEvent("/parameter", new HttpServer.EventCallback(Parameter_Field));
                server.AddEvent("/parameter_file.txt", new HttpServer.EventCallback(Parameter_file));
                                     
                while (true)
                {
                    // Sleep for 500 milliseconds
                    Thread.Sleep(500);
                   
                }

            } 

        private static void Parameter_Field(object sender, HttpServer.EventCallbackEventArgs e)
        {
            e.Response = Resources.GetString(Resources.StringResources.Parameter_Field);
            e.ResponseContentType = "text/html";
        }

        private static void Parameter_file(object sender, HttpServer.EventCallbackEventArgs e)
        {
            e.Response = Resources.GetString(Resources.StringResources.parameter_file);
            e.ResponseContentType = "text/html";
        }

what does mean " the request should have a the name and the age request parameters in one form or another."? I don’t understand it, can you explain it for me clearly again?

Thank you !

You should just ignore your text file and the PHP file. Your request should be something like

mygetrequest.open("GET", "johnsmith.htm?name="+namevalue+"&age="+agevalue, true)

Then in your code on the Fez you just have to have a handler that deals with johnsmith.htm and extracts the parameters - then the parameters can be written to your text file

I have tried the 2 possible “GET” and “POST” requests using Ajax from the online example http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml , but not successfull, I don’t understand how can I get the “name” and “value”.

I used the html file “johnsmith.html” as URL in this example.


"johnsmith.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>johnsmith webpage</title>
	</head>
	
<body>

    <?php $name=htmlspecialchars($_GET['name']); 
          $name=stripslashes($name);
          $age=(int)$_GET['age'];
          echo "<span style='color:red'>Welcome <b>$name</b> to JavaScript Kit. So you're <b>$age</b> years old eh?</span>";
    ?>


	
</body>
</html>


I have added the handler for all files which I used.


            using (HttpServer server = new HttpServer())
            {

                server.AddEvent("/parameter", new HttpServer.EventCallback(Parameter_Field));
                server.AddEvent("/johnsmith.html", new HttpServer.EventCallback(Johnsmith));

                while (true)
                {
                    // Sleep for 500 milliseconds
                    Thread.Sleep(500);
                   
                }

            } 

        private static void Parameter_Field(object sender, HttpServer.EventCallbackEventArgs e)
        {
            e.Response = Resources.GetString(Resources.StringResources.Parameter_Field);
            e.ResponseContentType = "text/html";
        }

        private static void Johnsmith(object sender, HttpServer.EventCallbackEventArgs e)
        {
            e.Response = Resources.GetString(Resources.StringResources.johnsmith);
            e.ResponseContentType = "text/html";
        }

the code of GET requests using Ajax is as follow: and the result is in picture 1


GET

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Get Test</title>
<script type="text/javascript">

    function ajaxRequest()
     {
        var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
        if (window.ActiveXObject)
         { 
            //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
             for (var i = 0; i < activexmodes.length; i++)
             {
                 try 
                 {
                    return new ActiveXObject(activexmodes[i])
                 }
                 catch (e)
                 {
                    //suppress error
                 }
             }
          }
        else if (window.XMLHttpRequest) // if Mozilla, Safari etc
        return new XMLHttpRequest()
        else
        return false
     }

</script>
</head>

<body>

<script type="text/javascript">
    


   function ajaxget() 
    {
        var mygetrequest = new ajaxRequest()

        mygetrequest.onreadystatechange = function ()
        {
            if (mygetrequest.readyState == 4)
            {
                if (mygetrequest.status == 200 || window.location.href.indexOf("http") == -1)
                {
                  document.getElementById("result").innerHTML=mygetrequest.responseText
                }
               else
                {
                  alert("An error has occured making the request")
                }
             }
        }
        var namevalue=encodeURIComponent(document.getElementById("name").value)
        var agevalue=encodeURIComponent(document.getElementById("age").value)
        mygetrequest.open("GET", "johnsmith.html?name=" + namevalue + "&age=" + agevalue, true)
        mygetrequest.send(null)

    }
</script>

<form method="get" action="">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your age:  <input type="text" id="age"  name="age"  size="25" /> <br />
<input type="button" value="submit" onClick="ajaxget()" />
</form>

<div id="result"> </div>
</body>
</html>

the code of POST requests using Ajax is as follow: and the result is in picture 2


POST 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Get Test</title>
<script type="text/javascript">

    function ajaxRequest()
     {
        var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
        if (window.ActiveXObject)
         { 
            //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
             for (var i = 0; i < activexmodes.length; i++)
             {
                 try 
                 {
                    return new ActiveXObject(activexmodes[i])
                 }
                 catch (e)
                 {
                    //suppress error
                 }
             }
          }
        else if (window.XMLHttpRequest) // if Mozilla, Safari etc
        return new XMLHttpRequest()
        else
        return false
     }

</script>
</head>

<body>

<script type="text/javascript">
    
    function ajaxget() 
    {
        var mygetrequest = new ajaxRequest()

        mygetrequest.onreadystatechange = function ()
        {
            if (mygetrequest.readyState == 4)
            {
                if (mygetrequest.status == 200 || window.location.href.indexOf("http") == -1)
                {
                  document.getElementById("result").innerHTML=mygetrequest.responseText
                }
               else
                {
                  alert("An error has occured making the request")
                }
             }
        }
      var namevalue = encodeURIComponent(document.getElementById("name").value)
      var agevalue = encodeURIComponent(document.getElementById("age").value)
      var parameters = "name=" + namevalue + "&age=" + agevalue
      mypostrequest.open("POST", "johnsmith.html", true)
      mypostrequest.send(parameters)

    }
</script>

<form method="post" action="">
Your name: <input type="text" id="name" name="name" size="25" /> <br />
Your age:  <input type="text" id="age"  name="age"  size="25" /> <br />
<input type="button" value="submit" onClick="ajaxget()" />
</form>

<div id="result"> </div>
</body>
</html>

I nedd some help, how can I get the value of “name” and “age” ? I want to use the value from form for my Threadloop in C# file.

@ Brett -

sorry Brett, I haven’t solved my problem…

In your:

private static void Parameter_file(object sender, HttpServer.EventCallbackEventArgs e)
{
...
}

You don’t need to serve back the actual html page. This is the place where you need to “dig out” the query string values. Check e parameter what does it have for you there?

@ workhard10 -

Before trying the most complicated as aver : AJAX / JS / PHP, why not to start with a pure Html page, and se how it does manag GE and POST ?

Do it step by step :

  1. Pure html without any ajax or JS script ! Just a field, and a submit !
  2. Add some JS if you need, but as far as I understand what you need to achieve, I’m not sure it will make sens !

If you have your HTTPServer implemented on the EMX, you’ll see that when you open your browser and type the Website address, the HTTPServer will receive a GET, which is always the case when opening a webpage at first time !
Then, when you click on SUBMIT, your HttpServer will receive a POST, which is always the case when you submit some fields on contained in an html !

You’ll probably see that even without any servside code, you can retrieve anything you want in a pure html page, where the only thing important is that you have a FORM, a SUBMIT, and one or more fields…

@ Architect -

I checked the definition of HttpServer.EventCallbackEventArgs e


 public class EventCallbackEventArgs 
        {
            /// <summary>
            /// The requested URL.
            /// </summary>
            public string Url;

            /// <summary>
            /// Key-value-pair table with HTTP GET variables.
            /// Key and value are both of the type string.
            /// </summary>
            public Hashtable Get;

            /// <summary>
            /// Key-value-pair table with HTTP POST variables.
            /// Key is of the type string.
            /// Value is of the type string in case of a normal POST.
            /// Value is of the type MultiPartPostVariable in case of a multipart POST.
            /// </summary>
            public Hashtable Post;

            /// <summary>
            /// The string response to send.
            /// Set by event handler (in case of a string response).
            /// </summary>
            /// <remarks>If you both set Response and BinaryResponse in one event, only Response will be sent.</remarks>
            public string Response = null;

            /// <summary>
            /// The binary response to send.
            /// Set by event handler (in case of a binary response).
            /// </summary>
            /// <remarks>If you both set Response and BinaryResponse in one event, only Response will be sent.</remarks>
            public byte[] BinaryResponse = null;

            /// <summary>
            /// The content type of the response.
            /// Set by the event handler.
            /// </summary>
            public string ResponseContentType = null;

            /// <summary>
            /// Creates a new instance of the EventCallbackEventArgs class.
            /// </summary>
            /// <param name="url">The URL to link with the event.</param>
            /// <param name="get">The HTTP GET variables linked with the event.</param>
            /// <param name="post">The HTTP POST variables linked with the event.</param>
            public EventCallbackEventArgs(string url, Hashtable get, Hashtable post)
            {
                Url = url;
                Get = get;
                Post = post;
            }

            public HtmlReader ResponseHtml;
           
        }

The Get hashtable will have what you need if you are using GET. Post for POST requests.

@ Architect -

Do you mean these:


            /// <summary>
            /// Key-value-pair table with HTTP GET variables.
            /// Key and value are both of the type string.
            /// </summary>
            public Hashtable Get;
 
            /// <summary>
            /// Key-value-pair table with HTTP POST variables.
            /// Key is of the type string.
            /// Value is of the type string in case of a normal POST.
            /// Value is of the type MultiPartPostVariable in case of a multipart POST.
            /// </summary>
            public Hashtable Post;

I can get the content of form something like that:


            string name1  = e.Get.Values.ToString();
            string name2 = e.Post.Values.ToString();

Check documentation on Hashtable how to access what inside of it.