Skip to content

Html post and get values outside a form

Html post AND get values outside a form

Retrieving html post and get values outside a form will come in handy when you are taking the page layout and design into consideration. Sometimes, you do not want a button or textfield to be where your form is but yet want to get the value of the element on the server side

a normal form looks like

<form>
  <button type='submit' value='submit'/>
</form>

so to be able to retrieve input value outside a form, you have to first give your form an id

<form id='submitForm'>
  <button type='submit' value='submit'/>
</form>

Lets say you have a textbox outside the form that you want to post/get together with your form when the submit button is pressed. You just have to give it the attribute ‘form=form_id’ like so,

<input type='text' name='txt' form='submitForm'/>

So finally you have something like this for the html

<input type='text' name='txt' form='submitForm'/>
<form id='submitForm'>
  <button type='submit' value='submit'/>
</form>

On the server side, for example in PHP, you would get your $_POST or $_GET as per normal like so,

<?php
$txt = $_POST["txt"];
echo "<p> The text you entered is $txt </p>";
?>

 

Enjoyed the content ? Share it with your friends !
Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.