rashedirshad Posted July 26, 2011 Share Posted July 26, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/242861-need-to-post-form-values-to-a-remote-script-using-javascript/ Share on other sites More sharing options...
requinix Posted July 26, 2011 Share Posted July 26, 2011 Can you not simply change the action of the to point to the other server? Quote Link to comment https://forums.phpfreaks.com/topic/242861-need-to-post-form-values-to-a-remote-script-using-javascript/#findComment-1247453 Share on other sites More sharing options...
rashedirshad Posted July 26, 2011 Author Share Posted July 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242861-need-to-post-form-values-to-a-remote-script-using-javascript/#findComment-1247505 Share on other sites More sharing options...
requinix Posted July 26, 2011 Share Posted July 26, 2011 User should not know that his information is stored somewhere else. That's interesting. Why not? Quote Link to comment https://forums.phpfreaks.com/topic/242861-need-to-post-form-values-to-a-remote-script-using-javascript/#findComment-1247524 Share on other sites More sharing options...
nogray Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242861-need-to-post-form-values-to-a-remote-script-using-javascript/#findComment-1248760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
× 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.