繁体   English   中英

PHP 脚本未发布数据

[英]PHP script not posting data

它应该如何工作:

  • 用户在第 1 页输入 URL。
  • 第 1 页将 URL 发布到第 2 页。
  • Page2 获取 URL 内容并将其发布回 page1。
  • 第 1 页显示 URL 的内容。

怎么了?

  • Page2 不发回任何内容。

第1页

<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> 

第2页



 <?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 已发布到 email API 用于测试目的,它很少发送 email。 如果我让它发送 URL 而不是 URL 内容它可以正常工作。 有人能弄清楚吗? 我实际上只是 PHP 或任何类型的编码的初学者。

@El_Vanja 感谢您的帮助。 所以这个问题可以被认为是回答我现在发布你帮助我的代码以及我对丢失样式表问题的解决方案。 我最终只是在输出结果之前将基本标签注入结果中。

第1页

    <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>

第2页

 <?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);

?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM