Space added in a text field - php

I have a problem with an input type text.
The value of the field is affected by PHP, like this:
<input type="text" class="form-control" name="ClientName" id="ClientName" value="<?php echo strtoupper($Client['name']).' '.trim($Client['surname']); ?>" />
Each time i have a space added at the end of the string, so when i save datas in my db with htmlentities() function, i have for example:
NAME Surname
I tried a lot of things, i'm sure my data from PHP don't have a space at the end. When i save datas in db, i make a TRIM on this input.
I can't remove this space and i don't know where it comes from!!
Do you have any idea?
Thanks

Related

How to remove special characters in URL during Form Submission

I am trying to remove the special characters during form submission in PHP. Everytime I want to submit a data for example :
<form action="?" method="get">
<input type="text" name="str" value="I want to remove this" />
</form>
the output in the browser looks like this
http://localhost/?str=I+want+to+remove+this
Is there anyway that we can get rid of those "+" before submission or during submission?
My expected result is
http://localhost/?str=I want to remove this
Thanks for your help...
In the url you cannot have empty spaces, on the server side
str=I+want+to+remove+this
will be read as
str=I want to remove this
only. So i do not think you really need to worry about that encoding.

php - input doesnt show the full value

I'm trying to put input value, which is called from base.
<input class=\"title\" name=\"title\" value=".$edit['title']." />
Title is : New York Welcome! But it shows just New. If i write NewYorkWelcome! it shows right, but when there are spaces - no.
I just tried to echo the $edit['title'] not in input - it shows correctly. Very strange problem. Please, any solution?
If the string is delimited by double quotes, you must escape those as you did with the previous attributes:
<input class=\"title\" name=\"title\" value=\"{$edit['title']}\" />

Apostrophe in Textarea PHP

I have a textarea that is part of a form that submits to a PHP file.
The problem is that when an apostrophe (’) is entered into the textarea, the corresponding REQUEST variable in PHP turns up empty ($_REQUEST['description']). If there is no apostrophe, the $_REQUEST['description'] contains the textarea text as intended. Entering punctuation like single quotes and double quotes also works but an apostrophe does not. The same problem occurs for <input type="text"></input> as well. Is there any way to fix that?
Try this
HTML code
<form action="cible.php" method="POST">
Group name: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
PHP :
<?php
$groupname = htmlspecialchars($_POST['user'], ENT_QUOTES);
echo $groupname;
?>
It's work fine for me
Do you have magic quote in your php config ? Try to disable it.

stripslash acting weird or is that normal?

I have a database var that contains
5/8\" Cabinet Grade Plywood
the \ being added by either WP or SQL to escape the ".
when retrieving this var i use stripslashes() both in in the value of the field used to edit that table (so that the next time someone want to edit that field he/she will see whats in that input already) and in the actual website where it suppose to appear.
The weird thing is ..
in the field it cuts from 5/8\" Cabinet Grade Plywood to just 5/8
and in the website where it suppose to appear it shows normally without slashes or anything unusual.
this is how I stripslash the field:
$somevar = '<input value="'.stripslashes($currentselected['something']).'" class="niceclass" name="something" type="text" />';
and this is how I use it when it appears on page:
<td><span style="font-size: larger;"><?php echo stripslashes($goods['verygood']); ?></span></td>
it simply collides with HTML markup
<input value="'.stripslashes($currentselected['something']).'"/>
will result in
<input value="5/8" Cabinet Grade Plywood" />
take a look on those ", its broken right there, you need to escape those "
to fix this use urlencode function in php
<input value="'.urlencode(stripslashes($currentselected['something'])).'"/>
or htmlspecialchars function, it should replace quotes with
"
You could use some htmlspecialchars on the output. The " has special meaning in HTML, and in this case would actually be seen as the closing of the value attribute of the input. Thus, you should escape it (htmlspecialchars will translate " to ").

Trailing spaces when using <textarea>

<tr>
<td>
<b>Escalation:
</td></b>
<td>
<TextArea name='escalation' onKeyDown=\"limitText(this.form.escalation,this.form.countdown,100);\"
onKeyUp=\"limitText(this.form.escalation,this.form.countdown,100);\">$Text</textarea>You have <input readonly type=\"text\" name=\"countdown\" size=\"3\" value=\"100\"> characters left.
</td>
</tr>
That is a excerpt of the code im trying to use. Basically I'm trying to fill the text area with a value stored in a php variable, which comes from a SQL database. the Javascript functions limit the amount of text in a block to 100 Chars.
Problem is that it fills whatever space isnt used in the initial value with spaces! I printed the $Text between two quotes so I would know for a fact it doesnt have spaces in the database, which it doesnt. You can also clearly see that I dont have any space at all between the textarea tags so that isnt the issue that I see other posters have.
Any ideas?
Yes, I have seen that behavior before. Check to see if the column that you are reading the value from in the database is of type "CHAR" or type "VARCHAR". It is more efficient to always use fixed-length (CHAR) over variable-length (VARCHAR) field types, so databases are sometimes designed that way. The down side is that shorter data stored in those fields is always padded with spaces.
The solution: You probably have a line in your PHP that looks something like this:
$Text = $row['text'];
Change that line to the following:
$Text = trim($row['text']);
The 'trim' function will strip leading and trailing spaces. If you are using fixed length fields, remember that you will HAVE TO pad the values that you write to the database as well. That means that you will have to add leading spaces to the string to be written to the database to make the then proper length for fixed-width field.

Categories