Solved

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

Posted on 2006-11-13
6
783 Views
Last Modified: 2012-05-05
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
Comment
Question by:xberry
  • 2
  • 2
  • 2
6 Comments
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 125 total points
ID: 17932279
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
 
LVL 6

Accepted Solution

by:
aescnt earned 125 total points
ID: 17932474
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
 
LVL 14

Author Comment

by:xberry
ID: 17932736
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 125 total points
ID: 17932782
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
 
LVL 6

Assisted Solution

by:aescnt
aescnt earned 125 total points
ID: 17932899
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
 
LVL 14

Author Comment

by:xberry
ID: 17933544
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now