0

I am programming a server side script on an Apache machine with cgi. I am using C for the cgi programming. I am a total noob and learning from online examples(I must say except the basics I didn't come across more web sources for detailed learning!). I am having a simple HTML page where the username(input) is added to a list which is a file I have in my system and then the updated list should be displayed in the SAME PAGE. I am not able to "print" the results of both the script and http link on the same page so therefore in the code below, you will only see buttons. Please help.

Here is what I have: Html:

<html>
<head><title>Home</title></head>
<body>
<h1>REGISTER</h1>
<form action= "/cgi-bin/mycgi.cgi" name ="create user" method ="get">
Enter name:<input type="text" name="user">
<br>
<input type="submit" value="add">
</form>
 <FORM action="http://localhost:8000/getusers/" method="get">
    <P>
    <input value="Display Users" type="submit">
    </P>
 </FORM>
</body>

Here is the cgi Code:

#include<stdio.h>
#include<string.h>

int main(){
char *tmpStr;
char *user;
printf("Content-Type:text/html\n\n");
printf("<html><head><title></title></head><body>");
tmpStr = getenv("QUERY_STRING");
while(tmpStr && *tmpStr != '='){
tmpStr++;
}
user = tmpStr+1,
printf("Adding %s to  User Database",user);
//system("wget http://localhost:8000/newuser/");//call script to add user?
printf("</body></html>");  
return 0;//return user?
}

Could you please tell me how I can realize these? How can I display the user list without opening a new html site? Also in the above C code, I have to call the link "http://localhost:8000/newuser/" which returns a success or failure value. How can I return it to the parent form? Thanks.

user907810
  • 3,208
  • 10
  • 39
  • 47
  • 2
    important to notice: loop part of your code, is buggy doesn't checks if `*tempStr` == `\0`, it may causes illegal memory access. better to write it as `while(*tempStr && *tmpStr != '='){....}` – Grijesh Chauhan Jul 23 '13 at 12:54
  • @GrijeshChauhan more better: `while (tmpStr && *tmpStr && *tmpStr != '=') {}` would result in segfault, if `tmpStr` is `NULL`. – mohit Jul 23 '13 at 13:04
  • Thanks guys. Corrected it. Infact I did get a seg fault when it was null and then I noticed it. – user907810 Jul 23 '13 at 13:30

1 Answers1

0

You could add an iframe to your html:

<iframe id="theiframe" name="theiframe"></iframe>

And then setting the target of your form to the iframe:

<form action= "/cgi-bin/mycgi.cgi" name ="create user" method ="get" target="theiframe">

Anyway, it is not clear to me if the updated list should be displayed when you click on the first or second button.

user2553780
  • 161
  • 8
  • thanks. I could use this to display the list of users. However the poitn is I want to have permenently display the result of the link on the html page. Once new user is added. It has to be refreshed(probably via the cgi script). Any ideas? – user907810 Jul 23 '13 at 13:38
  • can I use the target in the form to have say text areas too?? – user907810 Jul 23 '13 at 13:38
  • @user907810 Let's see. As for your first question, you need to set up a javascript function that periodically queries for the list of users. And for the second question, anything inside the iframe will be replaced by the contents of the CGI response. However, the response itself could contain textareas and do whatever you need. But keep in mind that if it's constantly refreshed, any input tag will be useless. – user2553780 Jul 23 '13 at 13:50
  • I am actually writing a C cgi script. Where to integrate the JavaScript? – user907810 Jul 23 '13 at 14:50
  • CGI's are functions on server side that provide functionality that cannot be achieved on client side. You write js scripts on your webpage, and from there you may call periodically any CGI residing on your server. Check [link](http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) for more information. – user2553780 Jul 23 '13 at 15:10
  • Hi. Thanks for the link and reply. However I still dont see how it may help. I think the problem I have is to display the "getuser" http link on the html page when the page is created in the first place. I dont want any "SUBMIT BUTTONS" at all. It should be already there for me wehn I open the page. I just started with Javascript. I dont want to call a script when I do any SUMIT button function at all. Can you help me? – user907810 Jul 24 '13 at 09:35
  • @user907810 You may use the onLoad event on the body of your html page. This event is triggered when the page is loaded and it calls the javascript function you specify. I'd recommend you te get started with jQuery too (a javascript library) which will be really handy. With jQuery you may use the `$(document).ready(...)` event for the same purpose. – user2553780 Jul 24 '13 at 12:05