Jump to content

Need to post form values to a remote script using Javascript


rashedirshad

Recommended Posts

Hi,

 

I need to post form values to a PHP file located at other server. I want to use Javascript. I have done it with PHP CURL. I need to insert form values to mysql server located at myserver.  I will ask client to include javascript in contact us file. If I use PHP for it then I can only install it to PHP based websites.

 

Here is the PHP function. I need same with Javascript.

 

function fetch_remote_file($url, $post_data=array())
{
    $post_body = '';
    if(!empty($post_data))
    {
        foreach($post_data as $key => $val)
        {
            $post_body .= '&'.urlencode($key).'='.urlencode($val);
        }
        $post_body = ltrim($post_body, '&');
    }
//echo $post_data['fn'];
    //echo $post_body;
    if(function_exists("curl_init"))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        if(!empty($post_body))
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
     else if(function_exists("fsockopen"))
    {
        $url = @parse_url($url);
        if(!$url['host'])
        {
            return false;
        }
        if(!$url['port'])
        {
            $url['port'] = 80;
        }
        if(!$url['path'])
        {
            $url['path'] = "/";
        }
        if($url['query'])
        {
            $url['path'] .= "?{$url['query']}";
        }
        $fp = @fsockopen($url['host'], $url['port'], $error_no, $error, 10);
        @stream_set_timeout($fp, 10);
        if(!$fp)
        {
            return false;
        }
        $headers = array();
        if(!empty($post_body))
        {
            $headers[] = "POST {$url['path']} HTTP/1.0";
            $headers[] = "Content-Length: ".strlen($post_body);
            $headers[] = "Content-Type: application/x-www-form-urlencoded";
        }
        else
        {
            $headers[] = "GET {$url['path']} HTTP/1.0";
        }
        
        $headers[] = "Host: {$url['host']}";
        $headers[] = "Connection: Close";
        $headers[] = "\r\n";
        
        if(!empty($post_body))
        {
            $headers[] = $post_body;
        }
        
        $headers = implode("\r\n", $headers);    
        if(!@fwrite($fp, $headers))
        {
            return false;
        }
        while(!feof($fp))
        {
            $data .= fgets($fp, 12800);
        }
        fclose($fp);
        $data = explode("\r\n\r\n", $data, 2);
        return $data[1];
    }
    else if(empty($post_data))
    {
        return @implode("", @file($url));
    }
    else
    {
        return false;
    }
}  
echo fetch_remote_file('http://myserver.com/ipinfo.php?ip='.$_SERVER['REMOTE_ADDR'], $_REQUEST);

But I need to stay on the same page. User should not know that his information is stored somewhere else. I can post values to the file on remote server and send those variables back to original file but that is a lengthy process and user will know what happened at back end.

You can create a hidden iframe (using css with:0px; height:0px;) and set the form target to that iframe and the action to your php script. The form will submit to the iframe and the user wont see it. If you want to load a thank you page, set a timeout for a few seconds and that send the user to that page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.