Solved

Make a sticky menu server behavior?

Posted on 2011-09-02
7
441 Views
Last Modified: 2012-08-14
I have been working through a project to create a form to collected data for internal incidents, and would like to enable my fields to retain data during validation. I have created fields in and enabled sticky text field behavior as David Powers shows in his Essential guide to Dreamweaver CS4, however, I have been unsuccessful in creating sticky behavior on drop down menu/list.

Here is my drop down code:

 

<select name="remediation_status">     

     <option value="" selected="selected">--PLEASE CHOOSE--</option>     

     <?php
        do {  
     ?>
     <option value="<?php echo $row_selectStatus['name']; 
              if (isset($_POST['remediation_status']) && 
              $_POST['remediation_status'] == $row_selectStatus['name']) {
               echo 'selected="selected"';
              }?>"><?php echo $row_selectStatus['name']?></option>
                                                    <?php
             } while ($row_selectStatus = mysql_fetch_assoc($selectStatus));
               $rows = mysql_num_rows($selectStatus);
               if($rows > 0) {
                mysql_data_seek($selectStatus, 0);
                $row_selectStatus = mysql_fetch_assoc($selectStatus);
               }
             ?>

</select>

Open in new window



So I have a 2 part question. What is the proper way to format a sticky menu server behavior so I can add it as a snippet?

and 2, can someone point out what i'm missing in my sticky menu to make it retain the selection?

 

Thank you all very much for your help.

Chris
0
Comment
Question by:udsfsg
  • 3
  • 3
7 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 36473173
Chris: There are a lot of moving parts to your question, and I will try to respond to some of them with some examples and discussion of the standard design patterns.  First, look at the behavior of this little demonstration script, and see the code in the code snippet.
http://www.laprbass.com/RAY_form_highlight_errors.php

When you run that you will see that it keeps the values in the input control boxes, whether they are correct or not.  And it highlights errors.  You might want to eliminate the inputs that are invalid.  To do that you would activate the code on lines 34 and 43.
<?php // RAY_form_highlight_errors.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO HIGHLIGHT ERRORS IN FORM INPUT
// CLIENT IS ASKED TO PUT IN A VALUE
// IF THE VALUE FAILS OUR TEST WE SHOW AN ERROR MESSAGE
// WE PUT A MARKER NEXT TO THE INPUT CONTROL ON THE FORM
// WE TURN THE FORM BORDER RED
// SEE http://www.w3schools.com/CSS/pr_class_visibility.asp


// THESE CONDITIONS ARE SET FOR THE INITIAL ENTRY
$error_abc = 'hidden';
$boxer_abc = 'black';
$error_xyz = 'hidden';
$boxer_xyz = 'black';
$error_any = 'hidden';


// CAPTURE AND NORMALIZE THE POST VARIABLES - ADD YOUR OWN SANITY CHECKS HERE
$abc = (isset($_POST["abc"])) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = (isset($_POST["xyz"])) ? trim(strtoupper($_POST["xyz"])) : NULL;

// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
    // VALIDATE THE 'abc' FIELD
    if ($abc != 'ABC')
    {
        $error_any = 'visible';
        $error_abc = 'visible';
        $boxer_abc = 'red';
        // $abc       = NULL;
    }

    // VALIDATE THE 'xyz' FIELD
    if ($xyz != 'XYZ')
    {
        $error_any = 'visible';
        $error_xyz = 'visible';
        $boxer_xyz = 'red';
        // $xyz       = NULL;
    }

    // DO WE HAVE INPUT FREE FROM ANY ERRORS?
    if ($error_any != 'visible')
    {
        echo "CONGRATULATIONS";
        die();
    }

    // OOPS - WE HAVE ERRORS
}

// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input style="border-color:$boxer_abc;" name="abc" value="$abc" />
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input style="border-color:$boxer_xyz;" name="xyz" value="$xyz" />
<input type="submit" />
</form>
ENDFORM;

// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;

Open in new window

Normally, a sticky form has its foundation in a data base table (or tables) and the form is prepopulated from the data base.  If there is no matching record in the data base, the form is empty.  When the client fills in the form and submits the form, an action script (HTTP action, not actionscript) is run.  The action script validates the client input and puts all of it into the data base.  If any of the validation fails, the action script puts the form back on the screen for the client to correct the errors.  Lather, rinse, repeat.

There is a unique problem with checkbox and radio control buttons.  They are fundamentally different from input controls that are text, hidden, or submit, in that they do not appear at all in the POST array if the client did not check them.  Thus you must know exactly what to expect in these controls in advance of processing the form.  This article shows a little more about how to handle checkboxes.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

Drop-down select lists are usually handled with a default value marked SELECTED.  If the client changes the selection, you can validate the changed selection against your expected value.  If the client has not made a selection, the POST input will still contain the default value.  For multiple selections, you would use something like what is shown in this code snippet.  The snippet allows you to choose years.  The default value is the current year.  Try running it and seeing how it works.  Note on line 17 we have the name of the select control shown with [] at the end.  This creates an array in $_POST['year']; that array contains the default selection or the selection(s) from the client.
http://www.laprbass.com/RAY_temp_uds.php
<?php // RAY_temp_uds.php
error_reporting(E_ALL);

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// SHOW THE INPUT IF ANY
var_dump($_POST);

// SET THE BOUNDARIES FOR THE YEARS
$starting_year = date('Y', strtotime('-3 YEARS'));
$future_year   = date('Y', strtotime('+3 YEARS'));
$current_year  = date('Y');

// CREATE THE FORM
echo '<form method="post">' . PHP_EOL;
echo '<select name="year[]" multiple="multiple">' . PHP_EOL;

