Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
| Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 22nd, 2006, 02:06 PM
Authorized User
 
Join Date: Nov 2005
Location: UTTARKASHI, , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default form validation and processing

Hi, I'm new to PHP and to this forum.
I'd like to clear one doubt. I've two php pages and I am unable to locate the error
page1
<?php
//connect to MySQL; note we have used our own parameters- you should use
//your own for hostname, user, and password
$connect = mysql_connect("localhost", "root", "pass") or
     die ("Hey loser, check your server connection.");

//create the main database if it doesn't already exist
$create = mysql_query("CREATE DATABASE IF NOT EXISTS registration")
  or die(mysql_error());

//make sure our recently created database is the active one
mysql_select_db("registration");


//create "user_info" table
$user_info = "CREATE TABLE user_info (
    userName varchar(255) NOT NULL,
    password varchar(10) NOT NULL,
    gender varchar(8) NOT NULL,
    hobby varchar(20) default NULL,
    interest varchar(50) default NULL,
    subInterestA varchar(50) default NULL,
    subInterestB varchar(50) default NULL,

    id int(10) NOT NULL,
)";

$results = mysql_query($user_info)
  or die(mysql_error());

$insert = "INSERT INTO admin( userName , password , gender , admin_level, id)".
                    "VALUES('johndoe' , 'john doe', 'john@johndoe.com' , PASSWORD('jane') , 'male' , '1' ,'')".
                    "('janedoe' , 'jane doe' , 'jane@jane.doe' , PASSWORD('john') , 'female' , '2' , '')".
$results = mysql_query($insert)
  or die(mysql_error());
echo "Registration Database successfully created!";

?>
page2
<?php
session_start();
ob_start();
include "conn.inc.php";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="description" content=" ">
<meta name="keywords" content=" ">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>form</title>
<style type="text/css">
#frm{
    position: absolute;
    left: 141px;
    top: 150px;
    width: 300px;
    height: 250px}
.frm002{width: 115px}
</style>
<script language="javascript">
var subInterestsA = Object();
subInterestsA['Computer'] = '|Hardware|Software|Networking|Multimedia|Troubles hooting|Other';
subInterestsA['Other'] = '|Other';

var subInterestsB = Object();
subInterestsB['Hardware']='|Processor|Motherboard|Monitor|hdd|fdd|cd|keybor d|ups|modem|Other';
subInterestsB['Software']='|cpp|java|vb|vb.net|vb script|java script|asp|jsp|php|jsf|Other';
subInterestsB['Networking']='|lan|wan|Other';
subInterestsB['Multimedia']='|movies|game|mixing|Other';
subInterestsB['Troubleshooting']='|cpu trouble|monitor trouble|soft trouble|Other';
subInterestsB['Other']='|Other';
function setInterests()
{
    for (interest in subInterestsA)
        document.write('<option value="' + interest + '">' + interest + '</option>');
}

function set_subInterestA(oInterestSel, oSubInterestASel, oSubInterestBSel)
{
    var subInterestAArr;
    oSubInterestASel.length = 0;
    oSubInterestBSel.length = 0;
    var interest = oInterestSel.options[oInterestSel.selectedIndex].text;
    if (subInterestsA[interest])
    {
        oSubInterestASel.disabled = false;
        oSubInterestBSel.disabled = true;
        oSubInterestASel.options[0] = new Option('SELECT INTEREST','');
        subInterestAArr = subInterestsA[interest].split('|');
        for (var i = 0; i < subInterestAArr.length; i++)
            oSubInterestASel.options[i + 1] = new Option(subInterestAArr[i], subInterestAArr[i]);
    }
    else oSubInterestASel.disabled = true;
}
function set_subInterestB(oSubInterestASel, oSubInterestBSel)
{
    var subInterestBArr;
    oSubInterestBSel.length = 0;
    var subInterestA = oSubInterestASel.options[oSubInterestASel.selectedIndex].text;
    if (subInterestsB[subInterestA])
    {
        oSubInterestBSel.disabled = false;
        oSubInterestBSel.options[0] = new Option('SELECT INTEREST','');
        subInterestBArr = subInterestsB[subInterestA].split('|');
        for (var i = 0; i < subInterestBArr.length; i++)
            oSubInterestBSel.options[i+1] = new Option(subInterestBArr[i],subInterestBArr[i]);
    }
    else oSubInterestBSel.disabled = true;
}

