Solved

Database value into textbox - cutting off problem

Posted on 2004-04-22
28
287 Views
Last Modified: 2013-12-12
The form code below is a section of code that is meant to display a
user's details in textboxes (it should be simple to understand) for
editing. It functions fine - witht he exception of the 3rd line of PHP
code marked below. The value= part of the textbox does contain the
value of 'fname' (which holds a user's full name) - however, it cuts
it off at the space. (ie: John Smith becomes just John, Jane Smith
becomes Jane, etc).

Incidently if i just use print the value on its own (not in the form
tag) - it works fine.

Anyone tell me why this might be happening?

Code below. Many thanks.

--------------------------------------------------------------------

<form method='post' name='addform'>
 
<?=($e)?"":""?>

<?=($a[$p.'authtype'] == "db" || !$e)?"<B>"<input type=text
class='formtextbox' name='uname' size=10
value=".$a[$p.'uname'].">":$a[$p.'uname']?>

//PROBLEM ARISES ON THIS LINE
<?=($a[$p.'authtype'] == "db" || !$e)?"<input type=text
class='formtextbox' name='fname' size=20
value=".$a[$p.'fname'].">":$a[$p.'fname']?>

<?=($a[$p.'authtype'] == "db" || !$e)?"<input type=text
class='formtextbox' name='email' size=30
value=".$a[$p.'email'].">":$a[$p.'email']?>
0
Comment
Question by:stevehayter
  • 10
  • 7
  • 4
  • +3
28 Comments
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10887826
You aren't quoting the value...

value=".$a[$p.'fname'].">"

needs to be...

value='".$a[$p.'fname']."'>"



Alan
0
 

Author Comment

by:stevehayter
ID: 10887878
Thanks Alan.

I've changed it to...

print "<input type=hidden name='fname' value='".$a[$p.'fname']."'>";

but it still only displays the user's name up to the space.

Any other ideas!?
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10888652
$a[$p.'fname'] where does this com from ?

try
echo $a[$p.'fname']."<input type=\"text\" value=\"".$a[$p.'fname']."\">";
if u don't see the same value before and in the text field, php is nuts but i don't think so ;)

btw you must quote ALL values for ANY attribute including numeric values, and strings even if they don't contain spaces.
it is not very safe either to change lines inside a tag though i don't think many parsers (text) are old enough to run into this.

quoting was probably your issue indeed but you must have uploaded a cached version of your page when you tryed.
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10888688
There is nothing wrong with the code you posted other than the quoting issue I already pointed out. If the code you posted is actually the "exact" code you are testing, then your problem lies somewhere else... perhaps a problem with the variable itself.



Alan
0
 

Author Comment

by:stevehayter
ID: 10888932
"you must have uploaded a cached version of your page when you tryed"

It is not cached - it uses meta no-cache tags. And echoing the value does work properly, thats what i don't understand!

"perhaps a problem with the variable itself"

What exactly do you mean?
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10890712
<< It is not cached - it uses meta no-cache tags. And echoing the value does work properly, thats what i don't understand! >>
this is definitely not reliable as your browser can override those settings.
anyway it must not be the issue since you must have tryed many times since. (simple way to be sure, modify or add a character each time)

variable itself : i though of something in the code before.
i guess p is a prefix...
did u try the exact code i gave : i mean echo the variable on the same line as the field just to be sure.

can you post 1 sample for the code (just the part under the query to get what the construction comes from),
and 1 sample for the resulting source code. is the complete variable visible in there ?
can the variable contain quotes ? (would definitely mess it up)
line endings ? (depending on the browser)
and just to be sure does it work when all the quotes are in place ?

this is weird : i've been using similar codes for ages on with all kind of form controls and they always worked properly.
if u put the quotes and don't use some kind of weird browser,
and the variable's content is actually a regular string without quotes, it definitely SHOULD work !

that's why i'd like to see how you construct that variable.

cheers

ps i digged mu code a bit and forgot quite a few quotes on the type and size attributes (located before the value each time)
...and did not have issues though i must admit i did not do extended tests with older browsers for form fields.
0
 
LVL 9

Expert Comment

by:gtkfreak
ID: 10891514
Try below:
value=".$a[$p.\"fname\"].">":$a[$p.\"fname\"]?
0
 

Author Comment

by:stevehayter
ID: 10899093
Bear with me, quite a long post! By gthe way thanks for your help so far guys...

skullnobrains, here is the code you asked for.

