SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CMS add/insert data forms of the same kind?

    Hello All,

    Wondering if someone could point me in the right direction. I build various CMS pages to adding/inserting data into a mysql database like so:

    PHP Code:
    <?php

    // If submit set the variables    
    if (isset($_POST['Submit'])) 

       
    $errors                = array(); 
       
    $question               cleanText($_POST['question']);
       
    $teaser                 cleanText($_POST['teaser']);
       
    $main_text            cleanText($_POST['main_text']);
       
    //Start errors note: ending } way down the page  
       
       
    if ($question === ''
       { 
          
    $errors[] = "<img src='../images/error.png' class='align'> You forgot to enter a question for this competition.<br />"
       } 
       
       if (
    $teaser === ''
       { 
          
    $errors[] = "<img src='../images/error.png' class='align'> You forgot to enter the opening teaser paragraph.<br />"
       }   
       
       if (
    $main_text === ''
       { 
          
    $errors[] = "<img src='../images/error.png' class='align'> You forgot to enter any text.<br />"
       }   

    // If errors is equal to zero then set the image details and insert into the database!
       
    if(count($errors) == 0
       { 
          
    $result mysql_query("Insert into tble (question,teaser,main_text) 
                                 values ('
    $question', '$teaser', '$main_text')") or die(mysql_error());
          print 
    "You have uploaded everything correctly.";
      } 
       else 
       { 
          print 
    "<span style='color:#c00'>" implode(' '$errors) . "</span>"
       } 

    ?>
    <body>

      <form id="webform" action="" method="post" name="webform">
        <h1>Add data</h1>
        <p class="indentation"><span class="asterix">*</span> Please note that an asterix Indicates a mandatory field:</p>
        
    <!-- Question-->
        <label>Question:
        <span class="small">Please enter the Question</span>
        </label>
        <input name="question" class="big" type="text" id="question" style="width: 300px" value="<?php echo stripslashes($question);?>" />
         <div class="spacer"></div> 
        
    <!-- Teaser -->
        <label>Teaser:
        <span class="small">Please enter the teaser</span>
        </label>
        <textarea name="teaser" class="big" id="teaser" cols="60" rows="4"><?php echo stripslashes($teaser);?></textarea>
        <span class="warning">*</span>
         <div class="spacer"></div> 
        
    <!-- Story -->
        <label>Story:
        <span class="small">Please enter the story</span>
        </label>
        <textarea name="main_text" class="big" id="main_text" cols="80" rows="20"><?php echo stripslashes($main_text);?></textarea>
        <span class="warning">*</span>
         <div class="spacer"></div> 
        
        
    <!-- Submit --> 
        <button  name="Submit" type="submit" value="Submit">Submit</button>
        <button type="submit">Reset</button>


      </form>
      </body>
      </html>
    Now each time I build a new page for adding/inserting data into a mysql database it's always pretty much like the layout of the form above - yet each time I create a new page and change the variables and names of the textboxes to suit.

    So what i'm wondering/thinking is there must be a better way to do this? Surely i'm making life hard for myself. Is there some way of buiding some kind of function to create a form where I could pass in various parameters, or some kind of form templating system I could conform to?

    Tried googing a few terms, but don't know the correct termonology for what exactly this would be called. Would anyone be able to point me in the direction of anything that could help me here?

    Thanks

  2. #2
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vim is great for tedious text editing. Alternatively, you could always make a form that generates the script.
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •