Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why I am geting spaces from database

Status
Not open for further replies.

iffoiffy

Programmer
Feb 24, 2005
67
CA
Hi,

When I enter data through a form, using javascript trim function I remove all the left/right spaces if there is any before inserting in MYSQL,


When I display the same data like following


<td align=left><textarea name=display_size rows=4 cols=25 >'.$display_size.'</textarea></td>

Looks like I am geting spaces from the database. because even if there is one word in the database my textarea scroll bar gets activated.

I don't know why I am geting spaces?

Thanks
 
Can we see more than one line of your code. When it's out of context, who knows what's wrong.

Ken
 
Well, I think this is just your sloppy html.

Code:
<td align=\"left\">
  <textarea name=\"display_size\" rows=\"4\" cols=\"25\">
    {$display_size}
  </textarea>
</td>

Also, when storing to mysql, use PHP to trim whitespaces with trim(). Never use client-side coding for *important* validation!

I hope you also use mysql_real_escape_string() and strip_tags().

Good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
Olav

i've never used curly braces before inside html to identify php code. do you have to change the php.ini file to allow this? typically i use <?=$display_size?> notation.

Justin
 
Do this:
Code:
<?php
echo "<td align=\"left\">
  <textarea name=\"display_size\" rows=\"4\" cols=\"25\">
    {$display_size}
  </textarea>
</td>";
?>

Or:
Code:
<td align="left">
  <textarea name="display_size" rows="4" cols="25">
    <?=$display_size?>
  </textarea>
</td>

If you echo a string with a variable, you put the curlybraces around the variable.
It's to tell the processor what is the variable..
eg. {$foo}bar != $foobar

In some languages, this also makes the processor parse faster, but I dont know if this is also the case with PHP.
I however think it's good coding-practice.

Olav Alexander Mjelde
Admin & Webmaster
 
Actually, if you look at the piece of code the OP posted:
Code:
<td   align=left><textarea name=display_size rows=4 cols=25 >'.$display_size.'</textarea></td>
it lookes like it was part of an echo statement:
Code:
[COLOR=red]echo '[/color]<td   align=left><textarea name=display_size rows=4 cols=25 >'.$display_size.'</textarea></td>[COLOR=red]';[/color]
which is why I asked to see more of the code.

Yes, the HTML code is lousy, but it will work. We don't know what the OP's database code looks like, or whether this is a register_globals problem, which I suspect.

Commenting on the code fragment out of context does not solve the problem.

Ken
 
Actually, Actually,

I think I need to clarify why I felt I could answer this post, without more information than he gave.

When reading:
When I enter data through a form, using javascript trim function I remove all the left/right spaces if there is any before inserting in MYSQL,
...In the PHP forum, I think it's correct to tell him to use trim(), as it's not good to do things like this client-side (compatability issues, abuse, etc.), which I'm sure you agree on (most people do anyways).

And.. When I first gave him that tip, I thought it might be clever to the same time make him write proper HTML-code, as sloppy code can affect how the HTML is interpeted.

If you read his question one more time, he did not ask "Why cant I see any data from my database?", but he asked:
Looks like I am geting spaces from the database. because even if there is one word in the database my textarea scroll bar gets activated.

I don't know why I am geting spaces?
If the HTML code is faulty, that may affect how it is interpered in his browser, as I stated above.
I do not know if he tested in several browsers, if he tried echo'ing out the variable, etc. I agree this should be in his post and maybe he should have tested this before he tried asking us.

But even so, he says he gets the scrollbar, even if it's only one character. If he has one character, I guess the problem can not be register_globals, as then he would have no characters (NULL).

I might have mis-understood here, but the way I interpeted it, I came to the conclusion that his problem is not database->PHP, but rather javascript/html -> PHP.
It might also just be PHP -> HTML, which we would know more about, if he told us more information (as you requested, and I think it's good of you to request more information).

Therefore, I gave him the suggestion to clean up his coding and remove the javascript, to use trim() in PHP instead, in cooperation with strip_tags() and mysql_real_escape_string().

If that does not work, and if I understood him right, that he gets data from the database, I would recommend printing out the variable like so:

echo "#{$myvar}#";

Olav Alexander Mjelde
Admin & Webmaster
 
i too took the same view as Olav - it didn't appear to be a php issue but a pure html prob.

at the risk of continuing a chain when the OP hasn't even responded to the first response (! - more like hijacking) -i'd surmise that the issue is just default browser behaviour.

IE will display scroll bars whether or not they are needed or whether there is any text at all in the textarea. Firefox does not.

with css the default behaviour of conforming (hah) browsers can, of course, be changed.

hey ho. let's see what the OP thinks...
 
While I think iffoiffy's html code looks terrible there is actually nothing seriously wrong with it in a way that would make user agents display it differently. Quoted values are not necessary in HTML so that shouldn't really be a problem. From the code it is impossible to see if the code is well formed however, which could cause strange and erratic behaviour.
 
I have seen tags like:
<input type=text value=hello >
I guess that gives whitespace!

In his code above, I see that he uses textarea and that there is no whitespace between the variable or the textarea.

But, however so, he should fix his code.
I think this issue (as I said above), is before inserting to the db.

Olav Alexander Mjelde
Admin & Webmaster
 
Sorry for the late reply. Thanks for your suggestions. I had other text area where I was not having any problem even with out using the javascript trim function. I just took that html code and change the
variable inside and now it works. Though it turned out to be indetical code. So the code that is working with out space problem is



echo'<tr>
<td class=verdanasmall><b>display_size:</b></td>
<td align=left><textarea name=display_size rows=4 cols=25>'.$display_size.' </textarea></td>
</tr>';



And the cose that was giving space problem is as follows

echo'<tr>
<td class=verdanasmall><b>display_size:</b></td>
<td align=left><textarea name=display_size rows=4 cols=25 >'.$display_size.'</textarea></td>
</tr>';


Strange? both are same, but atleast it works now... not sure what was the problem
I did same to this other textreaa which was giving same space problem and that got fixed too
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top