THIS IS THE QUERY:
----------------------
$query = "
      SELECT
            user_id,user_uname,user_fname,user_email,user_type,user_authtype
      FROM
            user
      WHERE
            user_id = $id
      ORDER BY
            user_uname ASC";
      
$r = db_query($query);
$a = db_fetch_assoc($r);
----------------------

THIS IS THE FULL FUNCTION FOR COMPLETION OF THE USER DETAILS FORM:
----------------------
<?
function doUserForm($a,$p='',$e=0) {
?>
</div><div id='separator'></div><div id='navsubtitle'><?=($e)?"Edit Existing User":"Add A New User"?></div><div id='bodytext1'>
      
<form method='post' name='addform'>
      
<?=($e)?"":""?>

<?=($a[$p.'authtype'] == "db" || !$e)?"<b>Username: </b><input type=text class='formtextbox' name='uname' size=10 value=".$a[$p.'uname'].">":$a[$p.'uname']?>

<?=($a[$p.'authtype'] == "db" || !$e)?"<b>Full Name: </b>".$a[$p.'fname']."<input type=text class='formtextbox' name='fname' size=19 value=".$a[$p.'fname'].">":$a[$p.'fname']?>
      
<?=($a[$p.'authtype'] == "db" || !$e)?"<b>E-Mail: </b><input type=text class='formtextbox' name='email' size=25 value=".$a[$p.'email'].">":$a[$p.'email']?>
      
<b>Type: </b><select class='formtextbox' name=type>
            
<option value='stud' <?=($a[$p.'type']=='stud')?" selected":""?>>Student</option>
            
<option value='prof' <?=($a[$p.'type']=='prof')?" selected":""?>>Academic Staff</option>
            
<option value='staff' <?=($a[$p.'type']=='staff')?" selected":""?>>Other Staff</option>
            
<option value='admin' <?=($a[$p.'type']=='admin')?" selected":""?>>Portal Administration</option>
      
</select>
      
<input type=hidden name='action' value='<?=($e)?"edit":"add"?>'>
<?
if ($e) {
      print "<input type=hidden name='id' value='".$a[$p."id"]."'><input type=hidden name=commit value=1>";
      if ($a[$p.'authtype'] != "db") {
            print "<input type=hidden name='uname' value=".$a[$p.'uname'].">";
            print "<input type=hidden name='fname' value='".$a[$p.'fname']."'>";
            print "<input type=hidden name='email' value=".$a[$p.'email'].">";
      }
       } else {
      print "";
      }      
?>
<input type='button' class='NavButtonRed' onClick='document.addform.submit()' value='<?=($e)?"Update Details":"Save"?>'>
            
      
</form>
      
<?
}
?>
-------------

Theres a whole load of other code for the $p prefix - i will give this to you also if you wish.

Regards
Steve
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10899667
<< Theres a whole load of other code for the $p prefix - i will give this to you also if you wish. >>
i assume 'user' in your db query is the same as $p ? "no thanks" : "there's something i dont understand"

actually, i keep thinking that the fields IN THE DB must contain something it does not like (though it is weird)
\n, \0, or such characters ?
if the other attributes (after name) don't show, you can be almost sure.

is there something when u INPUT to the db that could be it ?
if for eg you ask for name and firstname, and perform a concatenation, firstname may end with a '\0' and this would not be accepted by many browsers ?

<< $a[$p.'fname']."<input type=text class='formtextbox' name='fname' size=19 value=".$a[$p.'fname'].">" >>
if this outputs "firstname name [ firstname ]", it looks more like that kind of problem.

does this part of the source show in html
AS firstname name<input type=text class='formtextbox' name='fname' size=19 value="firstname name">"
OR AS .........................................................................................................value="firstname">
OR MAYBE ...................................................................................................value="firstname
name">

if you have the first that's more like a damn crazy browser issue
if you have the second that's simply unbelievable
if you have the latter, you probably got your problem
dunno how a '\0' character would be output but it would definitely mess the browser.

digging shamelessly into your code
btw i didn't know you could directly send output using the ...?...:... syntax.
that's more like html-oriented and rather cool :)
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10899711
btw, if the '\n' '\0' stuff is the issue, a simple trim when u input should let u out, but you'll have to update all the existing inputs.
try and manually input a user in the db an see if this one displays properly if u're not sure.
0
 
LVL 1

Expert Comment

by:noy_ipunk
ID: 10900797
Hi,

