Jouninshinobi
08-26-2004, 11:51 AM
how can I creat a form in php that sends the information straight to a MySQL table I've created and sends a displays a message or confirmation email
thx,
thx,
![]() | View Full Version : umm question on php forms Jouninshinobi 08-26-2004, 11:51 AM how can I creat a form in php that sends the information straight to a MySQL table I've created and sends a displays a message or confirmation email thx, stormraven 08-26-2004, 12:12 PM Here's some references if you want to do it yourself.. PHP Communicating to mySQL: http://us2.php.net/manual/en/ref.mysql.php PHP Mail() function: http://us2.php.net/manual/en/ref.mail.php You could probably hire someone to do it cheap as well.. Crimson54 08-26-2004, 12:31 PM Form: <script LANGUAGE="JavaScript"> <!-- var foc_start = 16; // Needs to be the index of the first Focalex item function ValidateForm() { if(document.mainform.user_alias.value == "") { alert("You must enter an username to sign up for a Crimson Archives account."); document.mainform.user_alias.focus(); return false; } if(document.mainform.user_firstname.value == "") { alert("You must enter your first name to sign up for a Crimson Archives account."); document.mainform.user_firstname.focus(); return false; } if(document.mainform.user_lastname.value == "") { alert("You must enter your last name to sign up for a Crimson Archives account."); document.mainform.user_lastname.focus(); return false; } if(document.mainform.user_password.value.length < 4) { alert("You must enter a password from 4-10 characters to sign up for a Crimson Archives account."); document.mainform.user_password.focus(); return false; } if((document.mainform.user_email.value == "") || (document.mainform.user_email.value.indexOf("@") == -1) || (document.mainform.user_email.value.indexOf(".") == -1)) { alert("You must enter a valid e-mail address to sign up for a Crimson Archives account."); document.mainform.user_email.focus(); return false; } if(document.mainform.user_email2.value == "") { alert("Please confirm your e-mail address."); document.mainform.user_email2.focus(); return false; } if(document.mainform.user_email.value != document.mainform.user_email2.value) { alert("Your e-mail addresses don't match up. Please make sure that you enter the same address into both of the fields provided."); document.mainform.user_email2.value = ""; document.mainform.user_email.focus(); return false; } return true; } function CookieVal(key) { var arr = new Array(); var value; arr = document.cookie.split(";") for(var i=0; i<arr.length; i++) { if(arr[i].indexOf(key + "=") != -1) { value = arr[i].substring(arr[i].indexOf("=")+1); return(value); } } return(-1); } function DoStuff() { referrer_name = CookieVal("referrer_username"); if(referrer_name != -1) document.forms[0].user_referrer.value = referrer_name; } function DoClick(x) { document.forms[0].elements[foc_start + x].checked = !(document.forms[0].elements[foc_start + x].checked); } // --> </script> <style> BODY { scrollbar-base-color: 313233; scrollbar-arrow-color: 313233; scrollbar-face-color: 000000; scrollbar-track-color: 000000; scrollbar-shadow-color: 313233; scrollbar-highlight-color: 313233; scrollbar-dark-shadow-color: 313233; scrollbar-3d-light-color: 313233; } </style> </head> <body bgcolor="#000000" text="#FFFFFF" link="#FFCC00" vlink="#FFCC00" alink="#FFFFFF" topmargin="4"> <form method="post" name="mainform" action="register.php" onSubmit="return ValidateForm();"> <table border="0" cellspacing="0" width="510" cellpadding="3"> <tr> <td nowrap><font color="#C0C0C0"><b><font face="Arial, Helvetica, sans-serif"><font size="2">Username</font></font><font size="2" face="Arial, Helvetica, sans-serif">:</font></b> </font></td> <td colspan="1" nowrap><font color="#C0C0C0"><input type="text" name="user_alias" size="20" maxlength="20" style="font-weight: bold; background-color: rgb(0,0,0); color: rgb(192,192,192)"> <font size="2">*</font> </font></td> <td colspan="2" rownspan="2" nowrap><div align="left"><p><font color="#C0C0C0">Fields marked * are required.</font></td> </tr> <tr> <td nowrap><font color="#C0C0C0"><font size="2" face="Arial, Helvetica, sans-serif"><b>First Name:</b></font> </font></td> <td colspan="1"><font color="#C0C0C0"><input type="text" name="user_firstname" size="20" maxlength="20" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bold"> <font size="2">*</font> </font></td> <td colspan="2" rowspan="5" valign="middle"><font size="2" face="Arial,Helvetica"><font color="#C0C0C0"><br> You will receive a verification email to validate your account. We don't sell email addresses or send SPAM.</font><p></font> </td> </tr> <tr> <td nowrap><font color="#C0C0C0"><font size="2" face="Arial, Helvetica, sans-serif"><b>Last Name:</b></font> </font></td> <td colspan="3"><font color="#C0C0C0"><input type="text" name="user_lastname" size="20" maxlength="20" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bold"> <font size="2">*</font> </font></td> </tr> <tr> <td><font color="#C0C0C0"><font size="2" face="Arial, Helvetica, sans-serif"><b>Password:</b></font> </font></td> <td colspan="3"><font color="#C0C0C0"><input type="password" name="user_password" size="19" maxlength="10" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bolder"> <font size="2">*</font> </font></td> </tr> <tr> <td valign="top"><font color="#C0C0C0"><font size="2" face="Arial, Helvetica, sans-serif"><b>E-mail:</b></font> </font></td> <td colspan="1" nowrap><font color="#C0C0C0"><input type="text" name="user_email" size="20" maxlength="50" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bolder"> <font size="2">*</font> </font></td> </tr> <tr> <td valign="top" nowrap><font color="#C0C0C0"><font size="2" face="Arial, Helvetica, sans-serif"><b>Confirm E-mail:</b></font> </font></td> <td colspan="1" nowrap><font color="#C0C0C0"><input type="text" name="user_email2" size="20" maxlength="50" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bolder"> <font size="2">*</font> </font></td> </tr> <tr> <td></td> <td colspan="3"><table border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="middle"><input type="checkbox" name="user_email_visible" CHECKED value="ON" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bold"> </td> <td> </td> <td><font size="1" face="Arial, Helvetica, sans-serif">E-mail Visible to Users</font> </td> <td> </td> </tr> <!-- <tr> <td colspan="3" valign="middle"> <input type="checkbox" name="receive_adult" CHECKED> </td> <td> </td> <td> <font size="1" face="Arial, Helvetica, sans-serif">Receive Adult Mailings</font> </td> <td> <font face="arial" size="1" color="#FFCC00">+ 5 Experience Points!</font></td> </tr> --> </table> </td> </tr> <!-- <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">AIM name:</font> </td> <td colspan="3"> <input type="text" name="user_aim" size="20" maxlength="30" value=""> <font face="Arial, Helvetica, sans-serif" size="1" color="#FFCC00">AIM = America Online (AOL) Instant Messenger.</font> </td> </tr> <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">Web URL:</font> </td> <td colspan="3"> <input type="text" name="user_weburl" size="20" maxlength="60" value="http://"> </td> </tr> <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">Web name:</font> </td> <td colspan="3"> <input type="text" name="user_webtitle" size="20" maxlength="30"> </td> </tr> <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">Webcam:</font> </td> <td colspan="3"> <input type="text" name="user_webcam" size="20" maxlength="80" value="http://"> </td> </tr> <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">Age:</font> </td> <td> <select name="user_age"> <option value="null">-- choose --</option> <option value="1">13-17</option> <option value="2">18-25</option> <option value="3">26-29</option> <option value="4">30-39</option> <option value="5">40-49</option> <option value="6">50-59</option> <option value="7">60 or over</option> </select> </td> <td colspan=2 rowspan="3" valign="top"><font face="Arial, Helvetica, sans-serif" size="1" color="#FFCC00">If you are under 18 years of age, you are certifying that you have the permission of a parent or legal guardian. If you are under 13, please leave immediately!</font></td> </tr> --> <tr> <td><font size="2" face="Arial, Helvetica, sans-serif">Gender:</font> </td> <td><select name="user_gender" size="1" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bolder"> <option value="null">Select</option> <option value="m">Male</option> <option value="f">Female</option> </select> </td> </tr> <!-- <tr> <td> <font size="2" face="Arial, Helvetica, sans-serif">Zip code:</font> </td> <td> <input type="text" name="zip" size="6" maxlength="6"> </td> </tr> <tr> <td valign="top" nowrap> <font size="2" face="Arial, Helvetica, sans-serif">Referrer:</font> </td> <td valign="top"> <input type="text" name="user_referrer" size="20" maxlength="20"> </td> <td colspan="2" valign="top"><font face="Arial, Helvetica, sans-serif" size="1" color="#FF0000">If a friend referred you to this site, enter their Newgrounds username and get them +5 bonus experience points!</font></td> </tr> --> <tr> <td colspan="4"></td> </tr> <tr> <td colspan="1"></td> <td colspan="2" valign="top"><input type="submit" name="Submit" value="Submit Information" style="background-color: rgb(0,0,0); color: rgb(192,192,192); font-weight: bold"><br> </td> <td colspan="1" nowrap><font face="Arial, Helvetica, sans-serif" size="1" color="#C0C0C0"><strong>Please press "submit" ONCE and wait</strong>.</font> </td> </tr> </table> </form> PHP: <? //replace username and password with your mysql name and password $conn = mysql_connect("localhost","database_db","123"); //select the database $db = mysql_select_db("database_home"); $username = $_POST["user_alias"]; $password = $_POST["user_password"]; $fname = $_POST["user_firstname"]; $lname = $_POST["user_lastname"]; $email = $_POST["user_email"]; $gender = $_POST["user_gender"]; $age = $_POST["user_age"]; //insert the values $result= MYSQL_QUERY("INSERT INTO Users (id, username, fname, lname, password, email, gender, age)". "VALUES ('NULL', '$username', '$fname', '$lname', '$password', '$email', '$gender', '$age')"); echo "Your name and password have been submitted into our database :)"; ?> need anymore help then pm me armstrongecom 08-27-2004, 10:04 AM Here's what I often use, but you have to make the form's field names exactly the same as your mysql table field names (ie case sensitive).... <? //set tablename to be inserted $tablename = "users"; //list table fieldnames that do NOT require '' delimiters (ie numbers) $no_delim_array = array("users_ID"); //collect info from form for inserting and mailing $insertKeys = ""; $insertValues = ""; $mailinfo = ""; for(reset($HTTP_POST_VARS); $key = key($HTTP_POST_VARS); next($HTTP_POST_VARS)) { $postValues[$key] = trim($HTTP_POST_VARS[$key]); $mailinfo .= "$key = $postValues[$key] \n"; if (in_array($key, $no_delim_array)) { if ($postValues[$key] != "") { $insertKeys .= "$key, "; $insertValues .= "$postValues[$key], "; } } else { $insertKeys .= "$key, "; $insertValues .= "'$postValues[$key]', "; } } //trim and set sql insert statement $insertKeys = substr($insertKeys, 0, strlen($insertKeys)-2); $insertValues = substr($insertValues, 0, strlen($insertValues)-2); $insertQuery = "INSERT INTO $tablename ($insertKeys) VALUES ($insertValues)"; //insert info into db //connect to db server require("./inc/conn.inc"); $cResult = mysql_query($insertQuery, $dbLink); //close connection mysql_close($dbLink); //EMAIL NOTIFICATION //SET THESE MAIL VARs $from = "\"From Name\" <fromaddr@domain.com>\n"; $subject = "New User Profile"; $body = "\nA new User has been submitted to the database.\n_____________________________________ \n\nThe following is a copy of the User Profile data that was submitted...\n\n".$mailinfo." \n"; $to = "<toaddr@domain.com>"; //ok mail it mail($to, $subject, $body, $from); ?> -Allen Jouninshinobi 08-28-2004, 08:09 AM I don't exactly understand this. I'm goin to insert my questions straight into the code. I you can answer any plz do. <? //set tablename to be inserted //This is where my datatbase name goes right? $tablename = "users"; //list table fieldnames that do NOT require '' delimiters (ie numbers) //I don't understand this one at all and what are delimiters? $no_delim_array = array("users_ID"); //collect info from form for inserting and mailing //adding keys? adding values? and what is mail info? $insertKeys = ""; $insertValues = ""; $mailinfo = ""; for(reset($HTTP_POST_VARS); $key = key($HTTP_POST_VARS); next($HTTP_POST_VARS)) { $postValues[$key] = trim($HTTP_POST_VARS[$key]); $mailinfo .= "$key = $postValues[$key] \n"; if (in_array($key, $no_delim_array)) { if ($postValues[$key] != "") { $insertKeys .= "$key, "; $insertValues .= "$postValues[$key], "; } } else { $insertKeys .= "$key, "; $insertValues .= "'$postValues[$key]', "; } } //trim and set sql insert statement $insertKeys = substr($insertKeys, 0, strlen($insertKeys)-2); $insertValues = substr($insertValues, 0, strlen($insertValues)-2); $insertQuery = "INSERT INTO $tablename ($insertKeys) VALUES ($insertValues)"; //insert info into db //what information do I need to put in, so the database connects //connect to db server require("./inc/conn.inc"); $cResult = mysql_query($insertQuery, $dbLink); //close connection mysql_close($dbLink); ?> armstrongecom 08-28-2004, 04:30 PM First create a form that contains any or all of the fields from your mysql table, and be sure that the names of the form's fields match the names of your mysql table fields exactly. Now use method="POST" and point the ACTION= statement to the code below on a separate page. This code will dynamically build an INSERT statement and insert the form's values into your table. There are only four things you need to add to this code, and I've modified the original code and placed them in comments at the top so you can make the changes more easily. Look for them in the code below numbered 1 through 4, and let me know if you have any questions. -Allen <? // 1) insert your database name here // $dbName = "database"; // 2) insert your tablename here // $tablename = "users"; // 3) insert your username and password for the DB // $dbUsername = "username"; $dbPassword = "password"; // 4) modify this array so it contains any and all field // //names that have a data type where the values // //don't need to be surrounded by single-quotation // //marks when they're inserted using a SQL statement // //(usually number data types) // $no_delim_array = array("users_ID", "other_ID"); // From this point on, this code doesn't need modification // // However, if you use the mail function I included in the original // // posting, you'd need to modify some of those variables too // $insertKeys = ""; $insertValues = ""; $mailinfo = ""; for(reset($HTTP_POST_VARS); $key = key($HTTP_POST_VARS); next($HTTP_POST_VARS)) { $postValues[$key] = trim($HTTP_POST_VARS[$key]); $mailinfo .= "$key = $postValues[$key] \n"; if (in_array($key, $no_delim_array)) { if ($postValues[$key] != "") { $insertKeys .= "$key, "; $insertValues .= "$postValues[$key], "; } } else { $insertKeys .= "$key, "; $insertValues .= "'$postValues[$key]', "; } } //trim and set sql insert statement $insertKeys = substr($insertKeys, 0, strlen($insertKeys)-2); $insertValues = substr($insertValues, 0, strlen($insertValues)-2); $insertQuery = "INSERT INTO $tablename ($insertKeys) VALUES ($insertValues)"; $dbLink = mysql_connect("localhost", "$dbUsername", "$dbPassword"); mysql_select_db("$dbName", $dbLink); $cResult = mysql_query($insertQuery, $dbLink); //close connection mysql_close($dbLink); ?> Jouninshinobi 08-28-2004, 05:40 PM ok I think I understand it now. BUt now that I have the users information in the MySQL database table, how can I get it out to post on the page and use for various other activities? Thx alot + sorry if my questions seem pointless armstrongecom 08-28-2004, 06:18 PM To get the data back out again, you can use PHP to execute a SELECT query to select the user information you want, and then print it out on the page. However, creating pages like this on a custom basis is not a straight forward process, and usually requires a developer. Depending on what you're trying to do with your website, you might want to explore some of the pre-packaged PHP scripts that use mySQL as a database backend. There are hundreds of them available for the most common applications, and many are free. I suggest you check out a site such as http://www.hotscripts.com to see what's available for your needs before you try to custom code something from scratch. Good luck... -Allen |