Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.