To be honest I don't really know what you guys are discussing about....and it looks complicated alright. Just want to add a small post for it might help in a way.

What I know is only that you're trying to post a string which contain space...I usually use the trick:

$customer = ereg_replace(" ", "//",$customer); //change the char 'space' into '//'

then pass this new value to the next page...and change it back again afterwards with :

$customer = ereg_replace("//", " ",$customer);  //and you get it back with 'space' instead of '//'

-----------------------------
second..maybe you should try to 'trim' the string if you think the string looks the same but somehow they're not (i.e. different in length). Sometime they have 'space' in front and behind that we can't see.

anyway....I don't think what I said might help..., just sharing ideas ;)
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10901617
you can post values that contain a space without that !!!
and slashes will be stripped so you may have to unstrip them afterwards !!.

actually it is more about a whitespace which would print properly in a page (such as '\n' or '\r') (shows properly here btw)
but which would mess the navigator.

if u write
$a="one line
two lines";
echo"<input value=\"$a\">";
the parser may read
<                   start tag
input              name of tag (ended by whitespace)
value=           first attribute
one line\n\r    end of attribute, the parser is lost and if you are unlucky, waits for '>' before display
the parser may or may not understand the other attributes if there are any
(the lucky version is the rest of the tag gets printed in the page so u see it at once)

i guess this may happen more often if quotes have been forgoten before as this is a commonly handled mistake,
which might result in parsing in a kind of safe mode that does not take care of quotes properly (ie give higher priority to other chars)

of course even an ideally designed parser will be mislead by a quote in the vaiable (even if it is escaped)

...but maybe this is not at all the point. other ideas ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:stevehayter
ID: 10901907
Once again, thanks everyone for your help! :oD

So much to go through! So start with with the 'simpler' things..

1. I input a value manually in the database (firstname surname - literally) but all that displays is firstname. The HTML output for the textbox is:

<form method='post' name='addform'>
      
<b>Username: </b>
<input type=text class='formtextbox' name='uname' size=10 value=testusername>      
<b>Full Name: </b>
<input type=text class='formtextbox' name='fname' size=19 value=firstname>
<b>E-Mail: </b>
<input type=text class='formtextbox' name='email' size=25 value=first.second@domain.com>      
<b>Type: </b>
<select class='formtextbox' name=type>
            <option value='stud'  selected>Student</option>
            <option value='prof' >Academic Staff</option>
            <option value='staff' >Other Staff</option>
            <option value='admin' >Portal Administration</option>
</select>
      
<input type=hidden name='action' value='edit'>
<input type=hidden name='id' value='13'><input type=hidden name=commit value=1><input type='button' class='NavButtonRed' onClick='document.addform.submit()' value='Update Details'>
</form>

So don't think thats the problem!

2. The ereg_replace workaround seems a bit messy - especially since it would require me to change the other areas of code where the fullname is output.

3. The name is all one attribute!

Further to that i'm lost!
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10901974
You guys are spinning your wheels here. I said it once, I said it twice, and I will now say it again... There is "ABSOLUTLY" nothing wrong with the way he is displaying the data in the input tags' value property, other than the missing quotes I pointed out early on in this post, which by the way are gone again in more recent posts to this thread. I still feel the problem has to be in the variable itself.

stevehayter, you said earlier that the varible printed by itself correctly, right? So, try this to prove it one way or the other...

$name = $a[$p.'uname'];
echo $name;

That will show weather or not the variable is OK or not. Then try this...

<?=($a[$p.'authtype'] == "db" || !$e)?"<b>Username: </b><input type=text class='formtextbox' name='uname' size=10 value=".$name.">":$name?>

If nothing else, this will show the value of the var at runtime.


Alan







0
 
LVL 26

Accepted Solution

by:
skullnobrains earned 63 total points
ID: 10903134
ALAN

<< There is "ABSOLUTLY" nothing wrong with the way he is displaying the data in the input tags' value property, other than the missing quotes I pointed out early on in this post, which by the way are gone again in more recent posts to this thread. I still feel the problem has to be in the variable itself. >>

thanks for pointing out, that's what we where on as a matter of fact if you would take time to read.

thought that something in the variable could be the cause and looking for chars such as '\n' '\0'

if u can echo the variable directly,  why should storing it's value in an other change anything ?
outputting an strlen may be usefull to get sure there is no hidden char as they will not all display in a browser.

STEEVE

