Hi,

Hopefully I'm in the right forum. Farily new to PHP and have a couple questions which hopefully can be answered. First, I'm trying to validate my drop down list using a regular expression as shown below:

if(eregi("[a-z | A-Z]+", $_POST['whichtutorial'])){
$d=true;
}else{
$d=false;
$message[]="Please select a tutorial.";
}

My drop down list contains characters, a hyphen, and numbers, the above is not working currently, not sure where I'm going wrong. Secondly, how can I retain the value which the visitor selected from this drop down list when I validate? In other words, I have contact.php, which contains the form. After hitting the submit button, I get directed to contact_result.php. On this page, there's a link that allows users to go back to the contact.php and fill in their errors. So far I have sessions working properly, except on the drop down list, my attempt was this which didn't work (bolded below):

<select name="whichtutorial">
[b]<?php echo $_SESSION['tutorial_memory']['whichtutorial'];?>[/b]
<option value="null">--------------</option>
<option value="definingsitedwmx">Defining Site, DW MX</option>
<option value="definingsitedwmx04">Defining Site, DW MX 2004</option>
<option value="definingsitedwmx8">Defining Site, DW MX 2004</option>
<option>--------------</option>
<option value="htmlbasics">HTML &mdash; Basics</option>
<option>--------------</option>
<option value="htmlframes">HTML &mdash; Frames</option>
<option>--------------</option>
<option value="htmlforms">HTML &mdash; Forms</option>
<option value="htmlforms2">HTML &mdash; Forms &mdash; Part 2</option>
<option>--------------</option>
<option value="htmltables">HTML &mdash; Tables</option>
<option>--------------</option>
<option value="cssintro">CSS &mdash; Intro</option>
<option>--------------</option>
<option value="csstablelayout">CSS &mdash; Table Layout</option>
<option>--------------</option>
<option value="cssprint">CSS &mdash; Print</option>
<option>--------------</option>
<option value="csslayout">CSS &mdash; Layout</option>
<option value="csslayout2">CSS &mdash; Layout 2</option>
<option>--------------</option>
<option value="ftp">FTP</option>
<option>--------------</option>
<option value="hosting">Hosting</option>
<option>--------------</option>
<option value="phpcontactform">PHP &mdash; Contact Form</option>
<option>--------------</option>
<option value="phpdynamicmenu">PHP &mdash; Dynamic Menu</option>
<option>--------------</option>
<option Value="aspnetpart1">ASP.NET, Part 1</option>
<option Value="aspnetpart2">ASP.NET, Part 2</option>
<option Value="aspnetpart3">ASP.NET, Part 3</option>
<option Value="aspnetpart4">ASP.NET, Part 4</option>
<option Value="aspnetpart5">ASP.NET, Part 5</option>
<option Value="aspnetpart6">ASP.NET, Part 6</option>
<option Value="aspnetpart7">ASP.NET, Part 7</option>
<option Value="aspnetpart8">ASP.NET, Part 8</option>
<option Value="aspnetpart9">ASP.NET, Part 9</option>
<option Value="aspnetpart10">ASP.NET, Part 10</option>
<option>--------------</option>
<option Value="aspnettips-access-error">ASP.NET, Tips, Access Error</option>
<option Value="aspnettips-lockfile">ASP.NET, Tips, Lock file</option>
<option Value="aspnettips-sqlserver">ASP.NET, Tips, SQL Server Password</option>
<option Value="aspnettips-template">ASP.NET, Tips, VS.NET Template</option>
<option>--------------</option>
<option Value="flashone">Flash Article One</option>
<option Value="flashtwo">Flash Article Two</option>
<option Value="flashthree">Flash Article Three</option>
<option Value="flashfour">Flash Article Four</option>
<option Value="flashfive">Flash Article Five</option>
<option Value="flashsix">Flash Article Six</option>
<option Value="flashseven">Flash Article Seven</option>
<option Value="flasheight">Flash Article Eight</option>
<option Value="flashnine">Flash Article Nine</option>
<option>--------------</option>
<option Value="miscellaneouscoloredscrollbars">Miscellaneous &mdash; Colored Scrollbars</option>
<option Value="miscellaneoushtmlextension">Miscellaneous &mdash; HTML Extension</option>
<option Value="miscellaneoussubjectlines">Miscellaneous &mdash; Subject Lines</option>
</select>

If anyone knows a solution to this problem, it would be of great help.

    store the select choices in an array. here is a simple example:

    <?php
    $array = array(
    'htmlbasics' => 'HTML &mdash; Basics',
    'htmlframes' => 'HTML &mdash; Frames',
    'htmlforms' => 'HTML &mdash; Forms'
    );
    
    function form()
    {
    	global $array;
    
    echo '
    <form action="" method="POST">
    <select name="whichtutorial">
    ';
    
    foreach ($array as $key => $value)
    {
    	echo '<option value="' . $key . '"';
    	if (isset($_POST['whichtutorial']) && $_POST['whichtutorial'] == $key) {echo ' selected';}
    	echo '>' . $value . '</option>';
    }
    
    echo '	
    </select>
    <input type="submit" name="submit" value="submit">
    </form>
    ';
    }
    
    if (!isset($_POST['submit']))
    {
    	echo 'select tutorial:<br>';
    	form();
    }
    else
    {
    	echo '
    	you selected ' . $array[$_POST['whichtutorial']] . '<br>
    	(note that your selection is retianed in the form below)
    	';
    	form();
    }
    ?> 
    

      Thanks for the response. So I take it by your response storing the value of the selected item in a session won't work period? Just curious before I start digesting how this will work.

        ryanbutler wrote:

        So I take it by your response storing the value of the selected item in a session won't work period?

        you can store the form values and/or the array of selections anywhere you want as long as they are available to the form processing code.

          If the value of the drop list could be stored in a session (assuming it can be), then why go through all the hassle of creating an array? Furthermore, why wouldn't it be working then? Perhaps I'm missing something?

            ryanbutler wrote:

            If the value of the drop list could be stored in a session (assuming it can be), then why go through all the hassle of creating an array?

            the array makes it much simpler (less code) to output the form and retain the value when the form is re-displayed (which was part of your original question)

              So can it be done using the session way?

                Hi.

                It can, but there are a few obvious problems with the code you supplied;
                - You arent populating the $SESSION variable ($SESSION['selected'] = $_POST['myselection']; )
                - You must place 'session_start()' in the code to retrieve the session data, i.e. allow you to use them.
                - You need <option> tags around where you have echo'd the selection.

                  Can you define "not working?" Is it kicking back an error code or is it not storing the value for later retrieval?

                    You must place 'session_start()' in the code to retrieve the session data, i.e. allow you to use them.

                    Sorry, I am doing this, just didn't provide the code.

                    You arent populating the $SESSION variable ($SESSION['selected'] = $_POST['myselection']; )

                    Hmmm...how exactly?

                    You need <option> tags around where you have echo'd the selection.

                    How, like this?

                    <select name="myselectlist">
                    <option value="<?php echo $_SESSION['somevalue'];?>"></option>
                    </select>
                    

                    Can you define "not working?" Is it kicking back an error code or is it not storing the value for later retrieval?

                    Sure, when I click the submit button on contact.php, it directs users to tutorialscontact_result.php. If there are errors, such as lack of information, then I provide a link on tutorialscontact_result.php back to contact.php. Returning to contact.php doesn't retain the selection from the drop down list, which I'm assuming will work based on the following ideas if I can figure out how to implement them correctly.

                    This an I can't figure out my regular expression.

                      Write a Reply...