Socrates
Socrates

Reputation: 189

How to echo inserted value (=not default value) after form submission in PHP

I have a form field for estimating shipping costs. Currently the field has a label "ZIP/Postal code" and the form field itself is empty by default. I want to do the following:

  1. Put the label "inside" the form field so that it says "Enter your zipcode..." (probably by value="

  2. Once the users puts the cursor into the form field, the default text is deleted.

  3. Once the users entered his zip code and clicked the submit button, his zip code (=the value he entered) is echoed in the form field, thus replacing the default text

Here's the code I am working with:

<label for="postcode" <?php echo $this->__('Zip/Postal Code') ?></label>
<input class="input-text validate-postcode<?php if ($this->isZipCodeRequired()):?>
 required-entry<?php endif;?>" type="text" id="postcode" name="estimate_postcode" 
 value="<?php echo $this->htmlEscape($this->getEstimatePostcode()) ?>" />

How can I achieve this?

Upvotes: 0

Views: 270

Answers (2)

magicianiam
magicianiam

Reputation: 1579

<script>
function clearField()
{
  $(#postal_code).val('');
}
</script>


 <form>
 <input id = "postal_code" type = "text" name = "postal_code" value = "Input postal code here" onclick="clearField()"/>
 </form>

something like this should work. clearField will just empty the field when the user clicks on it, and the in putted data would then be send by the form.

Upvotes: 0

Judd Lyon
Judd Lyon

Reputation: 538

You can make the text disappear on the client-side using the HTML5 placeholder attribute. To make it cross-browser, use the jQuery Placeholder plugin (or one similar).

Depending on how you submit the form, you will have *estimate_postcode* available as a GET or POST variable.

$_GET['estimate_postcode']

$_POST['estimate_postcode']

Echo that as the value attribute. This won't be persistent across pages though.

If you are doing this strictly for the user's visual benefit, ditch the PHP and do it all on the client-side via cookies.

Upvotes: 3

Related Questions