3

This has doing my head in for a couple of days now. I am creating a listing website. The listings are coming from direct listing creation in our website (using database). And also, we get the listings from another website in the form of data feed.

First of all, has anyone done the same work? I wonder whether this even possible. Can we get the listings from both sources (direct listings from database and data-feed from another website) and show them all on a same page (page a for example)?

If YES, then how we can search for the listings results using one single search form? I assume get and post methods need to be used at the same time. Get because of the data-feed which we use as below

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

And post for the direct listings from database as below

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

Any suggestions would be highly appreciated.

Thanks heaps

Webnerdoz
  • 155
  • 1
  • 6
  • 15

5 Answers5

4

You can’t GET and POST at the same time, they’re two different HTTP methods. You’re either making a GET request or a POST request to a URL.

If you need to scrape data from an external data source, then that’s a GET. If a user is searching on your site then that’s usually a GET too (since if the query parameters are in the URL then that can be cached, scraped by search engines etc).

In your scenario, the server-side PHP script would do a GET request to the external data feed via say, cURL and save the results to a variable; you would also query your database in this script; and then finally filter the results using the values that the user submitted.

I’m not sure where POST comes into this, unless I’ve misunderstood your problem.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
2

You should be using the $_REQUEST global variable. This will hold $_POST or $_GET items passed to the script. You can read more here... http://php.net/manual/en/reserved.variables.request.php

BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
2

Instead of having the form submit your request have your checkForm routine make the calls separately using ajax? You can then combine the results for whatever display you are doing. Remember to have checkForm return false;

I'd noticed you hadn't accepted an answer yet and that mine was vague. If what you want to do is collect data from two sources using GET for one and POST for the other, you can do that. I'm including a sample javascript using ajax. When you click your button the checkForm function will send a POST request and, when it completes, send a GET to a second service. The results can be combined and it will look to the user as though its one operation. This is working code (although, of course, you will have to adapt it to your services).

<?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> 
BillFromHawaii
  • 334
  • 1
  • 7
0
<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>
  • Please explain why your answer works, that way others understand what it is doing. https://stackoverflow.com/help/how-to-answer – Marcello B. Sep 26 '19 at 04:55
0

Change the method to POST and put the variables you want to GET[] at the action url:

<form name="Search_Form" method="POST" action="http://a.com/a/a.php?variable=<?php echo $variable; ?>" onsubmit="return checkForm()">
Rodha
  • 1
  • 1