Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you post data with a link

Tags:

html

php

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

like image 888
Howard May Avatar asked Jan 08 '09 22:01

Howard May


People also ask

How do you use POST method?

To send data using the HTTP POST method, you must include the data in the body of the HTTP POST message and specify the MIME type of the data with a Content-Type header. Below is an example of an HTTP POST request to send JSON data to the server. The size and data type for HTTP POST requests is not limited.

How do you POST a href?

You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.


5 Answers

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.

like image 105
Ben Avatar answered Sep 26 '22 17:09

Ben


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.

like image 23
Ali Avatar answered Sep 27 '22 17:09

Ali


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.

like image 34
Sergey Kuznetsov Avatar answered Sep 28 '22 17:09

Sergey Kuznetsov


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"

like image 30
brook Avatar answered Sep 27 '22 17:09

brook


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

like image 26
Jon Tackabury Avatar answered Sep 29 '22 17:09

Jon Tackabury