By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

Multiple spaces become single space when submitting

P: 3
hie all

i got a form

Expand|Select|Wrap|Line Numbers
  1. <form name="TEST" method=POST action="test.php">
  2.  
  3. <input type="text" name="MyInput">
  4.  
  5. <input type="submit" name="ACTION" value="SAVE">
  6.  
  7. </form>
if i input "Welcome ", with a few spaces, i cannot get all the spaces. the $_POST['MyInput'] only give single space. can anyone help me

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ( isset($_POST['ACTION']) )
  3. {
  4.   if ( $_POST['ACTION'] == 'SAVE' )
  5.   {
  6.     $SQL = 'SELECT * FROM NAME WHERE NAME = \''.$_POST['MyInput'].'\'';  // this is where multiple spaces become single space
  7.   }
  8. }
  9.  
  10.  
help me on this..thanks
Jun 25 '07 #1
Share this Question
Share on Google+
5 Replies


Purple
Expert 100+
P: 404
Hi polturgiest and welcome to TSDN !!

I have just tested this using :

[PHP]<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>New document</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form name="TEST" method=POST action="spaces.php">

<input type="text" name="MyInput">

<input type="submit" name="ACTION" value="SAVE">

</form>
<?php
if (!empty($_POST['MyInput'])) $var = str_replace(" ","*",$_POST['MyInput']);
else $var = "not set";
echo "==>".$var."<==";
?>
</body>
</html>[/PHP]

and the spaces are maintained - I am running IIS, IE7 and PHP 5.1

try the code posted and confirm if the leading spaces are changed to *s

Regards Purple
Jun 25 '07 #2

P: 3
i've tried this

[PHP]$var = $_POST['MyInput']; // for example "AH 1902" with 2 spaces
$tmp = str_replace(" ", "*", $var);
print "var is : ".$var." ( ".strlen($var)." )";
print "tmp is : ".$tmp." ( ".strlen($tmp)." )";[/PHP]

output :
[HTML]var is : AH 1F02 ( 8 )
tmp is : AH**1F02 ( 8 )[/HTML]

when i print var, its only give me one blank space (tried it by copying to notepad) even its return 8 when strlen it

any idea?
Jun 26 '07 #3

Purple
Expert 100+
P: 404
Hi polturgiest,

What you are seeing is your browser rendering the two spaces to one - this is not happening within your code - this is confirmed by the string length of the two variables at 8 chars..

This removal of spaces is normal behaviour for HTML.

What is the issue with the database query ?

Purple
Jun 26 '07 #4

P: 3
i cannot find macthing records before using this, bcoz most of my records got many spaces :( .

solved

[PHP]
$var = str_replace(' ', '&nbsp;', 'Micheal Owen'); // with multiple spaces
print $var; // the result

// when query i used this

$SQL = 'select name from player where name = \''.str_replace('&nbsp;', ' ', $var).'\''; // working its take all the spaces
[/PHP]
Jun 26 '07 #5

Purple
Expert 100+
P: 404
Hi

Good job, pleased you got there

Regards Purple
Jun 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.