// USE AN ITERATOR TO COUNT UP THE YEARS
while ($starting_year <= $future_year)
{
    // INITIALIZE A NULL STRING
    $s = NULL;

    // IF THE YEAR IS EQUAL TO THE CURRENT YEAR, MARK IT SELECTED
    if ($starting_year == $current_year) $s = 'selected';

    // WRITE THE OPTION TAG
    echo "<option value=\"$starting_year\" $s>$starting_year</option>" . PHP_EOL;

    // ADD ONE TO THE YEAR
    $starting_year ++ ;
}

// COMPLETE THE FORM
echo '</select>' . PHP_EOL;
echo '<input type="submit" />' . PHP_EOL;
echo '</form>' . PHP_EOL;

Open in new window

Please let me suggest a good learning resource for you.  Buy this very readable book and give yourself some time to work through the excellent examples.  Trying to learn PHP programming by studying Dreamweaver code is like trying to learn cooking by studying $hit.  It's just the wrong starting place.  The SitePoint book will give you a much better foundation, I promise!
http://www.sitepoint.com/books/phpmysql4/

Best regards, and good luck with your project, ~Ray
0
 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 250 total points
ID: 36475217
greetings udsfsg,  I looked at your  PHP do - while loop for accessing all of the rows in your MySQL select return of  $selectStatus. It looks like a real bad choice for a row access loop. Most all of the example code for row access of a select return uses a WHILE loop like this -

while ($row = mysql_fetch_assoc($selectStatus)) {
    echo $row['name'];
    echo $row["other"];
    echo $row['data'];
}

in that WHILE loop aboveyou do not need this code -
$rows = mysql_num_rows($selectStatus);
    if($rows > 0) {
        mysql_data_seek($selectStatus, 0);

Next, I do NOT understand your programming Logic when you have this test -
if (isset($_POST['remediation_status']) &&
    $_POST['remediation_status'] == $row_selectStatus['name']) {
        echo 'selected="selected"';

? ? ? you do not seem to want a multi - select option box, if that is true you will need to a test for a database select returned of that name BEFORE you start writing (echo) your options in the select. . .

maybe with some thing like the code below
the code below is UNTESTED, and may have errors in it!
I hope it give you some Ideas about how to use some steps to get what you want in the options.
ask questions if you need more info.




<?php
$PreSelect = false;

if (isset($_POST['remediation_status'])) {
$PreSelect = $_POST['remediation_status'];
$sql = "SELECT id
        FROM   sometable
        WHERE  name = '$PreSelect'";
$result = mysql_query($sql);
if (!$result) {
$PreSelect = false;
} else
    {
    if (mysql_num_rows($result) < 1) $PreSelect = false;
    }
}
?>
<select name="remediation_status">
<?php
if ($PreSelect) echo '<option value="" selected="selected">--PLEASE CHOOSE--</option> ';
    elae echo '<option value="0">--PLEASE CHOOSE--</option> ';

while ($row_selectStatus = mysql_fetch_assoc($selectStatus)) {
    echo '<option value="'.$row_selectStatus['name'].'" '; 
    if ($PreSelect == $row_selectStatus['name']) echo 'selected="selected"';
    echo '>'.$row_selectStatus['name'].'</option>;
    }

Open in new window

0
 
LVL 33

Expert Comment

by:Slick812
ID: 36475230
sorry, I see now that the line
if ($PreSelect)

should be

if (!$PreSelect)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 33

Expert Comment

by:Slick812
ID: 36475238
and
echo '>'.$row_selectStatus['name'].'</option>;

should be
echo '>'.$row_selectStatus['name'].'</option>';
0
 
LVL 2

Author Comment

by:udsfsg
ID: 36488914
Hi All,
Just an update on this. I was able to figure out the solution with your help.
I removed some of the erronious code that Slick812 pointed out, and simply had to close the para tag in my if statement.
Here is a before, and a after code block to show the differences between my old-bang-my-head-against-pavement-on-a-friday-afternoon vs my Tuesday after a long break version. :-)

Old Busted
<select name="remediation_status">
	<option value="" selected="selected">--PLEASE CHOOSE--</option>
	<?php
	do {  
	?>
	<option value="<?php echo $row_selectStatus['name']; 
	if (isset($_POST['remediation_status']) && 
	$_POST['remediation_status'] == $row_selectStatus['name']) {
	echo 'selected="selected"';
	}?>"><?php echo $row_selectStatus['name']?></option>
	<?php
	} while ($row_selectStatus = mysql_fetch_assoc($selectStatus));
	$rows = mysql_num_rows($selectStatus);
	if($rows > 0) {
	mysql_data_seek($selectStatus, 0);
	$row_selectStatus = mysql_fetch_assoc($selectStatus);
	}
	?>
</select>

Open in new window


New Hawtness
<select name="remediation_status">
	<option value="" selected="selected">--PLEASE CHOOSE--</option>
	<?php
	do {  
	?>
	<option value="<?php echo $row_selectStatus['name']; 
	if (isset($_POST['remediation_status']) && 
	$_POST['remediation_status'] == $row_selectStatus['name']) {
	echo '" selected="selected"';
	}?>"><?php echo $row_selectStatus['name']?></option>
	<?php } while ($row_selectStatus = mysql_fetch_assoc($selectStatus)); ?>
</select>

Open in new window



Thanks all!
0
 
LVL 2

Author Comment

by:udsfsg
ID: 36488931
Ray,
I will definately check out that book. Thanks for the link.
0
 
LVL 2

Author Closing Comment

by:udsfsg
ID: 36488945
Thank's for the links guys. I will give those a read, I knew I was close to a solution, I really needed a second set of eye to help point out the solution.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress syntax error 1 29
MySQL database data submission 7 40
Apostophes in PHP generated form 6 20
Filter wordpress query 3 13
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now