www.webdeveloper.com
Results 1 to 7 of 7

Thread: How to use a form?

  1. #1
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,703

    How to use a form?

    This is the form I'm using:
    HTML Code:
    <div id="bottom">
    	<div id="form">
    		<form action="index.php">
    			<fieldset id="rating">
    				<legend>RATING</legend>
    				<select>
    					<option>General</option>
    					<option>Mature</option>
    					<option>All</option>
    				</select>
    			</fieldset>
    
    			<fieldset id="newsite">
    				<legend>NEW SITE</legend>
    				<ul>
    					<li><div><label>Site Title</label><input type="text" class="text"></div></li>
    					<li><div><label>URL</label><input type="text" class="text"></div></li>
    					<li><div><label>Keywords</label><input type="text" class="text"></div></li>
    					<li><div><label>Mature</label><input type="checkbox" class="checkbox"></div></li>
    				</ul>
    			</fieldset>
    		</form>
    	</div>
    </div>
    In the "Rating" fieldset, I want to choose which sites my menu shows, whether it be my general links, mature links, or links to all.

    I do have these links listed in a MySQL database, including a boolean value telling whether or not I consider them "Mature"

    In the "New Site" fieldset, I want to be able to enter new sites, their keywords, URLS, Titles, and whether or not they are "mature" into my database. How do I do this with PHP?
    In y

  2. #2
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    Ok, I'll assume we're running on a MySQL database with the following credentials:
    Code:
    hostname: localhost
    username: root
    password: password
    database_name: mydb
    relative_table_name: mytbl
    Firstly, your form needs a few adjustments:
    HTML Code:
    <div id="bottom">
     <div id="form">
      <form action="index.php" method="post">
      
       <fieldset id="rating">
        <legend>RATING</legend>
         <select name="rating">
          <option value="General">General</option>
          <option value="Mature">Mature</option>
          <option value="All">All</option>
         </select>
       </fieldset>
    
       <fieldset id="newsite">
        <legend>NEW SITE</legend>
         <ul>
          <li><div><label>Site Title</label><input type="text" class="text" name="site_title"></div></li>
          <li><div><label>URL</label><input type="text" class="text" name="url"></div></li>
          <li><div><label>Keywords</label><input type="text" class="text" name="keywords"></div></li>
         </ul>
       </fieldset>
      
      </form>
     </div>
    </div>
    PHP Code:
    <?php
    /*
        filename: index.php

    */
    $mysql = array(
        
    'hostname' => 'localhost',
        
    'username' => 'root',
        
    'password' => 'password',
        
    'database_name' => 'mydb',
        
    'relative_table_name' => 'mytbl');
    $form = array(
        
    'rating',
        
    'site_title',
        
    'url',
        
    'keywords');
    $ratings = array(
        
    'General',
        
    'Mature',
        
    'All');
    $max_form_len 255;
    $error '';

    // make database connection
    if(!@mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']))
    {
        exit(
    '<strong>An unknown MySQL error has occurred: </strong><br /><pre>' mysql_errno() . ' / ' mysql_error());
    }

    // make database selection
    if(!@mysql_select_db($mysql['database_name']))
    {
        exit(
    '<strong>An unknown MySQL error has occurred: </strong><br /><pre>' mysql_errno() . ' / ' mysql_error());
    }

    // loop through post variable data
    foreach($_POST as $key => $value)
    {
        
    // if invalid variable name, discard
        
    if(!array_key_exists($key$form))
        {
            continue;
        }
        
        
    // if variable value empty, trigger error
        
    if(empty($_POST[$key]))
        {
            
    $error .= 'You must enter a ' ucwords(str_replace('_'' '$key)) . '.<br />';
            continue;
        }
        
        
    // if variable value length exceeds maximum length, trigger error
        
    if(strlen($_POST[$key]) > $max_form_len)
        {
            
    $error .= 'Your ' ucwords(str_replace('_'' '$key)) . ' exceeds the maximum allowed length. ';
            
    $error .= 'Please limit your ' ucwords(str_replace('_'' '$key)) . 'to $max_form_len characters (current length: ' strlen($_POST[$key]) . ').<br />';
            continue;
        }
        
        
    // if variable key is rating
        
    if($key 'rating')
        {
            
    // if invalid rating passed, trigger error
            
    if(!in_array($_POST[$key], $ratings))
            {
                
    $error .= 'Invalid Rating passed';
                continue;
            }
            
            
    // if rating is not mature, set as bool false, else, set as bool true
            
    if($_POST[$key] != 'Mature')
            {
                
    $_POST[$key] = false;
            }
            else
            {
                
    $_POST[$key] = true;
            }
        }
        
    // else, escape data
        
    else
        {
            
    $_POST[$key] = @mysql_real_escape_string($_POST[$key]);
        }
    }

    if(!empty(
    $error))
    {
        exit(
    $error);
    }
        
    $result = @mysql_query(<<<sql
    INSERT INTO `{$mysql['relative_table_name']}` (rating, site_title, url, keywords) 
    VALUES('
    $rating', '$site_title', '$url', '$keywords')
    sql;
    )

    if(!
    $result)
    {
        exit(
    '<strong>An unknown MySQL error has occurred: </strong><br /><pre>' mysql_errno() . ' / ' mysql_error() . "\nQuery: $result");
    }

    exit(
    'Site successfully added.');

    ?>
    This is provided untested.
    Last edited by pcthug; 10-12-2006 at 03:04 AM.

  3. #3
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,703
    Okay, I've got my page set up here, but I'm getting an error.
    This is my page so far:

    PHP Code:
    <?php

    $mysql 
    = array(
        
    'hostname'        =>    'localhost',
        
    'username'        =>    'root',
        
    'password'        =>    '',
        
    'database_name'         =>         'Lynx',
        
    'table_name'        =>    'links'
    );
    $form = array(
        
    'rating',
        
    'site_title',
        
    'url',
        
    'keywords'
    );
    $ratings = array(
        
    'General',
        
    'Mature',
        
    'All'
    );
    $max_form_len 255;
    $error='';

    //Database Connection
    if (!@mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']))
    {
        exit(
    '<strong>Connection mistake.  The database is borked: </strong> <br> <pre>' mysql_errno() . ' / ' mysql_error());
    }

    //Database selection
    if (!@mysql_select_db($mysql['database_name']))
    {
        exit(
    '<strong>Database went a-borking: </strong> <br> <pre>' mysql_errno() . ' / ' mysql_error());
    }

    //Set up the variables here
    $nl="\n";
    $keyword $_GET['keyword'];
    $result mysql_query("SELECT * FROM Links WHERE Keywords LIKE ('%$keyword%')") or die(mysql_error());

    //Loop through the post variable data
    foreach($_POST as $key => $value)
    {
        
    //if this variable is invalid, dump it
        
    if(!array_key_exists($key$form))
        {
            continue;
        }
        
        
    //If this variable is empty, squawk
        
    if (empty($_POST[$key]))
        {
            
    $error .= 'You must enter a ' ucwords(str_replace('_'' '$key)) . '.<br>';
        }
        
        
    //If variable exceeds maximum length.
        
    if (strlen($_POST[$key])>$max_form_len)
        {
            
    $error .= 'Your ' ucwords(str_replace('_'' '$key)) . ' exceeds the maximum allowed length.';
            
    $error .= 'Please limit your ' ucwords(str_replace('_'' '$key)) . ' to ' $max_form_len' characters (current length: ' strlen($_POST[$key]) . ').<br>';
            continue;
        }
        
    //If key is RATING
        
    if($key='ratings')
        {
            if(!
    in_array($_POST['key'], $ratings))
            {
                
    $error .= 'You want WHAT?';
            }
            if(
    $_POST[$key] != 'Mature')
            {
                
    $_POST[$key]=false;
            }
            else
            {
                
    $_POST[$key]=true;
            }
        }
        else
        {
            
    $_POST[$key] = @mysql_real_escape_string($_POST[$key]);
        }
    }
    if (!empty(
    $error))
    {
        exit(
    $error);
    }

    $result = @mysql_query(<<<sql
    INSERT INTO `{$mysql['table_name']}` (rating, site_title, url, keywords)
    VALUES('
    $rating', '$site_title', '$url', '$keywords')
    sql
    );

    if(!
    $result)
    {
        exit(
    '<strong>An unknown MySQL error has occurred: </strong><br /><pre>' mysql_errno() . ' / ' mysql_error() . "\nQuery: $result");
    }

    exit(
    'Site successfully added.');


    //Switch Statement sets up Header 2
    switch ($keyword)
    {
        case 
    'forum':
        
    $header="Forums";
        break;

        case 
    'furry':
        
    $header="Furry Sites";
        break;
        
        case 
    'html':
        
    $header="Website Help";
        break;
        
        case 
    'host':
        
    $header="Website Hosts";
        break;
        
        case 
    'local':
        
    $header="Local Sites";
        break;
        
        case 
    'videos':
        
    $header="Video Sites";
        break;
        
        default:
        
    $header="Choose A Catagory";
        break;
    }

    include (
    './Pages/Homepage/keywords.php');
    while(
    $row=mysql_fetch_array($result))
    {
        
    $link_list=''.$link_list.'<li><a href="http://'.$row['URL'].'"onclick="this.target=\'_blank\'">'.$row['Title'].'</a></li>'.$nl.'';
    }




    //Page created here

    echo('
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta name="Content-Style-Type" content="text/css">
    <title>Lynx Kraaikamp\'s Home Page</title>
    <link rel="stylesheet" type="text/css" href="./Pages/Homepage/style.css" media="all">
    </head>
    <body>
    <div id="all">
    <div id="top">
    <h1>Lynx Kraaikamp\'s Home Page</h1>
    </div>
    <div id="wrapper">
    <div id="content">
    '
    );

    echo(
    "<h2>$header</h2>\n");
    if (!empty(
    $keyword))
    {
        echo(
    "<ul>\n$link_list</ul>\n");
    }

    echo(
    '
    </div>
    </div>
    <div id="menu">
    <ul>
    <li><a href="./index.php?keyword=furry">Furry Sites</a></li>
    <li><a href="./index.php?keyword=local">Local Sites</a></li>
    <li><a href="./index.php?keyword=host">Website Hosts</a></li>
    <li><a href="./index.php?keyword=html">Website Help</a></li>
    <li><a href="./index.php?keyword=videos">Video Sites</a></li>
    <li><a href="./index.php?keyword=forum">Forums</a></li>
    <li id="last"><a href="about:blank" onclick="this.target=\'_blank\'">Blank Page</a></li>
    </ul>
    </div>
    <div id="bottom">
        <div id="form">
        <form action="index.php">
        <fieldset id="rating">
        <legend>RATING</legend>
        <select name="rating">
        <option value="General">General</option>
        <option value="Mature">Mature</option>
        <option value="All">All</option>
        </select>
        </fieldset>
        <fieldset id="newsite">
        <legend>NEW SITE</legend>
        <ul>
        <li><div><label>Site Title</label><input type="text" class="text" name="site_title"></div></li>
        <li><div><label>URL</label><input type="text" class="text" name="url"></div></li>
        <li><div><label>Keywords</label><input type="text" class="text" name="keywords"></div></li>
        <li><div><label>Mature</label><input type="checkbox" class="checkbox" name="Mature"></div></li>
        </ul>
        </fieldset>
        </form>
        </div>
    </div>
    </div>
    </body>
    </html>
    '
    );
    ?>

    Below is my error:

    An unknown MySQL error has occurred:

    1054 / Unknown column 'rating' in 'field list'
    Query:

  4. #4
    Join Date
    Feb 2006
    Location
    Where I live, people get shot. I need to move.
    Posts
    634
    check for typos.

    Your database may not contain a reference to 'rating', it could be 'ratings' for example. They only way you will know for sure is to get the structure of that table that your expecting to find 'rating' in.

    If the reference does not exist, you could always modify the database to add in another field.
    If life gives you a hard knock once in a while, your lucky. All I keep getting is a savage beating... everyday!
    AJAX! - Javascript Sha1 & MD5 - ASCII tables - Zend DevZone - My Space
    Stop Badware - Create "TEXT FILE" in Javascript - . - wikipedia on PHP -

  5. #5
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,703
    Okay, thanks. But now I've got another problem. Here's my code now:

    PHP Code:
    <?php

    $mysql 
    = array(
        
    'hostname'        =>    'localhost',
        
    'username'        =>    'root',
        
    'password'        =>    '',
        
    'database_name'    =>    'Lynx',
        
    'table_name'    =>    'links'
    );
    $form = array(
        
    'Title',
        
    'URL',
        
    'Keywords',
        
    'Mature'
    );
    $ratings = array(
        
    'General',
        
    'Mature',
        
    'All'
    );
    $max_form_len 255;
    $error='';

    //Database Connection
    if (!@mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']))
    {
        exit(
    '<strong>Connection mistake.  The database is borked: </strong> <br> <pre>' mysql_errno() . ' / ' mysql_error());
    }

    //Database selection
    if (!@mysql_select_db($mysql['database_name']))
    {
        exit(
    '<strong>Database went a-borking: </strong> <br> <pre>' mysql_errno() . ' / ' mysql_error());
    }

    //Set up the variables here
    $nl="\n";
    $keyword $_GET['keyword'];
    $result mysql_query("SELECT * FROM Links WHERE Keywords LIKE ('%$keyword%')") or die(mysql_error());

    //Loop through the post variable data
    foreach($_POST as $key => $value)
    {
        
    //if this variable is invalid, dump it
        
    if(!array_key_exists($key$form))
        {
            continue;
        }
        
        
    //If this variable is empty, squawk
        
    if (empty($_POST[$key]))
        {
            
    $error .= 'You must enter a ' ucwords(str_replace('_'' '$key)) . '.<br>';
        }
        
        
    //If variable exceeds maximum length.
        
    if (strlen($_POST[$key])>$max_form_len)
        {
            
    $error .= 'Your ' ucwords(str_replace('_'' '$key)) . ' exceeds the maximum allowed length.';
            
    $error .= 'Please limit your ' ucwords(str_replace('_'' '$key)) . ' to ' $max_form_len' characters (current length: ' strlen($_POST[$key]) . ').<br>';
            continue;
        }
        
    //If key is RATING
        
    if($key='ratings')
        {
            if(!
    in_array($_POST['key'], $ratings))
            {
                
    $error .= 'You want WHAT?';
            }
            if(
    $_POST[$key] != 'Mature')
            {
                
    $_POST[$key]=false;
            }
            else
            {
                
    $_POST[$key]=true;
            }
        }
        else
        {
            
    $_POST[$key] = @mysql_real_escape_string($_POST[$key]);
        }
    }
    if (!empty(
    $error))
    {
        exit(
    $error);
    }

    $result = @mysql_query(<<<sql
    INSERT INTO `{$mysql['table_name']}` (Title, URL, Keywords, Mature)
    VALUES('
    $rating', '$site_title', '$url', '$keywords')
    sql
    );

    if(!
    $result)
    {
        exit(
    '<strong>An unknown MySQL error has occurred: </strong><br /><pre>' mysql_errno() . ' / ' mysql_error() . "\nQuery: $result");
    }

    exit(
    'Site successfully added.');


    //Switch Statement sets up Header 2
    switch ($keyword)
    {
        case 
    'forum':
        
    $header="Forums";
        break;

        case 
    'furry':
        
    $header="Furry Sites";
        break;
        
        case 
    'html':
        
    $header="Website Help";
        break;
        
        case 
    'host':
        
    $header="Website Hosts";
        break;
        
        case 
    'local':
        
    $header="Local Sites";
        break;
        
        case 
    'videos':
        
    $header="Video Sites";
        break;
        
        default:
        
    $header="Choose A Catagory";
        break;
    }

    include (
    './Pages/Homepage/keywords.php');
    while(
    $row=mysql_fetch_array($result))
    {
        
    $link_list=''.$link_list.'<li><a href="http://'.$row['URL'].'"onclick="this.target=\'_blank\'">'.$row['Title'].'</a></li>'.$nl.'';
    }




    //Page created here

    echo('
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta name="Content-Style-Type" content="text/css">
    <title>Lynx Kraaikamp\'s Home Page</title>
    <link rel="stylesheet" type="text/css" href="./Pages/Homepage/style.css" media="all">
    </head>
    <body>
    <div id="all">
    <div id="top">
    <h1>Lynx Kraaikamp\'s Home Page</h1>
    </div>
    <div id="wrapper">
    <div id="content">
    '
    );

    echo(
    "<h2>$header</h2>\n");
    if (!empty(
    $keyword))
    {
        echo(
    "<ul>\n$link_list</ul>\n");
    }

    echo(
    '
    </div>
    </div>
    <div id="menu">
    <ul>
    <li><a href="./index.php?keyword=furry">Furry Sites</a></li>
    <li><a href="./index.php?keyword=local">Local Sites</a></li>
    <li><a href="./index.php?keyword=host">Website Hosts</a></li>
    <li><a href="./index.php?keyword=html">Website Help</a></li>
    <li><a href="./index.php?keyword=videos">Video Sites</a></li>
    <li><a href="./index.php?keyword=forum">Forums</a></li>
    <li id="last"><a href="about:blank" onclick="this.target=\'_blank\'">Blank Page</a></li>
    </ul>
    </div>
    <div id="bottom">
        <div id="form">
        <form action="index.php">
        <fieldset id="rating">
        <legend>RATING</legend>
        <select name="rating">
        <option value="General">General</option>
        <option value="Mature">Mature</option>
        <option value="All">All</option>
        </select>
        </fieldset>
        <fieldset id="newsite">
        <legend>NEW SITE</legend>
        <ul>
        <li><div><label>Site Title</label><input type="text" class="text" name="site_title"></div></li>
        <li><div><label>URL</label><input type="text" class="text" name="url"></div></li>
        <li><div><label>Keywords</label><input type="text" class="text" name="keywords"></div></li>
        <li><div><label>Mature</label><input type="checkbox" class="checkbox" name="Mature"></div></li>
        </ul>
        </fieldset>
        </form>
        </div>
    </div>
    </div>
    </body>
    </html>
    '
    );
    ?>
    All I see is "Site Successfully added." :-S

  6. #6
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,703
    *Bumps the thread.*

  7. #7
    Join Date
    Feb 2006
    Location
    Where I live, people get shot. I need to move.
    Posts
    634
    This might not help
    PHP Code:
    if($key='ratings'
    you may want to '==' and not assign the value that your looking to test.
    If life gives you a hard knock once in a while, your lucky. All I keep getting is a savage beating... everyday!
    AJAX! - Javascript Sha1 & MD5 - ASCII tables - Zend DevZone - My Space
    Stop Badware - Create "TEXT FILE" in Javascript - . - wikipedia on PHP -

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles