Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'spaghetti code'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. When I am developing an application for a client, I run into situations where I have to engineer what seems to be the best idea that I can come up with at the time. Of course that is the whole game right? I work alone and have no other programmer buddies to bounce a code idea off of, so I wing it and hope for the best. Recently an application specification calls for a "row of buttons" across the top of an admin page that also has tabbed "main navigation" links to non-admin application stuff. The idea is to make it "so simple a caveman could do it" by seeing the desired functionality at a glance and clicking a button to refresh the page via ajax to get the desired content. This is where the question comes in. I started with a jquery button click handler for each button that contained the associated ajax call. Ten buttons meant 10 separate click functions with an anonymous ajax function call. It seemed like way too much code to accomplish the mission with a lot of places for things to go wrong. It also did not at all adhere to the DRY axiom one bit. So I am trying something to streamline the process. Here is the HTML for the buttons: //All button ids will correspond to the associated action called in the jquery click handler echo'<div class="admin_buttons_div"> <button id="get_user_table" class="button">View Users</button> <button id="get_dmd_info" class="button">Get DMD Report</button> <button id="get_dmd_entry" class="button">DMD Entry</button> <button id="button_4" class="button">button_4</button> <button id="button_5" class="button">button_5</button> <button id="button_6" class="button">button_6</button> </div>'; //This is our place holder div to contain our ajax responses echo'<div class="placeholder_div"></div>'; Here is the jquery code: //admin_dashboard.js $(document).ready(function(){ $('.button').on('click',function(evt){ //Get a reference to the button var button = $(this); //Get the clicked button's default text var initial_text = $(this).text(); //Get the button's id which is also the php $_POST['action'] variable in action.php var action = $(this).attr('id'); //Button was clicked...give the user some visual feedback processing(button); //Set the action variable for the called php script - action.php var data = 'action=' + action; //Send the ajax data request to the action.php script $.post('modules/action.php', data, function(ajax_response){ //ajax response returns whatever the called script is written to return $('.placeholder_div').html(ajax_response); //Set the button text back to it's default value $(button).text(initial_text); });//$.post });//End click function //Give the user some visual feedback after a button click function processing(button){ $(button).text('Processing...'); } //Reset the button text to the default initial text function done_processing(initial_text){ $(button).text(initial_text); } });//ready The data sent in the ajax function is the button's id and is received by action.php as a post variable that is used in a switch to select either a php function or a php script that returns a response and populates the the place holder div with whatever the clicked button was supposed to do. Here is the scaled down relevant php code: include('../includes/mysqli_connect.inc.php'); $action = isset($_POST['action']) ? $_POST['action'] : NULL; // Determine what action to take based on $action sent by js click function switch ($action) { case 'get_user_table': echo display_users(); break; case 'get_dmd_info': display_dmd_info(); break; case 'get_dmd_entry': display_dmd_form(); break; // Default is to include the main page. default: $page = 'main.inc.php'; $page_title = 'Site Home Page'; break; }//end switch ?> I actually have many more cases to the switch because it is acting as a controller for all of my ajax function calls. The code works very well and seems real easy to add buttons, cases and functions to achieve the desired functionality. The user clicks the button, gets visual feedback via button text changing to "Processing..." then the content is updated and the button text reverts back to the original value. Seems like a good idea to me. The main application framework is built on the concepts presented in Chapter 2 "Modularizing a Web Site" with a MVC flavor tossed in all coded in a procedural fashion. So...Is one programmer's clever idea another programmer's spaghetti code? Is this a bad idea? Thanks for any suggestions or opinions.
×
×
  • Create New...