Stalingrad Posted December 6, 2011 Share Posted December 6, 2011 Hi all! I am building a website, and the users can create a pet name. I only want them to be able to use letters, numbers and underscores in the names. How can I do this? Thanks! I would also be using this for the usernames as well. Here is my pet creation code: adopt.php: <?php session_start(); include("config536.php"); ?> <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <?php if(!isset($_SESSION['username'])) { echo "<banner></banner><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content><content><center><font size=6>Messages</font><br><br></center></content>"; } if(isset($_SESSION['username'])) { echo "<banner></banner><nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>Adopt a Pet</font><br><br>"; $thedateis = date('F jS, Y'); $getpetid = $_GET['petid']; $adopt = $_POST['submit']; $petname = $_POST['petname']; $petgen = $_POST['gender']; $thebq = "SELECT * FROM pets WHERE petid='$getpetid'"; $theb = mysql_query($thebq); while($prr = mysql_fetch_array($theb)) { $pid = $prr['petid']; $species = $prr['pname']; $number = $prr['number']; } if(!isset($getpetid)) { $pq = "SELECT * FROM pets WHERE petid != '0'"; $p = mysql_query($pq); while($pr = mysql_fetch_array($p)) { $pid = $pr['petid']; $ptype = $pr['pname']; $im = $pr['pimage']; $number = $pr['number']; $limited = $pr['limited']; $types = $pr['types']; echo "<b>$ptype</b><br><a href=?petid=$pid><img src=/images/pets/$im></a><br>Number: $number<br><br>"; } } if(isset($getpetid)) { ?> <form action="<?php echo "$PHP_SELF"; ?>" method="POST"> Please select a Name: <input type="name" name="petname" maxlength="15"><br> Pet Gender: <select name="gender"><option value="female">Female</option><option value="male">Male</option><br><br><input type="submit" name="submit" value="Adopt Pet"></form> <?php $checkitq = mysql_query("SELECT * FROM userpets WHERE userpetname='$petname'"); $gcheckit = @mysql_num_rows($checkitq); $tq = "SELECT * FROM users WHERE username='$showusername'"; $t = mysql_query($tq); while($rw = mysql_fetch_array($t)) { $grabnum = $rw['pets']; } if(isset($adopt)) { if($grabnum >= "4") { echo "<font color=red>Error! You already have 4 pets.</font>"; } if($petname == "") { echo "<font color=red>Error! Please type in a name for your pet.</font>"; } if($qcheckit == "1") { echo "<font color=red>Error! That pet name already exists!</font>"; } if($gcheckit == "0" && $petname != "" && $grabnum < "4") { mysql_query("INSERT INTO userpets (owner, userpetname, userpetspecies, userpettype, petdatecreated, gender, strength, defence, health, level, booksread, smart, hunger) VALUES ('$showusername', '$petname', '$species', 'Normal', '$thedateis', '$petgen', '1', '1', '10', '1', '0', 'Normal', 'Full')"); mysql_QUERY("UPDATE users SET pets=pets+1 WHERE username='$showusername'"); echo "<font color=green>Success! Your pet has been created!</font>"; } } } } ?> </html> Thanks in advance for the help everyone!! Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/ Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2011 Share Posted December 6, 2011 Validate the input. $field = trim($_POST['your_form_field']); if( !ctype_alnum(str_replace('_', '', $field)) ) { // contains characters other than a-z, A-Z, 0-9 and _ } Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294927 Share on other sites More sharing options...
Stalingrad Posted December 6, 2011 Author Share Posted December 6, 2011 is that if statement sayign it has bad characters in it, or is it sayign that the field contains only a-z and _? thank you! Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294931 Share on other sites More sharing options...
Stalingrad Posted December 6, 2011 Author Share Posted December 6, 2011 Also, what would the variable $field be set as? Would I set that as the $petname variable? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294934 Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2011 Share Posted December 6, 2011 It has unwanted characters if it hits the if() condition . . . Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294935 Share on other sites More sharing options...
Stalingrad Posted December 6, 2011 Author Share Posted December 6, 2011 ok im usign the following if statement, and it isn't working: if( !ctype_alnum(str_replace('_', '', $field)) || $grabnum >= "4" || $petname == "" | $checkit == "1") { echo "<font color=red>Error! Pet names must only have letters, numbers, or underscores.</font>"; } look above to see my old code for what teh other statements are. how do I throw it all into one? Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294938 Share on other sites More sharing options...
Pikachu2000 Posted December 6, 2011 Share Posted December 6, 2011 You know $field was just an example variable name, right? You need to change it to your actual variable name. Quote Link to comment https://forums.phpfreaks.com/topic/252581-how-to-simply-remove-characters-and-spaces-from-a-form-text-box/#findComment-1294949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.