【问题标题】:How to Get and Post Method at the same time如何同时获取和发布方法
【发布时间】:2014-11-05 08:28:29
【问题描述】:

这已经困扰我好几天了。我正在创建一个列表网站。列表来自我们网站中的直接列表创建(使用数据库)。此外,我们还以 data feed 的形式从另一个网站获取列表。

首先,有人做过同样的工作吗?我想知道这是否可能。 我们能否从两个来源(数据库的直接列表和另一个网站的数据馈送)获取列表,并将它们全部显示在同一页面上(例如页面 a)?

如果是,那么我们如何使用一个搜索表单来搜索列表结果?我假设需要同时使用 get 和 post 方法。获取因为我们使用如下的数据馈送

<form name="Search_Form" method="get" action="http://a.com/a/a.php" onsubmit="return checkForm()">

并发布数据库中的直接列表,如下所示

<form method="post" action="a.php">

任何建议都将受到高度赞赏。

谢谢大家

【问题讨论】:

    标签: php mysql post get datafeed


    【解决方案1】:

    你不能同时 GET 和 POST,它们是两种不同的 HTTP 方法。您正在向 URL 发出 GET 请求或 POST 请求。

    如果您需要从外部数据源抓取数据,那就是 GET。如果用户在您的网站上搜索,那么这通常也是一个 GET(因为如果查询参数在 URL 中,那么可以缓存、被搜索引擎抓取等)。

    在您的场景中,服务器端 PHP 脚本会通过 cURL 向外部数据馈送发出 GET 请求,并将结果保存到变量中;您还可以在此脚本中查询您的数据库;然后最后使用用户提交的值过滤结果。

    除非我误解了您的问题,否则我不确定 POST 的来源。

    【讨论】:

      【解决方案2】:

      您应该使用$_REQUEST 全局变量。这将保存 $_POST$_GET 传递给脚本的项目。你可以在这里阅读更多...http://php.net/manual/en/reserved.variables.request.php

      【讨论】:

        【解决方案3】:

        不是让表单提交您的请求,而是让您的 checkForm 例程使用 ajax 单独进行调用?然后,您可以将结果组合到您正在执行的任何显示中。记得让 checkForm 返回 false;

        我注意到您尚未接受答案,而且我的答案含糊不清。如果您想要从两个来源收集数据,其中一个使用 GET,另一个使用 POST,您可以这样做。我包括一个使用 ajax 的示例 javascript。当您单击按钮时,checkForm 函数将发送一个 POST 请求,并在完成后向第二个服务发送一个 GET。结果可以组合起来,在用户看来,它就像是一次操作。这是有效的代码(当然,您必须根据您的服务对其进行调整)。

        <?xml version="1.0" encoding="ISO-8859-1" ?>
        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Form</title>
            <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        
            <script type="text/javascript">
        
        
            var postData = null;
        
            $(document).ready(function() {
        
                $("#formDiv").show();
                $("#tableDiv").hide();
        
            });
        
            function checkForm() 
            {
        
                postData = $("#Search_Form").serialize();
        
                alert(postData);
        
                $.ajax({
                       type: "POST",
                       url: "http://localhost:8080/FirstAjaxJspTest/AjaxService",
                       data: postData, // form's input turned to parms
                       contentType: "text",
                       success: function(data)
                       {
                            tableBuild(data); 
                        },
                        complete: function(data) {
                            nextAjaxCall();
                        },
                        failure: function(msg) 
                        {
                            alert("Failure: " + msg);
                        }
                });
        
                return false;
        
            }
        
        
            function nextAjaxCall() 
            {
        
                $.ajax({
                        type: "GET",
                        url: "http://localhost:8080/FirstAjaxJspTest/AjaxService",
                        data: postData, // form's input turned to parms
                        contentType: "text",
                        success: function(data) {
                            tableBuild(data); 
                            tableDisplay();
                        },
                        failure: function(msg) {
                            alert("Failure: " + msg);
                        }
        
                });
        
                return false;
        
            }
        
            function tableBuild(data)
            {
        
                data.forEach(function(entry) {
                    $("#resultsTable").append($("<tr><td>" + entry.name + "</td><td>" + entry.address + "</td></tr>"));
                });
                return;
        
            }
        
            function tableDisplay()
            {
        
                $("#formDiv").hide();
                $("#tableDiv").show();
                return;
        
            }
        
            </script>
        
        </head>
        <body>
            <div id="formDiv">
                <form id="Search_Form">
                    Name:<br/>
                    <input type="text" id="name" name="name" /><br/>
                    SSN:<br/>
                    <input type="text" id="ssn" name="ssn" /><br/><br/>
                    <button type="button"  onclick="return checkForm()">Submit</button>
                </form>
            </div>
            <div id="tableDiv">
                <table id="resultsTable">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Address</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </body>
        </html> 
        

        【讨论】:

          【解决方案4】:
          <table>
          
              <c:forEach var="w" items="${workers_data}">
          
                  <c:url var="workerLink" value="WorkerController1">
                      <c:param name="action" value="LOAD" />
                      <c:param name="workerId" value="${w.id}" />
                  </c:url>
          
                  <c:url var="deleteLink" value="WorkerController2">
                      <c:param name="action" value="DELETE" />
                      <c:param name="workerId" value="${w.id}" />
                  </c:url>
          
                  <tr>
                      <td>${w.id}</td>
                      <td>${w.name}</td>
                      <td>${w.age}</td>
                      <td>${w.salary}</td>
          
                      <td><a href="${workerLink}">Update</a> <a
                          href="${deleteLink}"
                          onclick="if (!(confirm('Delete worker?'))) return false">
                              Delete</a><br></td>
                  </tr>
          
              </c:forEach>
          
          
          </table>
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-10-17
          • 2014-08-13
          • 2019-10-25
          • 2014-06-04
          • 1970-01-01
          • 2014-01-22
          • 1970-01-01
          相关资源
          最近更新 更多