Using POST within SWITCH statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stewdizzle
    New Member
    • Apr 2009
    • 6

    Using POST within SWITCH statement

    I am setting up a small site for personal use. It consists of a form that transfers inputed values to an html template. The ouput is code that i can use to quickly post on a website. In the template i have the following code:
    Code:
    <?php $images = $_POST["myRadioButton"];
       switch ($images)
       {
       case 1:
         echo '<img src="http://i43.tinypic.com/29z3eyo.jpg">';
         break; 
       case 2:
         echo '<img src="http://i43.tinypic.com/29z3eyo.jpg"> &nbsp;
               <img src="http://i43.tinypic.com/29z3eyo.jpg">';
         break;
        case 3:
         echo '<img src="http://i43.tinypic.com/29z3eyo.jpg">&nbsp;
               <img src="http://i43.tinypic.com/29z3eyo.jpg">&nbsp;
               <img src="http://i43.tinypic.com/29z3eyo.jpg">';
         break;}
       ?>
    I want to replace the url with a variable:

    Code:
    $_POST["image1"];
    image1 being the field that the url would be entered into. I have done it outside the switch statement and it works fine. Any suggestions?
  • Amzul
    New Member
    • Oct 2007
    • 130

    #2
    i dont realy understand what do you mean bt replace the url
    somthing like that:
    header('Locatio n: $_POST['image1']");
    or you want to show $_POST['image1'] in the url like that
    http://$_POST['image1']/
    please explain

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      I wouldn't use switch, rather a for- or while-loop.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Can we see the HTML form you are using to submit the data? That way, if it sheds some light on the situation, we may be able to offer a better way to accomplish the task.

        - mark.

        Comment

        • stewdizzle
          New Member
          • Apr 2009
          • 6

          #5
          This is the code for the start page

          Code:
          <table width="800px"> 
          <tr>
          <td>
          <!--Select how many images and disable unused text boxes-->
          <form action="combinedcode.php" method="post">
          
          <input type="radio" name="myRadioButton" value="1" onclick="image1.disabled=false; image2.disabled=true; image3.disabled=true" checked="checked" /> 1 Picture
            
          <input type="radio" name="myRadioButton" value="2" onclick="image1.disabled=false; image2.disabled=false; image3.disabled=true" /> 2 Pictures
          
          <input type="radio" name="myRadioButton" value="3" onclick="image1.disabled=false; image2.disabled=false; image3.disabled=false" /> 3 Pictures
          
          
          <!--Information-->
            
            <p>Enter the Auction Title:
            <input type="text" name="title" cols="50" />
           
            <br />
            <br />
              Enter A Description: 
            <textarea cols="50" rows="10" wrap="soft" name="description"></textarea>
            <br />
            <br />
              Enter the URL for Image 1: <input type="text" name="image1" />
            <br />
            <br />
              Enter the URL for Image 2: <input type="text" name="image2" />
            <br />
            <br />
              Enter the URL for Image 3: <input type="text" name="image3" />
            
            </p> 
            <p>
           <input type="submit" name="submit1" value="Generate Auction Code"/>
            </p>
            
          </form>
          </td>
          
          </tr>
          </table>

          This is the output page:
          Code:
           <center>
           <!--This is the text to copy-->
          <textarea cols="100" rows="25" readonly="readonly" wrap="soft" onclick='highlight(this);'>
           <?php require_once('combinedtemp.html'); ?>
          
          </textarea>
          <br />
          <br />
          <!--This is the preview-->
          <?php include('combinedtemp.html'); ?> 
          </center>

          This is the relevant template code with variables:

          Code:
          <div class="float">
            <?php $images = $_POST["myRadioButton"];
             switch ($images)
             {
             case 1:
               echo '<img src="http://i43.tinypic.com/29z3eyo.jpg">';
               break; 
             case 2:
               echo '<img src="http://i43.tinypic.com/29z3eyo.jpg"> &nbsp;
                     <img src="http://i43.tinypic.com/29z3eyo.jpg">';
               break;
              case 3:
               echo '<img src="http://i43.tinypic.com/29z3eyo.jpg">&nbsp;
                     <img src="http://i43.tinypic.com/29z3eyo.jpg">&nbsp;
                     <img src="http://i43.tinypic.com/29z3eyo.jpg">';
               break;}
             ?>
          Before using the switch statement I used the below code to pull in the URL from the input page but I couldn't specify how many boxes to pull from:


          Code:
          <a href="<?php echo $_POST["image1"]; ?>" target="_blank"><img height="200" src="<?php echo $_POST["image1"]; ?>" ></a>

          Comment

          • stewdizzle
            New Member
            • Apr 2009
            • 6

            #6
            I know its not pretty and may not be the most efficient way to write it but I am learning as I create.

            the actual site link is http://codegenerator.x10hosting.com/combined.php

            Comment

            • stewdizzle
              New Member
              • Apr 2009
              • 6

              #7
              I figured it out. I had to echo each part of the image src code seperately: Like i said, not pretty but it gets the job done. If anyone can walk me through making this better i would appreciate the learning experience.


              Code:
              <?php $images = $_POST["myRadioButton"];
                 switch ($images)
                 {
                 case 1:
                   echo '<img src="';
                   echo $_POST["image1"]; 
                   echo '">';
                   break; 
                 case 2:
                   echo '<img src="';
                   echo $_POST["image1"]; 
                   echo '"> &nbsp;';
                   echo '<img src="';
                   echo $_POST["image2"]; 
                   echo '">';
                   break;
                  case 3:
                   echo '<img src="';
                   echo $_POST["image1"]; 
                   echo '"> &nbsp;';
                   echo '<img src="';
                   echo $_POST["image2"]; 
                   echo '"> &nbsp;';
                   echo '<img src="';
                   echo $_POST["image3"]; 
                   echo '">';
                   break;}
                 ?>

              Comment

              • Amzul
                New Member
                • Oct 2007
                • 130

                #8
                [CODE=PHP]
                case 1:
                echo '<img src="';
                echo $_POST["image1"];
                echo '">';
                break;
                [/CODE]
                write it like that:
                [CODE=php]
                case 1:
                echo "<img src='$_POST[\'image1\']'>";
                break;
                [/CODE]
                double quotes evaluate the variables

                Comment

                • stewdizzle
                  New Member
                  • Apr 2009
                  • 6

                  #9
                  I tried that but i got this error

                  <br />
                  <b>Parse error</b>: syntax error, unexpected T_ENCAPSED_AND_ WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in <b>/home/sbmccal/public_html/template.html</b> on line <b>95</b><br />

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you could also try
                    Code:
                    echo '<img src="', $_POST['image1'], '">";

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      You might also try
                      Code:
                      echo "<img src='{$_POST['img']}' />";
                      :)

                      Comment

                      Working...