<?=($a[$p.'authtype'] == "db" || !$e)?"<b>Full Name: </b>".$a[$p.'fname']."<input type=text class='formtextbox' name='fname' size=19 value=".$a[$p.'fname'].">":$a[$p.'fname']?>

there still are missing quotes in your code.
sorry i didn't notice this morning.

should be

<?=($a[$p.'authtype'] == "db" || !$e)?"<b>Full Name: </b>".$a[$p.'fname']."<input type=text class='formtextbox' name='fname' size=19 value=\"".$a[$p.'fname']."\">":$a[$p.'fname']?>

btw
<input type=text class='formtextbox' name='fname' size=19 value=firstname>
probably was
<input type=text class='formtextbox' name='fname' size=19 value=firstname name>

and let's guess you forged this one.

<< I've changed it to...

print "<input type=hidden name='fname' value='".$a[$p.'fname']."'>"; >>

your from above is ok as well
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10903155
<< which by the way are gone again in more recent posts to this thread >>

slipped over this one, sorry alan (yes you did see it and point it as well and before ;)
0
 
LVL 9

Assisted Solution

by:AlanJDM
AlanJDM earned 62 total points
ID: 10903203
"if u can echo the variable directly,  why should storing it's value in an other change anything ?"
  -  It shouldn't change anything, which is exactly why I suggested doing it... so everyone would see that it is not an input tag problem.

Alan
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10903438
stevehayter,

skullnobrains and I have been through you post pretty thoughoughly and I have run several tests with variables containing spaces with no problem whatsoever. There has to be something else going on in your script you aren't mentioning or showing us. Are you sure there isn't some other place in your script that this $a[$p.'fname'] variable could be getting stepped on?

Here is a quick test script I ran...

test.php
=====
<html>
   <form method="post" action="<?= $PHP_SELF ?>">
      <input type="text" name="fname" value="" size="40"">
      <input type="submit" name="submit">
      <input type="text" name="fname2" value="<?= $_REQUEST['fname'] ?>" size="40" disabled>
   </form>
</html>

Try that script in your browser. You can enter what ever you want with any kind of spaces in in the first input and it will take that value and put it in the second input perfectly every time.

I know it must appear that I am being condecending in giving such a trivial example to try, but be assured, that is not my intention. I just want to show"visually" that the input tag doesn't care about spaces and that the problem has to be your variable and/or it's contents. And yes, I know you have also been saying the same thing skull... just trying to illustrated it in a different manner.


Alan
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 10911696
<< There has to be something else going on in your script you aren't mentioning or showing us. >>
100% agreed but actually, i believe the quote thing IS the issue and we spend lots of time for little use.

does it work now Steeve ?
see u around Alan :)
0
 
LVL 1

Expert Comment

by:Dr_Snapid
ID: 10914934
Perhaps you could post the output HTML? Is the full value being echod to the page, or only the first name?

It seems to me, that if it is correct in the HTML output (view source), it's the tag not being interpreted properly by the browser. If it's wrong in the HTML output, it's the variable..

If the source shows up as:
<input type=text
class='formtextbox' name='fname' size=20
value="John">

then the variable has been stepped on somewhere.

If the source shows up as:
<input type=text
class='formtextbox' name='fname' size=20
value="John Smith">

but the browser still only shows the first name, then something is breaking the tag, perhaps a hidden character.
0
 
LVL 1

Expert Comment

by:Dr_Snapid
ID: 10914945
I noticed you have said in your first post:

>>>Incidently if i just use print the value on its own (not in the form
tag) - it works fine.

This does offer a clue, try echoing it into a textarea. You may see any special characters show up in the textarea.
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 10999052
cleaning out some old e-mails and came across this thread...

stevehayter,

I guess you gave up on this issue?


Alan
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 11475380
no time to read through the whole thread again, but just a quote from myself, which i remember was quite true

<< i believe the quote thing IS the issue and we spend lots of time for little use. >>

... which means allan's very first post was the proper correct answer, as far as i remember.*

skull
0
 
LVL 9

Expert Comment

by:AlanJDM
ID: 11479762
skullnobrains and I both spent quite a bit of time trying to help this person and he simply stopped responding to our posts. I feel it would be resonable to split the points among those of us who tried to help.


Alan
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 11541501
thanks to you all although again i don't feel like i answered anything.
seems that losen time can sometimes be found again...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

17 Experts available now in Live!

Get 1:1 Help Now