• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1140
  • Last Modified:

Text after space character cut off in input text field when reloading php script

I have created a genuine PHP-form
that reads bank informations such as account number and
owner of bank account from input text form fields
when the user submits the form. In case there is a mistake
the form is reloaded with calling $_SERVER['PHP_SELF']
and the existing entries in the form's text fields are recreated
by reading from the POST variables. This works fine except in case
where the user puts a text string that is seperated by a
space character. So when he types 'Anne Anyone'
and submits the form, then next time there would only
appear 'Anne' in the input text field. How to return the
complete name combination if it is divided by empty characters ?

0
xberry
Asked:
xberry
  • 2
  • 2
  • 2
4 Solutions
 
VoteyDiscipleCommented:
There's no inherent reason that won't work.  If you echo $_POST['name']  you'll get back exactly what was submitted with the form.

There must be something else going on in the code that's making this happen.  We'd have to see some code to know what.
0
 
aescntCommented:
I think I know whats happening, but this is just a guess. This is usually the culprit but there maybe other cases :)
Does your code look like:
<input type=text name=firstname value=<?php echo $_POST['firstname'] ?>>

If so, add quotes around your attribute values:
<input type="text" name="firstname" value="<?php echo $_POST['firstname'] ?>">

Or even better:
<input type="text" name="firstname" value="<?php echo htmlentities($_POST['firstname']) ?>">

Adding htmlentities() in there to make sure that the text is properly escaped.
0
 
xberryAuthor Commented:
Actually I solved it some seconds ago, but I am not experienced enough
to tell what was the exact reason behind, so here is what happened:

First, with the described problem, my code looked like this:
<?php someotherphpcode ...; echo 'value='.$_POST["name"];?>

Then I changed it to

<somehtml...  value="<?php echo $_POST["name"];?>">

which returned form-text-values as expected.

It seems to me that echoing the variable together with the html attribute (value)
instead of remaining inline caused the problem here, but why exactly ?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
VoteyDiscipleCommented:
Ahh, yep, aescnt solved it.

If you have...   echo 'value='.$_POST['name'];

... it will print out     value=Bob Jones

If you have value="<?php echo $_POST['name']; ?>"

... it will print out     value="Bob Jones"


Why?  Because you wrote quotes in the second one and didn't write quotes in the first one.  It has nothing to do with where or how the code was written, you just forgot quotes in the first version.
0
 
aescntCommented:
If you want it inline:

Change: <?php someotherphpcode ...; echo 'value='.$_POST["name"];?>
To: <?php someotherphpcode ...; echo 'value="'.$_POST["name"].'"';?>
notice the quotes.

And Even better: <?php someotherphpcode ...; echo 'value="'.htmlentities($_POST["name"]).'"';?> -- which is guaranteed not break when the text box contents has quotes and such :)
0
 
xberryAuthor Commented:
Ahhh . . . yes and html attributes that are seperate by space chars have
to be inside double qoutation in order to be recognized as the whole expression
that they are . . . right, thanks for that. I have to be rally careful . . . ; ))

I think it should be fair if I give VoteyDisciple the same amount of points
for this, since he helped me/us with the intro for solving this and
it was good to have the stable date from him
>> ("There's no inherent reason that won't work.  
  If you echo $_POST['name']  you'll get back exactly what was submitted with the form.")
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now