HTML INPUT

soffcec
soffcec used Ask the Experts™
on
When I post text from HTML <form>  I get no following spaces so if I post text string who sould be 80 bytes long and the text is only 10 bytes i ONLY GET 10 BYTES LONG STRING.  How disable stripping off the spaces ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dr. KlahnPrincipal Software Engineer

Commented:
What server is being used - Apache, IIS, nginx?
soffcecManager

Author

Commented:
Windows 2012 IIS
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
As far as I know you can't with the default form functionality  - HTML ignores white space. You will need to pad the string on the server.

However, you should be able to do it with jQuery
HTML
  <form action="t2322.php" method="post">
    <input type="text" name="comment" />
    <input type="submit" />
  </form>
  <pre id="result"></pre>

Open in new window

jQuery
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(function() {
  $('form').submit(function(e) {
    e.preventDefault();
    var x = $(this).serialize();
    $.post('t2322.php', x, function(resp) {
      $('#result').html(resp);
    });
  });
});
</script>

Open in new window

Server script (for interest) - this just reflects the input with [ ] around the data to show the spaces.
<?php
echo "[{$_POST['comment']}]";

Open in new window


Working sample here
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
NB if the jQuery solution does not work for you then I would guess that whatever you are using server side is trimming the input in which case you must pad on the server or terminate the string on the client with a non-whitespace character which you then trim on the server.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Using the simplest possible <form> and text <input>, trailing spaces (in Firefox and Chrome) are sent as '+' signs and are normally converted back to spaces by the server.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Updated sample following Dave's comment
<form action="t2322.php" method="post">
<input type="text" name="comment" />
<input type="submit">
</form>

Open in new window

Server
<pre><?php
$raw = file_get_contents("php://input");
echo "Raw : [{$raw}]<br/>";
echo "POST: [{$_POST['comment']}]<br/>";
?></pre>

Open in new window

Result
Raw : [comment=abc++++++++++]
POST: [abc          ]

Open in new window

Sample here

I did an initial test to see if I could replicate the authors situation. When I looked at the results it appeared that I had - however, here was my mistake which I think is relevant to this discussion.

I viewed the results as HTML - which means even though the spaces where being sent to the server and reflected back to the browser - the HTML rendering of those results is what "removed" the white space from the submitted value - so the 10 spaces I added to the end of my input came out as a single space when rendered in HTML - an assumption on my part that what I was seeing was a trimmed response was in fact my just not seeing the single space at the end of the output.

This could have been prevented had I used <pre> tags as above to contain the output.

This might be what the author is seeing - the reflected value has the spaces but they are not being shown because of HTML rendering removing the superfluous white space.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial