Multiple spaces become single space when submitting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • polturgiest
    New Member
    • May 2007
    • 3

    Multiple spaces become single space when submitting

    hie all

    i got a form

    Code:
    <form name="TEST" method=POST action="test.php">
    
    <input type="text" name="MyInput">
    
    <input type="submit" name="ACTION" value="SAVE">
    
    </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

    Code:
    if ( isset($_POST['ACTION']) )
    {
      if ( $_POST['ACTION'] == 'SAVE' )
      {
        $SQL = 'SELECT * FROM NAME WHERE NAME = \''.$_POST['MyInput'].'\'';  // this is where multiple spaces become single space
      }
    }
    help me on this..thanks
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    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(" ","*",$_POS T['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

    Comment

    • polturgiest
      New Member
      • May 2007
      • 3

      #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?

      Comment

      • Purple
        Recognized Expert Contributor
        • May 2007
        • 404

        #4
        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

        Comment

        • polturgiest
          New Member
          • May 2007
          • 3

          #5
          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]

          Comment

          • Purple
            Recognized Expert Contributor
            • May 2007
            • 404

            #6
            Hi

            Good job, pleased you got there

            Regards Purple

            Comment

            Working...