function print_subInterestB(oSubInterestASel, oSubInterestBSel)
{
    var subInterestA = oSubInterestASel.options[oSubInterestASel.selectedIndex].text;
    var subInterestB = oSubInterestBSel.options[oSubInterestBSel.selectedIndex].text;
}

</script>
</head>
<body>
<?php
if (isset($_POST['submit']) && $_POST['submit'] == "Register") {
  if ($_POST['username'] != "" &&
      $_POST['password'] != "" &&
      $_POST['gender'] != "" &&
      $_POST['hobby'] != "" &&
      $_POST['interest'] != "" &&
      $_POST['subInterestA'] != "" &&
      $_POST['subInterestB'] != "") {

    $query = "SELECT username FROM user_info " .
             "WHERE username = '" . $_POST['username'] . "';";
    $result = mysql_query($query)
      or die(mysql_error());

    if (mysql_num_rows($result) != 0) {
?>

  <b>The Username,
  <?php echo $_POST['username']; ?>, is already in use, please choose
  another!</b>
<form id="frm" method="POST" action="register.php" autocomplete="off">
    <table border="1" width="100%" id="table1">
        <tr>
            <td class="frm002">Password:</td>
            <td><input type="text" name="username" value="<?php echo $_POST['password']; ?>" size="20"></td>
        </tr>
        <tr>
            <td class="frm002">Gender:</td>
            <td><input type="radio" value="<?php echo $_POST['male']; ?>" name="gender">Male<input type="radio" name="gender" value="<?php echo $_POST['female']; ?>">Female</td>
        </tr>
        <tr>
            <td class="frm002">Hobby:</td>
            <td><select size="1" name="hobby">
            <option value="<?php echo $_POST['hobby']; ?>">select hobby</option>
            <option value="sports">sports</option>
            <option value="reading">reading</option>
            <option value="movies">movies</option>
            <option value="songs">songs</option>
            <option value="writing">writing</option>
            <option value="yoga">yoga</option>
            </select></td>
        </tr>
        <tr>
            <td class="frm002">Interest:</td>
            <td>
                  <select onchange="set_subInterestA(this,subInterestA,subIn terestB)" size="1" name="interest">
                <option>SELECT INTEREST</option>
                <option value="<?php echo $_POST['interest']; ?>"></option>
                <script type="text/javascript">
                setInterests(this);
                </script>
                </select></td>
        </tr>
        <tr>
            <td class="frm002" rowspan="2">&nbsp;</td>
            <td><select size="1" name="subInterestA" disabled="disabled" onchange="set_subInterestB(this,subInterestB)">
            <option value="<?php echo $_POST['subInterestA']; ?>">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
        <tr>
            <td><select size="1" name="subInterestB" disabled="disabled" onchange="print_subInterestB(subInterestA,this)">
            <option value="<?php echo $_POST['subInterestB']; ?>">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
    </table>

            <input type="checkbox" name="C1" value="ON">
            I affirm that I accept terms and condition.

    <p><input type="submit" value="register" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<?php
    } else {
      $query = "INSERT INTO user_info (username, gender, hobbies) " .
               "VALUES ('" . $_POST['username'] . "',
               '" . $_POST['password'] . "',
                '" . $_POST['gender'] . "',
               '" . $_POST['hobby'] . "',
               '" . $_POST['interest'] . "',
               '" . $_POST['subInterestA'] . "',
               '" . $_POST['subInterestB'] . "');";
      $result = mysql_query($query)
        or die(mysql_error());
      $_SESSION['user_logged'] = $_POST['username'];
      $_SESSION['user_password'] = $_POST['password'];
?>
<p>
  Thank you, <?php echo $_POST['username']; ?> for registering!<br>
<?php
      header("Refresh: 5; URL=index.php");
      echo "Your registration is complete! " .
           "You are being sent to the page you requested!<br>";
      echo "(If your browser doesn't support this, " .
           "<a href=\"index.php\">click here</a>)";
      die();
    }
  } else {
?>
  <b>All fields are required!</b>
<form id="frm" method="POST" action="register.php" autocomplete="off">
    <table border="1" width="100%" id="table1">
        <tr>
            <td class="frm002">User Name:</td>
            <td><input type="text" name="username" value="<?php echo $_POST['username']; ?>" size="20"></td>
        </tr>
        <tr>
            <td class="frm002">Password:</td>
            <td><input type="password" name="password" value="<?php echo $_POST['password']; ?>" size="20"></td>
        </tr>

        <tr>
            <td class="frm002">Gender:</td>
            <td><input type="radio" value="<?php echo $_POST['male']; ?>" name="gender">Male<input type="radio" name="gender" value="<?php echo $_POST['female']; ?>">Female</td>
        </tr>
        <tr>
            <td class="frm002">Hobby:</td>
            <td><select size="1" name="hobby">
            <option value="<?php echo $_POST['hobby']; ?>">select hobby</option>
            <option value="sports">sports</option>
            <option value="reading">reading</option>
            <option value="movies">movies</option>
            <option value="songs">songs</option>
            <option value="writing">writing</option>
            <option value="yoga">yoga</option>
            </select></td>
        </tr>
        <tr>
            <td class="frm002">Interest:</td>
            <td>
                  <select onchange="set_subInterestA(this,subInterestA,subIn terestB)" size="1" name="interest">
                <option>SELECT INTEREST</option>
                <option value="<?php echo $_POST['interest']; ?>"></option>
                <script type="text/javascript">
                setInterests(this);
                </script>
                </select></td>
        </tr>
        <tr>
            <td class="frm002" rowspan="2">&nbsp;</td>
            <td><select size="1" name="subInterestA" disabled="disabled" onchange="set_subInterestB(this,subInterestB)">
            <option value="<?php echo $_POST['subInterestA']; ?>">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
        <tr>
            <td><select size="1" name="subInterestB" disabled="disabled" onchange="print_subInterestB(subInterestA,this)">
            <option value="<?php echo $_POST['subInterestB']; ?>">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
    </table>

            <input type="checkbox" name="C1" value="ON">
            I affirm that I accept terms and condition.

    <p><input type="submit" value="register" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<?php
  }
} else {
?>
<form id="frm" method="POST" action="register.php" autocomplete="off">
    <table border="1" width="100%" id="table1">
        <tr>
            <td class="frm002">User Name:</td>
            <td><input type="text" name="username" size="20"></td>
        </tr>
        <tr>
            <td class="frm002">Password:</td>
            <td><input type="password" name="password" size="20"></td>
        </tr>

        <tr>
            <td class="frm002">Gender:</td>
            <td><input type="radio" value="male" name="gender">Male<input type="radio" name="gender" value="female">Female</td>
        </tr>
        <tr>
            <td class="frm002">Hobby:</td>
            <td><select size="1" name="hobby">
            <option value="hobby">select hobby</option>
            <option value="sports">sports</option>
            <option value="reading">reading</option>
            <option value="movies">movies</option>
            <option value="songs">songs</option>
            <option value="writing">writing</option>
            <option value="yoga">yoga</option>
            </select></td>
        </tr>
        <tr>
            <td class="frm002">Interest:</td>
            <td>
                  <select onchange="set_subInterestA(this,subInterestA,subIn terestB)" size="1" name="interest">
                <option>SELECT INTEREST</option>
                <option value="interest"></option>
                <script type="text/javascript">
                setInterests(this);
                </script>
                </select></td>
        </tr>
        <tr>
            <td class="frm002" rowspan="2">&nbsp;</td>
            <td><select size="1" name="subInterestA" disabled="disabled" onchange="set_subInterestB(this,subInterestB)">
            <option value="subInterestA">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
        <tr>
            <td><select size="1" name="subInterestB" disabled="disabled" onchange="print_subInterestB(subInterestA,this)">
            <option value="subInterestB">&lt;--------------------------&gt;</option>
            </select></td>
        </tr>
    </table>

            <input type="checkbox" name="C1" value="ON">
            I affirm that I accept terms and condition.



    <p><input type="submit" value="register" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<?php
}
?>

</body>

</html>

  #2 (permalink)  
Old April 22nd, 2006, 03:07 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what is the error ?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Get user input during form processing echovue PHP How-To 3 June 10th, 2006 11:54 PM
PROBLEM IN FORM PROCESSING GOUR CHANDRA PAUL PHP Databases 0 April 22nd, 2006 01:18 PM
Suspend Processing during Form Display echovue Access 2 November 17th, 2004 11:09 AM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.