I Just Start Having My PHP Class For A Few Weeks So I’m Still Very New To All PHP Stuff. One Of The Assignment I Had With My Professor Is To Add Another Field For User To Enter - “Zip Code”. After I Inputted All The Information Requires And Hit Register Button. The Form Doesn’t Send To The Database But Instead Resetting Itself. Could Somebody Look Over My Code And Help Me Out Please?
This Is Submitting Form Code.
[php]
// array of book titles
$booklist = array( "Internet and WWW How to Program 4e",
"C++ How to Program 6e", "Java How to Program 7e",
"Visual Basic 2005 How to Program 3e" );
// array of possible operating systems
$systemlist = array( "Windows XP", "Windows Vista",
"Mac OS X", "Linux", "Other");
// array of name values for the text input fields
$inputlist = array( "fname" => "First Name",
"lname" => "Last Name", "email" => "Email",
"phone" => "Phone", "zip" => "Zip Code" );
// initialize variables to check for errors
$formerrors["fnameerror"] = false;
$formerrors["lnameerror"] = false;
$formerrors["emailerror"] = false;
$formerrors["phoneerror"] = false;
$formerrors["ziperror"] = false;
$inputvalues = false;
$fname = false;
$lname = false;
$email = false;
$phone = false;
$zip = false;
$os = false;
$book = false;
// ensure that all fields have been filled in correctly
if ( isset ( $submit ) )
{
if ( $fname == "" )
{
$formerrors[ "fnameerror" ] = true;
$iserror = true;
} // end if
if ( $lname == "" )
{
$formerrors[ "lnameerror" ] = true;
$iserror = true;
} // end if
if ( $email == "" )
{
$formerrors[ "emailerror" ] = true;
$iserror = true;
} // end if
if ( !preg_match( "/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/", $phone ) )
{
$formerrors[ "phoneerror" ] = true;
$iserror = true;
} // end if
if ( !preg_match( "/^[0-9]{5}$/" , $zip ) )
{
$ziperror[ "ziperror" ] = true;
$iserror = true;
}
if ( !$iserror )
{
// build INSERT query
$query = "INSERT INTO contacts " .
"( LastName, FirstName, Email, Zip, Phone, Book, OS ) " .
"VALUES ( '$lname', '$fname', '$email', '$zip' " .
"'" . quotemeta( $phone ) . "', '$book', '$os' )";
// Connect to MySQL
if ( !( $database = mysql_connect( "localhost",
"iw3htp4", "password" ) ) )
die( "Could not connect to database" );
// open MailingList database
if ( !mysql_select_db( "mailinglist", $database ) )
die( "Could not open mailinglist database" );
// execute query in MailingList database
if ( !( $result = mysql_query( $query, $database ) ) )
{
print( "Could not execute query! <br />" );
die( mysql_error() );
} // end if
mysql_close( $database );
print( "<p>Hi<span class = 'prompt'>
<strong>$fname</strong></span>.
Thank you for completing the survey.<br />
You have been added to the
<span class = 'prompt'>
<strong>$book</strong></span>
mailing list.</p>
<strong>The following information has been saved
in our database:</strong><br />
<table><tr>
<td class = 'name'>Name </td>
<td class = 'email'>Email</td>
<td class = 'phone'>Phone</td>
<td class = 'zip'>Zip Code</td>
<td class = 'os'>OS</td>
</tr><tr>
<!-- print each form field’s value -->
<td>$fname $lname</td>
<td>$email</td>
<td>$phone</td>
<td>$zip</td>
<td>$os</td>
</tr></table>
<br /><br /><br />
<div><div>
<a href = 'formDatabase.php'>
Click here to view entire database.</a>
</div>This is only a sample form.
You have not been added to a mailing list.
</div></body></html>" );
die();
} // end if
} // end if
print( "<h1>Sample Registration Form.</h1>
Please fill in all fields and click Register." );
if ( $iserror )
{
print( "<br /><span class = 'largeerror'>
Fields with * need to be filled in properly.</span>" );
} // end if
print( "<!-- post form data to form.php -->
<form method = 'post' action = 'dynamicForm.php'>
<img src = 'images/user.gif' alt = 'User' /><br />
<span class = 'prompt'>
Please fill out the fields below.<br /> </span>
<!-- create four text boxes for user input -->" );
foreach ( $inputlist as $inputname => $inputalt )
{
//$inputtext = $inputvalues[ $inputname ];
print( "<img src = 'images/$inputname.gif'
alt = '$inputalt' /><input type = 'text'
name = '$inputname' value = '" . $$inputname . "' />" );
if ( $formerrors[ ( $inputname )."error" ] == true )
print( "<span class = 'error'>*</span>" );
print( "<br />" );
} // end foreach
if ( $formerrors[ "phoneerror" ] )
print( "<span class = 'error'>" );
else
print("<span class = 'smalltext'>");
if ( $formerrors[ "ziperror" ] )
print( "<span class = 'error'>" );
else
print( "<span class = 'smalltext'>");
print( "Phone Numbers must be in the form (555)555-5555<br>
Zip code must be in form of 5 digit numbers</span><br><br>
<img src = 'images/downloads.gif'
alt = 'Publications' /><br />
<span class = 'prompt'>
Which book would you like information about?
</span><br />
<!-- create drop-down list containing book names -->
<select name = 'book'>" );
foreach ( $booklist as $currbook )
{
print( "<option" );
if ( ( $currbook == $book ) )
print( " selected = 'true'" );
print( ">$currbook</option>" );
} // end foreach
print( "</select><br /><br />
<img src = 'images/os.gif' alt = 'Operating System' />
<br /><span class = 'prompt'>
Which operating system are you currently using?
<br /></span>
<!-- create five radio buttons -->" );
$counter = 0;
foreach ( $systemlist as $currsystem )
{
print( "<input type = 'radio' name = 'os'
value = '$currsystem'" );
if ( $currsystem == $os )
print( "checked = 'checked'" );
elseif ( !$os && $counter == 0 )
print( "checked = 'checked'" );
print( " />$currsystem" );
// put a line break in list of operating systems
if ( $counter == 1 ) print( "<br />" );
++$counter;
} // end foreach
print( "<!-- create a submit button -->
<br /><input type = 'submit' name = 'submit'
value = 'Register' /></form></body></html>" );
?>
[/php]
This Is Database Code.
[php]
// build SELECT query
$query = "SELECT * FROM contacts";
// Connect to MySQL
if ( !( $database = mysql_connect( "localhost",
"iw3htp4", "password" ) ) )
die( "Could not connect to database </body></html>" );
// open MailingList database
if ( !mysql_select_db( "mailinglist", $database ) )
die( "Could not open mailingList database </body></html>" );
// query MailingList database
if ( !( $result = mysql_query( $query, $database ) ) )
{
print( "Could not execute query! <br />" );
die( mysql_error() . "</body></html>" );
} // end if
?><!-- end PHP script -->
<h3>Mailing List Contacts</h3>
<table>
<tr>
<td>ID</td>
<td>Last Name</td>
<td>First Name</td>
<td>E-mail Address</td>
<td>Phone Number</td>
<td>Zip Code</td>
<td>Book</td>
<td>Operating System</td>
</tr>
<?php
// fetch each record in result set
for ( $counter = 0; $row = mysql_fetch_row( $result );
$counter++ )
{
// build table to display results
print( "<tr>" );
foreach ( $row as $key => $value )
print( "<td>$value</td>" );
print( "</tr>" );
} // end for
mysql_close( $database );
?><!-- end PHP script -->
</table>
[/php]
This Is myAdmin Sql Code
CREATE DATABASE mailingList;
USE mailingList;
CREATE TABLE contacts
(
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
LastName varchar(30),
FirstName varchar(30),
Email varchar(64),
Phone varchar(13),
Zip varchar(10),
Book varchar(60),
OS varchar(30)
);
Much Appreciate In Advanced For All The Helps.
[sub]p/s: Codes Are Taken Out From Existed Source To Modify In Purpose Of Class Assignment Exercise.
Source: PHP Novice To Ninja.[/sub]