?
Solved

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

Posted on 2006-11-13
6
Medium Priority
?
1,028 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 500 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 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 500 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 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