Jump to content

smsmarketeers

Members
  • Posts

    27
  • Joined

  • Last visited

    Never

Contact Methods

  • AIM
    Dopey2003
  • Website URL
    http://www.smsmarketeers.com

Profile Information

  • Gender
    Male
  • Location
    Phoenix, AZ

smsmarketeers's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. @Minimeallolla: strtolower() is a function that will lowercase all of the alpha characters in the string given. For instance: <?php $string = 'ABCDEFGHI'; echo strtolower($string); // Output: abcdefghi ?>
  2. My last code example does what you are asking to do for the most part. If you copy and paste the code into a PHP file directly, setup the database, and you will see it work. Basically what you want to do is pass the user_id through the URL parameters and then, using $_GET['user_id'], SELECT * FROM users WHERE user_id = $_GET['user_id']. As for displaying the username's and user_id's in a table as a link, you need to SELECT * FROM users then loop through the results and display them.
  3. @dreampho: Here is you go. Here is your example. This uses the same database and tables as before. This script will allow you to create a user, then it will redirect to the edit portion using the exact same form. All fields except the password are automatically filled in and you can change the diseases. <?php /* Written By: SMS Marketeers Website: http://www.smsmarketeers.com User registration with error checking and multiple checkbox associations with scrollable div but in editable format. This script takes a user_id GET parameter in the URL. Database Structure CREATE TABLE IF NOT EXISTS `disease` ( `disease_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`disease_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=5 ; INSERT INTO `disease` (`disease_id`, `name`) VALUES (1, 'HIV / AIDS'), (2, 'Tuberculosis'), (3, 'Malaria'), (4, 'Cancer'); CREATE TABLE IF NOT EXISTS `user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(32) CHARACTER SET latin1 NOT NULL, `lastname` varchar(32) CHARACTER SET latin1 NOT NULL, `email` varchar(96) CHARACTER SET latin1 NOT NULL, `username` varchar(32) CHARACTER SET latin1 NOT NULL, `password` varchar(32) CHARACTER SET latin1 NOT NULL, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`user_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ; CREATE TABLE IF NOT EXISTS `user_to_disease` ( `user_id` int(11) NOT NULL, `disease_id` int(11) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin; */ // Define Database Variables $dbHostname = 'localhost'; $dbUsername = 'username'; $dbPassword = 'password'; $dbDatabase = 'database'; // Establish Database Connection $dbCon = mysql_connect($dbHostname, $dbUsername, $dbPassword) or die('Error: Unable able to connect: ' . mysql_error()); // Select Working Database $dbSel = mysql_select_db($dbDatabase, $dbCon) or die('Error: Unable to select database: ' . mysql_error()); // Handle POST $error = array(); $showForm = true; if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['user_id'])) { $emailPattern = '/^[A-Z0-9._%\-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i'; // Validate fields first if (empty($_POST['firstname'])) { $error['firstname'] = '<strong>Error:</strong> First Name is a required field!'; } if (empty($_POST['lastname'])) { $error['lastname'] = '<strong>Error:</strong> Last Name is a required field!'; } if (empty($_POST['email']) || (!preg_match($emailPattern, $_POST['email']))) { $error['email'] = '<strong>Error:</strong> Either the email address was left blank or is not valid!'; } if (empty($_POST['username'])) { $error['username'] = '<strong>Error:</strong> Username is a required field!'; } if (!$error) { mysql_query("UPDATE user SET firstname = '" . $_POST['firstname'] . "', lastname = '" . $_POST['lastname'] . "', email = '" . $_POST['email'] . "', username = '" . $_POST['username'] . "', date = NOW()") or die('Error: Unable to execute query: ' . mysql_error()); // Insert Diseases mysql_query("DELETE FROM user_to_disease WHERE user_id = '" . (int)$_GET['user_id'] . "'"); foreach ($_POST['disease'] AS $key => $value) { mysql_query("INSERT INTO user_to_disease SET user_id = '" . (int)$_GET['user_id'] . "', disease_id = '" . (int)$key . "'"); } $showForm = true; } else { $showForm = true; } } elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_GET['user_id'])) { $emailPattern = '/^[A-Z0-9._%\-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i'; // Validate fields first if (empty($_POST['firstname'])) { $error['firstname'] = '<strong>Error:</strong> First Name is a required field!'; } if (empty($_POST['lastname'])) { $error['lastname'] = '<strong>Error:</strong> Last Name is a required field!'; } if (empty($_POST['email']) || (!preg_match($emailPattern, $_POST['email']))) { $error['email'] = '<strong>Error:</strong> Either the email address was left blank or is not valid!'; } if (empty($_POST['username'])) { $error['username'] = '<strong>Error:</strong> Username is a required field!'; } if (empty($_POST['password'])) { $error['password'] = '<strong>Error:</strong> Password is a required field!'; } if (empty($_POST['confirm']) || $_POST['confirm'] != $_POST['password']) { $error['confirm'] = '<strong>Error:</strong> Confirm is a required field and must match password!'; } if (!$error) { mysql_query("INSERT INTO user SET firstname = '" . $_POST['firstname'] . "', lastname = '" . $_POST['lastname'] . "', email = '" . $_POST['email'] . "', username = '" . $_POST['username'] . "', password = '" . md5($_POST['password']) . "', date = NOW()") or die('Error: Unable to execute query: ' . mysql_error()); $user_id = mysql_insert_id(); // Insert Diseases foreach ($_POST['disease'] AS $key => $value) { mysql_query("INSERT INTO user_to_disease SET user_id = '" . (int)$user_id . "', disease_id = '" . (int)$key . "'"); } header('Location: user_registration_multi_checkbox_edit.php?user_id=' . $user_id); } else { $showForm = true; } } // Check URL parameters for user_id if (empty($_GET['user_id'])) { $error['missing_user_id'] = 'Error: Missing user_id in query parameters.'; } elseif ($_GET['user_id']) { $query = mysql_query("SELECT DISTINCT * FROM user WHERE user_id = '" . (int)$_GET['user_id'] . "'"); $result = mysql_fetch_assoc($query); } // Setup input variables if ($_POST['firstname']) { $firstname = $_POST['firstname']; } elseif ($result['firstname']) { $firstname = $result['firstname']; } else { $firstname = ''; } if ($_POST['lastname']) { $lastname = $_POST['lastname']; } elseif ($result['lastname']) { $lastname = $result['lastname']; } else { $lastname = ''; } if ($_POST['email']) { $email = $_POST['email']; } elseif ($result['email']) { $email = $result['email']; } else { $email = ''; } if ($_POST['username']) { $username = $_POST['username']; } elseif ($result['username']) { $username = $result['username']; } else { $username = ''; } ?> <html> <head> <title>User Registration</title> <style type="text/css"> * { font-size:12px; font-family:Arial; margin:0px; outline:0px; padding:0px; } body { background:#ffffff; color:#000000; margin:10px 0px 0px 0px; } img { border:0px; } p { margin:5px 0px 10px 0px; } form { border:none; margin:0px; padding:0px; } a { cursor:pointer; } a:link { color:#9AB324; text-decoration:none; } a:visited { color:#9AB324; text-decoration:none; } a:hover { color:#9AB324; text-decoration:underline; } a:active { color:#9AB324; text-decoration:none; } .container { margin:0px auto; width:700px; } .success { background:#EEF5CD; border:1px dashed #9AB324; color:#608339; margin-bottom:5px; padding:5px; text-align:left; } .warning { background:#eed4d2; border:1px dashed #a94637; color:#ac241a; margin-bottom:5px; padding:5px; text-align:left; } .attention { background:#fefbcc; border:1px dashed #e6db55; color:#ada019; margin-bottom:5px; padding:5px; text-align:left; } form, fieldset { border:none; margin:0px; padding:0px; } input, textarea, select { font:100% arial, sans-serif; vertical-align:middle; } input[type='text'] { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } input[type='password'] { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } input[type='radio'] { margin:0px 5px 0px 5px; } input[type='hidden'] { display:none !important; } select { border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; min-width:100px; padding:1px; } select option { padding:0px 5px 0px 5px; } textarea { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } table.form th { background:#9AB324; border-bottom:1px solid #596E0E; color:#ffffff; font-weight:bold; padding:5px; text-align:center; } table.form td { padding:5px; } table.form td.colOne { background:#f0f0f0; border-bottom:1px solid #dddddd; } table.form td.colTwo { background:#f5f5f5; border-bottom:1px solid #dddddd; } table.form td.button { background:#ffffff; border:none; text-align:right; } .scrollbox { background:#ffffff; border:1px solid #bbbbbb; height:100px; overflow-y:scroll; width:350px; } .scrollbox div { padding:5px; } .scrollbox div input { margin:0px; margin-right:5px; padding:0px; } .scrollbox div.rowOne { background:#ffffff; border-bottom:1px solid #dddddd; } .scrollbox div.rowTwo { background:#f5f5f5; border-bottom:1px solid #dddddd; } </style> </head> <body> <div class="container"> <?php if ($showForm == true) { ?> <form action="" method="POST" name="form" id="form"> <div class="attention">User registration with error checking and multiple checkbox associations with scrollable div.</div> <table align="center" border="0px" cellpadding="0px" cellspacing="1px" class="form" width="700px"> <tr> <td class="colOne" width="150px">First Name:</td> <td class="colTwo"> <input name="firstname" type="text" value="<?php echo $firstname; ?>" /> <?php if ($error['firstname']) { ?><span class="warning"><?php echo $error['firstname']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne">Last Name:</td> <td class="colTwo"> <input name="lastname" type="text" value="<?php echo $lastname; ?>" /> <?php if ($error['lastname']) { ?><span class="warning"><?php echo $error['lastname']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne">Email Address:</td> <td class="colTwo"> <input name="email" type="text" value="<?php echo $email; ?>" /> <?php if ($error['email']) { ?><span class="warning"><?php echo $error['email']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne">Username:</td> <td class="colTwo"> <input name="username" type="text" value="<?php echo $username; ?>" /> <?php if ($error['username']) { ?><span class="warning"><?php echo $error['username']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne">Password:</td> <td class="colTwo"> <input name="password" type="password" value="" /> <?php if ($error['password']) { ?><span class="warning"><?php echo $error['password']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne">Confirm Password:</td> <td class="colTwo"> <input name="confirm" type="password" value="" /> <?php if ($error['confirm']) { ?><span class="warning"><?php echo $error['confirm']; ?></span><?php } ?> </td> </tr> <tr> <td class="colOne" valign="top">Previous Disease(s):</td> <td class="colTwo"> <div class="scrollbox" style="height:100px; width:100%;"> <?php // Select All Disease Records from Database $query = mysql_query("SELECT * FROM disease ORDER BY name ASC") or die('Error: Unable to execute query: ' . mysql_error()); if (is_resource($query)) { $i = 0; $data = array(); while ($result = mysql_fetch_assoc($query)) { $data[$i] = $result; $i++; } mysql_free_result($query); $results = array(); $results = $data; unset($data); } // Select All Use Disease Records from Database $query = mysql_query("SELECT * FROM user_to_disease WHERE user_id = '" . (int)$_GET['user_id'] . "'"); while ($row = mysql_fetch_assoc($query)) { $user_diseases[] = $row['disease_id']; } $rowOne = 'rowOne'; $rowTwo = 'rowTwo'; $rowCount = 0; ?> <?php foreach ($results as $disease) { ?> <?php $rowClass = ($rowCount % 2) ? $rowTwo : $rowOne; ?> <div class="<?php echo $rowClass; ?>"> <?php if (in_array($disease['disease_id'], $user_diseases)) { ?> <input name="disease[<?php echo $disease['disease_id']; ?>]" type="checkbox" checked="checked" /><?php echo $disease['name']; ?> <?php } else { ?> <input name="disease[<?php echo $disease['disease_id']; ?>]" type="checkbox" /><?php echo $disease['name']; ?> <?php } ?> </div> <?php $rowCount++; ?> <?php } ?> </div> </td> </tr> <tr><td class="button" colspan="4"> <?php if (isset($_GET['user_id'])) { ?> <input name="submit" type="submit" value="Update Profile" /> <?php } else { ?> <input name="submit" type="submit" value="Create Profile" /> <?php } ?> </td></tr> </table> </form> <?php } elseif ($showForm == false) { ?> <?php if (isset($_GET['user_id'])) { ?> <div class="success">User updated successfully.</div> <?php } else { ?> <div class="success">Thank you for your registration.</div> <?php } ?> <?php } ?> </div> <?php mysql_close($link); ?> </body> </html>
  4. @dreampho: Neither of those is really that difficult. The easiest thing to do for editing is to copy your registration form. Then, add code to select all of the "diseases" that the user had selected during registration from the database. Put them into an array and, while looping through all of the "diseases", check to see if the "disease" was checked by using checking to see if the diseases unique identifier exists in the array. If it does, make the box checked. If it does not, then the box is not checked. I do this using jQuery most of the time because I am pulling results using an API from another system but it does not have to be done that way. If you are confused, check out the in_array() function and let me know if you need an example. http://php.net/manual/en/function.in-array.php
  5. This is really going to depend on how you want to display the information. Did you want to display it in an editable for so that members can update their records or did you just want to display the information in a text formatted way on the screen?
  6. As fugix said, stupid mistakes happen to everyone, even me. Thank you for the props but I am by no means that best developer. I have a very specific way that I write code and format my code. Not only does it help others understand what I was thinking, but it also helps me when I have to go back to something that I wrote six months or six years ago. Generally, everything is tabbed out and I use an MVC framework. The framework I use was taken from an open source piece of software. I stripped it out, tweaked it, and rewrote some of it to fit my needs. Again, I am not the best but I learned by pulling other peoples code and small snippets apart, I suggest the same. But don't start with a bloated piece of software or something that is going to be difficult to understand. For instance, you were building a piece of forums software I would suggest downloading an open source piece of software, installing it, and then pulling it apart bit by bit. However, I wouldn't do it with phpBB3, but possibly phpBB2. OpenCart is another great piece of open source free software that uses an MVC framework and has been written VERY well. May I suggest one more thing. If you are using a school laptop, or no matter what you are using, get a month to month hosting account for a few dollars a month that gives you at least on MySQL database and supports PHP. Then, you can either upload code or download an IDE such as Notepad++ or EditPlus (I use EditPlus but it isn't free) and edit code directly through FTP. May I suggest: http://www.viewmyserver.com
  7. Yeah well, he said he wanted to do it in PHP, not the MySQL query nor did he ask for "simplified" or "efficient". Not to mention that I would NEVER use MySQL to control dates and times because of timezone settings and other problems. PHP makes a much better effort at controlling times with timezones.
  8. Here is a better way to write this: <?php if ((trim($country) !== "") && ($state !== "") && ($city !== "")) { $query = mysql_query("SELECT * FROM users WHERE users.state='" . $state . "' AND users.country='" . $country . "' AND users.city='" . $city . "' ORDER BY RAND() DESC LIMIT 1"); $result = mysql_fetch_array($query); Echo "Country / City / State / Search"; $getuname = $result['username'];
  9. The problem is, the other person showed you how to do it using MySQL and you said PHP. So, here is how we do this in PHP: <?php $myDate = '2011-05-06 16:10:30'; echo date('m/d/Y', strtotime($myDate)); // Output: 05/06/2011 echo date('g:iA', strtotime($myDate)); // Output: 4:10PM echo date('M jS, Y @ g:iA', strtotime($myDate)); // Output: Jan 6th, 2011 @ 4:10PM ?> Basically, you need to take your MySQL result, convert it to a UNIX timestamp using strtotime() then use the date() function to display what you want. For more information: http://php.net/manual/en/function.date.php
  10. Is this what you are looking for? <?php /* Written By: SMS Marketeers Website: http://www.smsmarketeers.com Using two arrays, use the name of the day to return the price for that day. Database Structure None */ ?> <html> <head> <title>Day Pricing using Arrays</title> <style type="text/css"> * { font-size:12px; font-family:Arial; margin:0px; outline:0px; padding:0px; } body { background:#ffffff; color:#000000; margin:10px 0px 0px 0px; } img { border:0px; } p { margin:5px 0px 10px 0px; } form { border:none; margin:0px; padding:0px; } a { cursor:pointer; } a:link { color:#9AB324; text-decoration:none; } a:visited { color:#9AB324; text-decoration:none; } a:hover { color:#9AB324; text-decoration:underline; } a:active { color:#9AB324; text-decoration:none; } .container { margin:0px auto; width:300px; } .success { background:#EEF5CD; border:1px dashed #9AB324; color:#608339; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; } .warning { background:#eed4d2; border:1px dashed #a94637; color:#ac241a; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; } .attention { background:#fefbcc; border:1px dashed #e6db55; color:#ada019; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; } table.data th { background:#9AB324; border-bottom:1px solid #596E0E; color:#ffffff; font-weight:bold; padding:5px; text-align:center; } table.data td { border-bottom:1px solid #dddddd; padding:5px; } table.data td.rowOne { background:#f5f5f5; } table.data td.rowTwo { background:#eeeeee; } table.data td.button { background:#ffffff; border:none; text-align:right; } </style> </head> <body> <div class="container"> <?php $arrDays = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"); $arrPrices = array("sunday" => "$1.00", "monday" => "$2.00", "tuesday" => "$3.00", "wednesday" => "$4.00", "thursday" => "$5.00", "friday" => "$6.00", "saturday" => "$7.00"); ?> <form action="" method="POST" name="form" id="form"> <div class="attention">Using two arrays, use the name of the day to return the price for that day.</div> <table align="center" border="0px" cellpadding="0px" cellspacing="1px" class="data" width="300px"> <tr> <th>Day of the Week</th> <th>Price</th> </tr> <?php $rowOne = 'rowOne'; $rowTwo = 'rowTwo'; $rowCount = 0; ?> <?php foreach ($arrDays as $day) { ?> <?php $rowClass = ($rowCount % 2) ? $rowTwo : $rowOne; ?> <tr> <td class="<?php echo $rowClass; ?>"><?php echo ucwords($day); ?></td> <td class="<?php echo $rowClass; ?>"><?php echo $arrPrices[$day]; ?></td> </tr> <?php $rowCount++; ?> <?php } ?> </table> </form> </div> </body> </html>
  11. I personally use EditPlus however, it is not free. It is however, a great editor and I prefer it over Notepad++. I put your code into my editor and executed it and yes, because you were missing the ?> the script failed. I rewrote the code how I would write it: <html> <head> <title>Website Title</title> </head> <body> <?php echo "<p>Random Text</p>"; print "<p>You can print with braces</p>"; print "<p>or without them!</p>"; ?> </body> </html> No difference really but it does work! FYI, when I was starting out over ten years ago I found the easiest way to learn was to download open source applications / scripts already written, install them, and then pull them apart to see how they work. The biggest thing that you would want to learn is OOP, Object Oriented Programming. It will help you with rapid development and reusing already written code. Good luck!
  12. dreampho: No problem, I am glad that you are starting to understand what you are trying to accomplish a bit better. Because you have another series of checkboxes you would want to add another database table called "user_to_food" or "user_to_allergies" or "user_to_food_allergies". Whatever you name it is entirely up to you. The two columns that would be in the table are "user_id" and "allergy_id" or "food_allergy_id". You will also need a second table called "food_allergies" or "allergies" that will hold the names of the allergies and their unique identifier that will be used in the previous table explained. MySQL is what is called a relational database. What this means is that all records usually have unique identifiers. Those unique identifiers can be used in other tables to associate one record in one table with another record in another table. This is not entirely what "relational database" means but more of a brief description of one way it is used. For the actual code to add another set of checkboxes you will need to duplicate two pieces of code and that code would be the two that you mentioned, of course changing the column names to match those of the new tables in the database that you need to create. As for getting multiple columns in the scrollable DIV what you would want to do is replace the row <div> with a table. I had to do this yesterday in an application that I am working on. My application had to accept a latitude and longitude for a each camera. If you need more help than this please let me know and I will add to the code I already wrote to show you how to accomplish what you are after.
  13. dreampho: The code snippet that I wrote for you previously has the database tables setup correctly for auto_increment as TLG mentioned. I urge you, copy the code into a PHP file, save it, create a test database, import the tables and values at the top of the script and execute the script to see how it is working. The easiest way to learn is to implement someone else code and add breaks / exits and echo statements to it to see what it is doing and how it is doing it. In answer to your questions: Thank you both! Question: For my form, there will be several sections that are like 'diseases' with checkboxes, so I am guessing I will need to have a separate table for each? Answer: No, you do not need to have a separate table for each "type" of disease if you are categorizing them. What you can do is a multitude of things. One, you can create a "type" field in the "disease" table and identify each disease with a category name. Of course, you would want to do this without spaces, capitalization and special characters because you will need to refer to them in the array to place them in the correct spots on the page. The other way is to create a separate table for "disease categories" and then use a relationship to categorize them. Then, you would loop through the MySQL query results on the page to place them correctly in categories. Question: Is it possible to set a unique ID to each submission of the form, then have this ID included in each table, say 'user_details', 'disease', 'food_types' etc. Then the diseases and food types would be connected to the users details similar to what you suggested above. Answer: Yes, TLG explained the auto_increment setting in MySQL / phpMyAdmin and I explained above how to do the relationship. You are not talking rubbish however, I do believe that for a beginning what you are trying to do is going to be very difficult in the beginning. If you can master what you are trying to put together then development with PHP will come a lot easier for you. The big thing that most people need to understand about development, especially with PHP, is you need to have commonsense. Writing PHP is just like talking. Let's take an IF statement for instance: $foo = 1; $bar = 1; if ($foo == $bar) { echo "WE MATCH!"; } else { echo "WE DO NOT MATCH"; } The above code snippet would sound like this talking: if foo is definitely equal to bar then WE MATCH otherwise WE DO NOT MATCH. You can take this further as well an explicit state the second else as an ELSEIF. Make sense?
  14. Whoa! Glancing at your code tells me that there are some bad tendencies that you have when writing code. Try this: <?php define('DB_NAME', 'dbname'); define('DB_USER', 'username'); define('DB_PASS', 'pass'); define('DB_HOST', 'server.sql.com'); // contact to database $connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Error , check your server connection.'); mysql_select_db(DB_NAME); // get & set variables if ($_SERVER['REQUEST_METHOD']=='POST') { $name = $_POST['name']; $email = $_POST['email']; $msg = $_POST['msg']; } else { $name = ''; $email = ''; $msg = ''; } if ($_SERVER['REQUEST_METHOD']=='POST') { // check for errors here if (strlen($msg) < $aError[] = 'Password must be at least 8 characters.'; if ($msg == strtolower($msg)) $aError[] = 'Password must have at least 1 uppercase.'; if (preg_replace("/[^a-zA-Z0-9\s]/", "", $msg) == $msg) $aError[] = 'Password must have a least one special character.'; if (strcspn($msg, '0123456789') == strlen($msg)) $aError[] = 'Password must one at least one number.'; if ($name == "" || $msg == "" ) $aError[] = 'Please enter a password.'; if (check_email_address($name) == false) $aError[] = 'Please enter a valid email.'; // NO ERRERS process form if (count($aError) == 0) { // --process form here-- echo 'SQL SERVER MESSUP YOU BLEW UP MESSAGE'; $query = "INSERT INTO contact(name,email,msg) VALUES ('$name','$email','$msg')"; $result = mysql_query( $query ); if (!$result) { die( mysql_error()); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Siteis</title> <link rel="stylesheet" type="text/css" href="http://site.com/signup.css"> </head> <body> <div class="main"> <!-- BEGIN LEFT --> <div class="left"> <h1>Why join?</h1> <div class="body">siteis the web's most robust research company with a universe of over 100 .</div><br> <img src="http://site.com/joined.png" alt="Over 000 have already joined." width="170" height="70"/> </div> <!-- BEGIN CENTER --> <div class="center"> <h1>Join free:</h1> <form action="" method="POST" id="insert"> <div class="labels">Name:</div><input type="text" size="28" name="name" value="<?php echo $name; ?>" /> <div class="labels">Email Address</div><input type="text" size="28" name="email" value="<?php echo $email; ?>" /> <div class="labels">Choose Password</div><input type="password" size="28" name="msg"> <?php if (isset($aError[0]) && $aError[0]!="") { ?> <div class="error"><?php echo $aError[0]; ?></div> <?php } ?> <div class="agreeterms"><input type="checkbox" name="gender" checked="checked">I agree to terms and privacy policy</div> <div class="joinbutton"><input type="submit" name="submit" value="Join"></div> <div class="agreeterms"><a href="">Click here to log in</a></div> <br /><br /><br /> </form> </div> <!-- BEGIN RIGHT --> </div> </body> </html> <?php // check valid email function check_email_address($email) { // First, we check that there's one @ symbol, and that the lengths are right if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; } ?> First, your code was outside of your beginning body tag. Second, you had no ending div tag. Third, try to keep your functions outside of your logic, like at the bottom of the script. I do want to warn you. I test the code above (commenting out the SQL stuff) and when I entered information correct, the fields pre-populated as you want however, it says my email address was bad. I highly suggest you check the regular expressions that you are using. There is no need to use more than one regular expression when checking the validation of an email address. Here is the one that I use and how I use it: /^[A-Z0-9._%\-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i if (!preg_match($emailPattern, $_POST['email'])) { $error['email'] = '<strong>Error:</strong> Email address is not valid!'; }
  15. You really should test more thoroughly. These code pasted directly into a .php file shows the following errors: Notice: Undefined index: item in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 36 Notice: Undefined index: credit_input in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 39 Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 42 You do not have sufficient funds to purchase a . Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 46 You do not have sufficient funds to purchase a . Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 50 You do not have sufficient funds to purchase a . Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 55 Coke resources depleted. Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 61 Sprite resources depleted. Notice: Undefined index: submit in /var/www/private_html/projects.smsmarketeers.com/html/scripts/vending_machine.php on line 67 Fanta resources depleted.Your current funds accumlate to The problem is that "submit" is not defined because "submit" is the submit button which is not defined until the form is submitted. I suggested checking to see if the form was submitted a different way, perhaps using $_SERVER variables. For example: <html> <head> <title>Vending Machine</title> <style type="text/css"> * { font-size:12px; font-family:Arial; margin:0px; outline:0px; padding:0px; } body { background:#ffffff; color:#000000; margin:10px 0px 0px 0px; } img { border:0px; } p { margin:5px 0px 10px 0px; } form { border:none; margin:0px; padding:0px; } a { cursor:pointer; } a:link { color:#9AB324; text-decoration:none; } a:visited { color:#9AB324; text-decoration:none; } a:hover { color:#9AB324; text-decoration:underline; } a:active { color:#9AB324; text-decoration:none; } .container { margin:0px auto; width:400px; } .success { background:#EEF5CD; border:1px dashed #9AB324; color:#608339; margin-bottom:5px; padding:5px; text-align:left; } .warning { background:#eed4d2; border:1px dashed #a94637; color:#ac241a; margin-bottom:5px; padding:5px; text-align:left; } .attention { background:#fefbcc; border:1px dashed #e6db55; color:#ada019; margin-bottom:5px; padding:5px; text-align:left; } h1 { color:#9AB324; font-size:16px; } form, fieldset { border:none; margin:0px; padding:0px; } input, textarea, select { font:100% arial, sans-serif; vertical-align:middle; } input[type='text'] { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } input[type='password'] { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } input[type='radio'] { margin:0px 5px 0px 5px; } input[type='hidden'] { display:none !important; } select { border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; min-width:100px; padding:1px; } select option { padding:0px 5px 0px 5px; } textarea { background:#ffffff; border:1px solid #c3c3c3; border-left-color:#7c7c7c; border-top-color:#7c7c7c; padding:2px; } table.data th { background:#9AB324; border-bottom:1px solid #596E0E; color:#ffffff; font-weight:bold; padding:5px; text-align:center; } table.data td { padding:5px; } table.data td.rowOne { background:#f0f0f0; border-bottom:1px solid #dddddd; } table.data td.rowTwo { background:#f5f5f5; border-bottom:1px solid #dddddd; } table.data td.button { background:#ffffff; border:none; text-align:right; } </style> </head> <body> <div class="container"> <?php // Check if form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Cost of items $price = array('coke' => '1.25', 'diet' => '1.50', 'sprite' => '1.75'); // Quantity Available $quantity = array('coke' => '5', 'diet' => '7', 'sprite' => '2'); // Selected Item Information $quantAvail = $quantity[strtolower($_POST['item'])]; $itemPrice = $price[strtolower($_POST['item'])]; // First check Quantity if ($quantAvail < $_POST['quantity']) { echo '<div class="warning">Insufficient quantity available.</div>'; } elseif ($quantAvail >= $_POST['quantity']) { echo '<div class="success">Sufficient quantity available. Checking funds...</div>'; } // If we passed quantity, check funds if (($_POST['quantity'] * $itemPrice) > $_POST['funds']) { echo '<div class="warning">Insufficient funds available.</div>'; } elseif (($_POST['quantity'] * $itemPrice) <= $_POST['funds']) { echo '<div class="success">Sufficient funds available.</div>'; } // Remaining Funds $remainingFunds = $_POST['funds'] - ($_POST['quantity'] * $itemPrice); echo '<div class="attention">You have <strong>$' . $remainingFunds . '</strong> funds remaining</div>'; } ?> <h1>Vending Machine</h1> <p>What would you like to purchase?</p> <form action="" method="post" name="vend"> <table align="center" border="0px" cellpadding="0px" cellspacing="1px" class="data" width="400px"> <tr> <th width="30px"> </th> <th style="text-align:left;">Item</th> <th width="70px">Price</th> </tr> <tr> <td align="center" class="rowOne"><input name="item" type="radio" value="Coke" /></td> <td class="rowOne">Coke</td> <td align="center" class="rowOne">$1.25</td> </tr> <tr> <td align="center" class="rowTwo"><input name="item" type="radio" value="Diet" /></td> <td class="rowTwo">Diet Coke</td> <td align="center" class="rowTwo">$1.50</td> </tr> <tr> <td align="center" class="rowTwo"><input name="item" type="radio" value="Sprite" /></td> <td class="rowTwo">Sprite</td> <td align="center" class="rowTwo">$1.75</td> </tr> <tr> <td align="center" class="rowOne" colspan="3"> Qty: <input type="text" size="2" name="quantity" value="1" /> Available Funds: $<input type="text" value="5.00" name="funds" size="5" /> </td> </tr> <tr><td class="button" colspan="3"><input name="submit" type="submit" value="Vend" /></td></tr> </table> </form> </div> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.