21

I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?

Regards

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
Howard May
  • 6,639
  • 9
  • 35
  • 47

7 Answers7

53

If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:

function formSubmit(house_number)
{
  document.forms[0].house_number.value = house_number;
  document.forms[0].submit();
}

Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:

<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">

<?php
foreach ($houses as $id => name)
{
    echo "<a href=\"javascript:formSubmit($id);\">$name</a>\n";
}
?>
</form>

That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Ben
  • 66,838
  • 37
  • 84
  • 108
  • Good solution, but I think using $_GET would be a better idea in this case. – Ali Jan 08 '09 at 23:10
  • btw, do you use capital A for the tag? – Ali Jan 08 '09 at 23:14
  • Oh - the capital letter was just a typo, really - I didn't let go of the shift key quickly enough! :o) I guess maybe I misunderstood the OP's question, but I thought he was specifically looking for a way to do it using POST. – Ben Jan 08 '09 at 23:22
  • True, perhaps he didn't know that it could be done via $_GET also :) – Ali Jan 08 '09 at 23:35
  • 2
    Thanks @Ben my Google search for "send post data with link" stopped here! – Web_Designer Jul 19 '11 at 23:12
  • @Ben Good solution. I have a similar case, and it involves sending POST data to a page at another site. Others saying "use GET" was no help, so thanks for actually answering the question. You get my "Click Upvote" the old fashioned way ... by earning it. – KiloVoltaire Dec 19 '15 at 18:47
12

I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:

<a href="house.php?id=<?php echo $house_id;?>">
  <?php echo $house_name;?>
</a>

Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.

yashastew
  • 28
  • 7
Ali
  • 261,656
  • 265
  • 575
  • 769
9

You cannot make POST HTTP Requests by <a href="some_script.php">some_script</a>

Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:

isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']

And in the streeview.php make links like that:

<a href="house.php?houseVar=$houseNum"></a>

Or something else. I just don't know your files and what inside it.

Sergey Kuznetsov
  • 8,591
  • 4
  • 25
  • 22
8

This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.

@ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.

however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.

here is an example is i use:

in PHP Class,

public function styleButton($style,$text){
    $html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
    $html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
    $html_str .= "<input id='view_button' type='submit' value='".$text."' >";
    $html_str .= "</form>";
    return $html_str;
}

Then in the CSS id="view_form" set "display:inline;" and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"

brook
  • 247
  • 2
  • 15
4

I would just use a value in the querystring to pass the required information to the next page.

Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
0

We should make everything easier for everyone because you can simply combine JS to PHP Combining PHP and JS is pretty easy.

$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";

Or a somewhat safer way

$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
0

This post was helpful for my project hence I thought of sharing my experience as well. The essential thing to note is that the POST request is possible only with a form. I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.

so I basically created each of the navigation items as a form using a loop as follows:

<ul>
    begin loop...
        <li>
            <form action="/" method="post">
                <input type="hidden" name="country" value="India"/>
                <button type="submit" name="button">India</button>
            </form>               
        </li>
    end loop.
</ul>

what it did is to create a form with hidden input with a value assigned same as the text on the button. So the end user will see only text from the button and when clicked, will send a post request to the server.

Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.

here is a screen shot of the navigation... enter image description here