tags:

views:

743

answers:

4

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

+2  A: 

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

Jon Tackabury
+3  A: 

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['house_id'], validate it using is_numeric() and then display its info.

Click Upvote
+2  A: 

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
+3  A: 

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 the Javascript submits the form.

Ben
Good solution, but I think using $_GET would be a better idea in this case.
Click Upvote
btw, do you use capital A for the <a href> tag?
Click Upvote
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
True, perhaps he didn't know that it could be done via $_GET also :)
Click Upvote