嵌入式CGI开发之旅——7

时间:2022-10-20 21:07:59

实验二、提交按钮


我们在上一个实验的页面中增加两个按钮,一个为提交按钮,一个为重置按钮,用DW画在页面上,部分代码如下:


<body>


<p>Hello To My CGIC Test! </p>


<form id="form1" name="form1" method="post" action="/cgi-bin/mycgictest.cgi">


  <p>


    <label for="user_name">Your Name </label>


    <input type="text" name="user_name" id="user_name" />


  </p>


  <p>


    <label for="name_submit"></label>


    <input type="submit" name="name_submit" id="name_submit" value="提交" />


    <label for="name_abord"></label>


    <input type="reset" name="name_abord" id="name_abord" value="重置" />


  </p>


</form>


</body>


修改mycgictest.c文件:


#include "cgic.h"


#define USER_NAME_MAX_BYTE 51


 


int hand_user_name(char *formname,char *username,int name_maxlength);


//处理用户名的函数


int cgiMain(){


  char user_name_result[USER_NAME_MAX_BYTE-1];


  int hand_result=-3;


  cgiHeaderContentType("text/html");


  fprintf(cgiOut,"<html><head>\n");


  fprintf(cgiOut,"<title>my cgic test</title>\n");


  fprintf(cgiOut,"</head>\n");


  fprintf(cgiOut,"<body>\n");


//HTML BODY


  if(cgiFormSubmitClicked("name_submit")==cgiFormSuccess){


  hand_result=hand_user_name("user_name",user_name_result,USER_NAME_MAX_BYTE);


  }


  switch(hand_result){


     case -1:


         fprintf(cgiOut,"OH! You have a mars name,it is too long!\n回火星去吧");


         break;


     case -2:


         fprintf(cgiOut,"OH! You have not input you name!");


         break;


     case 0:      


         fprintf(cgiOut,"Whelcom   ");


         fprintf(cgiOut,user_name_result);


         break;


     default:


         fprintf(cgiOut,"Do you have a name?");


     break;


  }


 


  fprintf(cgiOut,"</body></html>\n");


  return 0;


}


 


int hand_user_name(char *formname,char *username,int name_maxlength){


  int fetch_result=cgiFormStringNoNewlines(formname,username,name_maxlength);


 


  if(fetch_result==cgiFormTruncated){


     return -1;


  }else if(fetch_result==cgiFormEmpty){


     return -2;


  }else if(fetch_result==cgiFormSuccess){


     return 0;


  }


  return -3;


}

在这个实验中我们主要使用了 cgiFormSubmitClicked() 函数来检查用户所点击的提交按钮,并更加提交按钮的不同来做不同的处理