How it's supposed to work:
What happens?
Page1
<html>
<head>
<meta name="viewport" content="width=320, initial-scale=1">
<meta charset="utf-8">
<style>
body, html {
min-width: 100%;
min-height: 100%;
margin: 0;
padding: 0;
font: Arial 14px;
}
</style>
</head>
<body>
<form id="my-form"action="page2.php"method="post">
<input type="url" name="url" placeholder="Type or paste a valid URL" />
<input type="submit" value="Search"id="my-form-button">
<p id="my-form-status"></p>
</form>
<script>
window.addEventListener("DOMContentLoaded", function() {
// get the form elements defined in your form HTML above
var form = document.getElementById("my-form");
var button = document.getElementById("my-form-button");
var status = document.getElementById("my-form-status");
// Success and Error functions for after the form is submitted
function success() {
form.reset();
button.style = "display: none ";
status.innerHTML = "Success";
}
function error() {
status.innerHTML = "Oops! There was a problem.";
}
// handle the form submission event
form.addEventListener("submit", function(ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method, form.action, data, success, error);
});
});
// helper function for sending an AJAX request
function ajax(method, url, data, success, error) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response, xhr.responseType);
} else {
error(xhr.status, xhr.response, xhr.responseType);
}
};
xhr.send(data);
}
</script>
<?php
if ($_POST)
echo $_POST['field1'];
?>
</body>
</html>
Page2
<?php
if($_POST)
$content=file_get_contents($_POST["url"]);
else
$content="No url specified.";
// where are we posting to?
$url = 'page1.php';
// what post fields?
$fields = array(
'field1' => $content,
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>
Page2 has been posting to an email API for testing purposes and it rarely sends an email. If I make it send the URL instead of the URL contents it works all right. Can someone figure it out? I'm really just a beginner with PHP or coding of any kind in fact.
@El_Vanja thanks for your help. So that this question can be considered answered I am now posting the code you helped me with as well as my solution to the missing style sheet problem. I ended up simply injecting the base tag into the results before outputting them.
Page1
<body>
<div id="header" style="background-color:blue;
color:white;
text-align:center;
position:fixed;
width:100%;
">
<h1 id="title">
WEB PAGE DOWNLOADER
</h1>
<form id="my-form"action="page2.php"method="post">
<input type="url" inputmode="search" name="url" placeholder="Type or paste a valid URL" id="input"/>
<input type="submit" value="Search"id="my-form-button">
<p id="my-form-status"></p>
</form>
</div>
<script>
window.addEventListener("DOMContentLoaded", function() {
// get the form elements defined in your form HTML above
var form = document.getElementById("my-form");
var button = document.getElementById("my-form-button");
var status = document.getElementById("my-form-status");
var result = document.getElementById("myresult");
var header = document.getElementById("header");
var title = document.getElementById("title");
// Success and Error functions for after the form is submitted
function success(response,responseType,responseText) { status.innerHTML = "Url successfully posted to download script.";
document.write(responseText);
}
function error() {
status.innerHTML = "Oops! There was a problem.";
}
// handle the form submission event
form.addEventListener("submit", function(ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method, form.action, data, success, error);
});
});
// helper function for sending an AJAX request
function ajax(method, url, data, success, error) {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response, xhr.responseType,xhr.responseText);
} else {
error(xhr.status, xhr.response, xhr.responseType);
}
};
xhr.send(data);
}
</script>
</body>
Page2
<?php
if($_POST){
$opts = array('http' =>
array(
'header' => 'User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3',
)
);
$context = stream_context_create($opts);
$content = file_get_contents($_POST["url"], false, $context);
// $content = file_get_contents($_POST["url"]);
if(!$content)
echo "ERROR: Download script received URL but could not return its contents.";
}
if(!$_POST){
echo "ERROR: Download script could not receive URL. Please use the <a href='page1.php'>WEB PAGE DOWNLOADER</a>. This is only a script." ;
}
if($content)
$repstr = "<head>
<base href='".$_POST["url"]."'>";
echo str_replace("<head>",
$repstr,
$content);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.