string in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eagleswings
    New Member
    • Mar 2007
    • 2

    string in array

    I am new to PHP so sorry if this has been covered before - couldn't find anything through a search.

    I want to set up an array that I can use to fill a selection box in a form. My array looks like

    $AdTypeArray = array(1=> 'Not known', 'Newspaper text only', 'Newspaper Column Colour', 'Newspaper Column BW', 'Web directory', 'Directory Column Colour',
    'Search Engine', 'Website', 'Car', 'Recommendation ', 'Shop');

    and my form code is:

    <td id='input'><sel ect name='AdType'>" ;

    for ($n=1;$n<=sizeo f($AdTypeArray) ;$n++)
    {
    echo "<option value=$AdTypeAr ray[$n]";
    if ($_POST['AdType'] == $AdTypeArray[$n])
    {
    echo " selected";
    }
    echo "> $AdTypeArray[$n]";
    }

    echo "</select></td>

    On the form I get the full text from the array value but when the form is submitted $_POST[AdType] only holds the characters up to the first space. I have tried double quotes single quotes and combinations. Put double and single quotes around each value gives me the output I want from the form but the display on the form shows the value surrounded by the single quotes.

    Any ideas please?

    Robert
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to TSDN!

    Read the Posting Guidelines (top of this forum), especially the part about enclosing any shown code within php or code tags!!

    When you post code, post all relevant code, and not just part of it. Where e.g. is the <form> statement, the 'submit' button, etc.?

    Your problem: your box values have blanks in them, so when you show these entries in a value field, you must enclose them within quotes, otherwise only the first word will show.

    Here is the code as it works [php]
    if (isset($_POST['submit'])) {
    print_r($_POST) ;
    exit;
    }
    echo '<form action="a.php" method="POST">' ;
    echo "<td id='input'><sel ect name='AdType'>" ;

    for ($n=1;$n<=sizeo f($AdTypeArray) ;$n++) {
    echo "<option value='$AdTypeA rray[$n]'";
    if ($_POST['AdType'] == $AdTypeArray[$n]) {
    echo " selected='selec ted'";
    }
    echo "> $AdTypeArray[$n]</option>";
    }
    echo '</select></td>';
    echo '<input type="submit" value="submit" name="submit">' ;
    echo '</form>';
    [/php]

    Ronald :cool:

    Comment

    • eagleswings
      New Member
      • Mar 2007
      • 2

      #3
      Very many thanks, I knew it would be something simple.

      Sorry about the poorly displayed code, I will read all of the guidance before next posting and promise to behave in future!

      Rob
      Last edited by eagleswings; Mar 13 '07, 05:54 PM. Reason: mistyped word

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        You are welcome and will see you next time.

        Ronald :cool:

        Comment

        Working...