Strange String and URL Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IWP506@gmail.com

    Strange String and URL Problems

    Hey, I've got some code here that puzzles me. I'm putting together a
    url based on user input. Should be pretty straightforward , but there's
    a problem. First, here's my code.

    ---

    $myurl="write.p hp?T1=" . $_GET["T1"] . "&T2=" . $_GET["T2"] . "&T3=" .
    $_GET["T3"] . "&T4=" . $_GET["T4"] . "&T56=" . $_GET["T56"] . "&T7=" .
    $_GET["T7"] . "&T89=" . $_GET["T89"] . "&T10=" . $_GET["T10"] . "&T11="
    .. $_GET["T11"];

    echo $myurl;
    echo "<a href=\"" . $myurl . ">OK - Next Step &gt;&gt;" .
    "</a>";

    ---
    ** Please note that the 3 lines with the $_GET variables are really on
    1 line.

    So, that makes my link fine. The problem is my output:

    write.php?T1=Wo rd1
    Word2

    It takes spaces in the $_GET["T1"] and for some reason makes them into
    new lines....... which throws off my url completly because it's then
    sending you to write.php?T1=Wo rd1, dropping word2 and the rest of the
    get variables.

    Why is this??

    Please help. Sorry if this wasn't clear, but it's a bit confusing, and
    I'll try to clarify it if you need more.

    Thanks,
    iwp506@gmail.co m

  • IWP506@gmail.com

    #2
    Re: Strange String and URL Problems

    Sorry, but I have a few corrections. I meant "4" lines instead of 3,
    and it DOESN'T make my line fine.

    Comment

    • Ken Robinson

      #3
      Re: Strange String and URL Problems

      IWP506@gmail.co m wrote in news:1124478378 .973496.290910
      @z14g2000cwz.go oglegroups.com:
      [color=blue]
      > Hey, I've got some code here that puzzles me. I'm putting together a
      > url based on user input. Should be pretty straightforward , but there's
      > a problem. First, here's my code.
      >
      > ---
      >
      > $myurl="write.p hp?T1=" . $_GET["T1"] . "&T2=" . $_GET["T2"] . "&T3=" .
      > $_GET["T3"] . "&T4=" . $_GET["T4"] . "&T56=" . $_GET["T56"] . "&T7=" .
      > $_GET["T7"] . "&T89=" . $_GET["T89"] . "&T10=" . $_GET["T10"] . "&T11="
      > . $_GET["T11"];
      >[/color]

      Format your string using urlencode():

      $tmpa = array();
      foreach ($idx in array('T1','T2' ,'T3','T4','T56 ','T7','T89','T 10'))
      $tmpa[] = $idx . '=' . urlencode($_GET[$idx]);
      $myurl = 'write.php?' . implode('&',$tm pa);

      You'll notice I changed the way the code creates the URL. By using the
      foreach() construct, you can easily change what indices you use.
      [color=blue]
      > echo $myurl;
      > echo "<a href=\"" . $myurl . ">OK - Next Step &gt;&gt;" .
      > "</a>";
      >[/color]

      echo '<a href="' . $myurl . '">OK - Next Step &gt;&gt;</a>';

      See if that helps.

      Ken

      Comment

      • IWP506@gmail.com

        #4
        Re: Strange String and URL Problems

        That looks great to me, but when I run it, I get a

        Parse error: syntax error, unexpected T_STRING in C:\Program
        Files\Apache Group\Apache2\h tdocs\schedule\ confirm.php on line 48.

        Line 48 is foreach ($idx in
        array('T1','T2' ,'T3','T4','T56 ','T7','T89','T 10')).

        But I don't see a problem... Commenting out the code you gave resolves
        the error...

        Thanks,
        iwp506@gmail.co m

        Comment

        • Ken Robinson

          #5
          Re: Strange String and URL Problems

          IWP506@gmail.co m wrote in news:1124480293 .949505.8120
          @f14g2000cwb.go oglegroups.com:
          [color=blue]
          > That looks great to me, but when I run it, I get a
          >
          > Parse error: syntax error, unexpected T_STRING in C:\Program
          > Files\Apache Group\Apache2\h tdocs\schedule\ confirm.php on line 48.
          >
          > Line 48 is foreach ($idx in
          > array('T1','T2' ,'T3','T4','T56 ','T7','T89','T 10')).
          >[/color]

          Can you post the lines before line 48. Sometimes the error is actually on a
          preceding line and PHP doesn't realize it until something doesn't make
          sense.

          Ken

          Comment

          • IWP506@gmail.com

            #6
            Re: Strange String and URL Problems

            <?php

            $tmpa = array();
            foreach ($idx in array('T1','T2' ,'T3','T4','T56 ','T7','T89','T 10')) {
            $tmpa[] = $idx . '=' . urlencode($_GET[$idx]);
            }
            $myurl = 'write.php?' . implode('&',$tm pa);


            echo $myurl;

            echo '<a href="' . $myurl . '">OK - Next Step &gt;&gt;</a>';

            ?>

            That's the only php in this document...

            Sorry for being a pest

            iwp506@gmail.co m

            Comment

            • IWP506@gmail.com

              #7
              Re: Strange String and URL Problems

              Oh, I tried adding the { }'s after it didn't work the first time,
              because the php manual uses them, but they didn't help.

              Comment

              • Ken Robinson

                #8
                Re: Strange String and URL Problems

                IWP506@gmail.co m wrote in news:1124481301 .015505.79180
                @g49g2000cwa.go oglegroups.com:
                [color=blue]
                > <?php
                >
                > $tmpa = array();
                > foreach ($idx in array('T1','T2' ,'T3','T4','T56 ','T7','T89','T 10')) {[/color]

                My error (that's what I get for not checking my code...)
                The foreach line should read:

                foreach (array('T1','T2 ','T3','T4','T5 6','T7','T89',' T10') as $idx)
                [color=blue]
                > $tmpa[] = $idx . '=' . urlencode($_GET[$idx]);
                > }
                > $myurl = 'write.php?' . implode('&',$tm pa);
                >
                >
                > echo $myurl;
                >
                > echo '<a href="' . $myurl . '">OK - Next Step &gt;&gt;</a>';
                >
                > ?>
                >
                > That's the only php in this document...
                >
                > Sorry for being a pest[/color]

                Not a problem. How else would you learn...

                Ken

                Comment

                • IWP506@gmail.com

                  #9
                  Re: Strange String and URL Problems

                  Works like a charm, thanks for your patience!

                  Thanks,
                  iwp506@gmail.co m

                  Comment

                  Working...