LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Form processing with cgi and uclinux (https://www.linuxquestions.org/questions/programming-9/form-processing-with-cgi-and-uclinux-169363/)

GridX 04-12-2004 08:24 PM

Form processing with cgi and uclinux
 
Hi, I read tutorials on cgi.... cant find that much help. I have a question.

I am working with a dragonball mc68k processor running uClinux. I will be installing the BOA webserver on this board. The board will control some lights. Either lights on or lights off. Now I want to put control in a web site so I can connect to the web site anywhere and turn on my house lights via web page. I have the C program written that will control the lights.
I want to do the following very simple form.

Code:

<FORM ACTION="URL">
 <INPUT TYPE="radio" NAME="state" VALUE="Y">LightsON<BR>
 <INPUT TYPE="radio" NAME="state" VALUE="N">LightsOFF<BR>
 <INPUT TYPE="submit" NAME="button" VALUE="Send">
 </FORM>


So when some one selects lights on, it will execute a cgi script that will call a c program that turns on a light.( the web server will be hosted on a NETdimm processor, the processor is connected to a light and the c program controls the light(on/off).

I want to implement this in a web site, so when a user goes to the web site they use the form on the website and select on or off in the form for lights.

So Do I write a shell script that calls the C program? or a shell script that calls a cgi script which inturn calls my C lights program
Code:

#!/bin/sh
eval "`cgi script here?  or my C lights program? $*`"

#what else do I need in this shell script.?

cat - << \END
echo Content-type: text/plain
echo
echo done.

I am confused? I dont know how to write cgi scripts, do I need one to process user input to my very simple form(on/off) or all I need is a shell script that calls my C lights program?

any guidance will be helpfull

aluser 04-12-2004 09:06 PM

Here's an example perl script which handles both the job of printing the web page and running the lightsprogram. I make the assumption that your program is called lightsprogram and accepts -state, -on, and -off switches. I assume the -state switch prints "on" or "off".

This can of course be done through a shell script but perl has the nice CGI library, and I know it better.. hopefully this example is reasonably transparent anyway.

You would put this script in your cgi-bin directory and chmod +x it.

Code:

#!/usr/bin/perl
use strict;
use warnings;
use CGI ();

my $q = CGI->new();
my $current_state;
my $form_state = $q->param('state');
if (not defined $form_state or ($form_state ne 'Y' and $form_state ne 'N')) {
        my $output = `lightsprogram -state`;
        if (substr($output, 0, 2) eq 'on') {
                $current_state = 1;
        } else {
                $current_state = 0;
        }
} elsif ($form_state eq 'Y') {
        system('lightsprogram -on');
        $current_state = 1;
} else {
        system('lightsprogram -off');
        $current_state = 0;
}

my $onoff_word = $current_state ? "ON" : "OFF";
my $on_checked = $current_state ? "checked" : "";
my $off_checked = $current_state ? "" : "checked";
print $q->header, qq(
        <html>
        <head>
        <title>Lights</title>
        </head>
        <body>
        <h1>Lights are now $onoff_word</h1>
        <form action="lights.cgi" method=GET>
          <input type=radio name=state value=y $on_checked />Lights ON<br />
          <input type=radio name=state value=n $off_checked />Lights OFF<br />
          <input type=submit value=Send />
        </form>
        </body>
        </html>
);


GridX 04-12-2004 10:38 PM

Hey thanks for the help, nice job on the script but I cant use perl scripts(sorry), boa doesnt support it, I have to use either unix shell scripts or C programming scripts.


I dont need the results sent back to the web page, all I need is the user imput to activate or call my C program. C lights program

GridX


All times are GMT -5. The time now is 12:58 AM.