tags:

views:

202

answers:

3

Hi,

I am having C function. From the html page, through ajaxcall the request is passed and the response is got.

If I perform some action like click submit button, then some action will be performed. How to get back to the same html page after redirection?

Can I set the environment variable which automatically redirects me to that page?


EDIT: Adding more detail from 'answer' below

Thank you Roe.

As I am new to this I am not able to get it. If we are using php then I can get the URL and put it in the header("Location:".$url).

But I am having only C functions.

HTML Code


function x(id)
{
    var val;
    var URL = window.location.href;
    if(id =="y")
    {
     val=1;
     document.location = "http://ipaddr/cgi-bin/file.cgi?value="+val+"&url="+URL;
    }
}

C Code


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

int main(void)
{
    printf("Content-type: text/html; charset=utf-8\n\n");

    char* data = getenv("QUERY_STRING");
        //process
}

How to return from C to the same page? How to give header("Location:".$url); in C??

A: 

Your question is rather unclear, but if I understand you correctly: present the user with something that looks like a submit button, but actually just performs your AJAX operation. You stay on the same HTML page, get back your return from the server, and alter the page in whatever manner is appropriate.

Joe Mabel
No, Ajaxcall is a seperate one which will give request and get the response periodically. I have to accept some user input and perform some operation on click of a submit button. After on click, it goes to the file.cgi. How to get back to the same page? If I click I get Internal server error.
MalarN
+1  A: 

Strictly speaking, this has nothing to do with neither cgi nor c, but anyway.

You have a couple of options, I suppose the best would be to put the current page as an <input type="hidden" name="currentPage" value="/my/current/page"/>, that way you'll get it in your form submission as a variable and you can redirect back to that page from your script by putting a Location: header in your response (or by other means, such as a link and a http-refresh meta tag on your confirmation page, or whatever you're doing).

EDIT
There's the referrer as mentioned, but this might or might not be set (I suppose there are 'privacy' plugins for browsers which remove the referrer header).

Now there's the http-equiv refresh option, which is detailed in another answer, so I'll show you the Location-header style. And this is actually cgi. :) The CGI protocol says you should start your output with a header (assuming you're not doing NPH-CGI, but if you are, I hope you know what you're doing) followed by an empty line. Simply put this as your header:

printf ("Status: 302 Moved Temporarily\r\n");
printf ("Location: %s\r\n", url);
printf ("\r\n"); /* end of header */

You need to parse the query string (in case of a GET request, otherwise parse stdin, but that's a little bit more complicated) and extract the url. You can do any kind of processing you like, although be aware that any data you output will most likely never be displayed to the user (as opposed to the http-equiv refresh is likely to cause the browser to flash the content, empty or not, briefly before moving to the next page).

301s (moved permanently) are cached, and the browser might bypass your cgi script the next time and go to the url directly, which is not what you want.

roe
I have given the code snippet. Help me out in resolving this.
MalarN
A: 

The referring page is given by the environment variable HTTP_REFERER. You can get back to the same page you came from by using a redirect page, like the following:

const char * redirect_page_format =
"<html>\n"
"<head>\n"
"<meta http-equiv=\"REFRESH\"\n"
"content=\"0;url=%s\">\n"
"</head>\n"
"</html>\n";
printf (redirect_page_format, getenv (HTTP_REFERER));

Ideally you should check that HTTP_REFERER is a valid environment variable before doing this.

Kinopiko
Thank you Kinopiko.It worked well.
MalarN