mastercjb Posted June 1, 2009 Share Posted June 1, 2009 I'm making a script so people can change there picture, quote, and username. I need help on how to limit the number of characters they can put into the username field. I dont want them to be able to put more then 20 characters in the box. Here is the full page script im working with. EDIT: It would also be nice if the script could look in my database to see if the username has been taken, and if it is, then tell them it has been. <? include 'header.php'; if (isset($_POST['submit'])) { $avatar = $_POST["avatar"]; $quote = $_POST["quote"]; $username = $_POST["username"]; //insert the values if (!isset($message)){ $result= mysql_query("UPDATE `grpgusers` SET `avatar`='".$avatar."', `quote`='".$quote."', `username`='".$username."' WHERE `id`='".$user_class->id."'"); echo Message('Your preferences have been saved.'); die(); } } ?> <? if (isset($message)) { echo Message($message); } ?> <tr><td class="contenthead"> Account Preferences </td></tr> <tr><td class="contentcontent"> <form name='login' method='post'> <table width='50%' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td height='28'><font size='2' face='verdana'>Avatar Image Location: </font></td> <td><font size='2' face='verdana'> <input type='text' name='avatar' value='<?= $user_class->avatar ?>'> </font></td> </tr> <tr> <tr> <td height='28' align="right"><font size='2' face='verdana'>Quote: </font></td> <td><font size='2' face='verdana'> <input type='text' name='quote' value='<?= $user_class->quote ?>'> </font></td> </tr> <tr> <tr> <td height='28' align="right"><font size='2' face='verdana'>Username: </font></td> <td><font size='2' face='verdana'> <input type='text' name='username' value='<?= $user_class->username ?>'> </font></td> </tr> <tr> <tr> <td> </td> <td><font size='2' face='verdana'> <input type='submit' name='submit' value='Save Preferences'> </font></td> </tr> </table> </form> <? include 'footer.php'; ?> Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/ Share on other sites More sharing options...
br3nn4n Posted June 1, 2009 Share Posted June 1, 2009 You could add a check: <?PHP if(mysql_query("SELECT from `grpgusers` WHERE `uesrname` = '$username'") { echo "Sorry that usename is taken. Please go back and try another"; exit; } ?> Hope that helps a little. Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846689 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Please try this. <? include 'header.php'; if (isset($_POST['submit'])) { $avatar = $_POST["avatar"]; $quote = $_POST["quote"]; $username = $_POST["username"]; // Check whether username is already taken or not $sql = "SELECT COUNT(*) AS `user_count` FROM `grpusers` WHERE `username` = '$username' LIMIT 1"; $rs = mysql_query($sql); // If the SELECT query failed if ( $rs === FALSE ) { // Do your error handling stuff here } else { $row = mysql_fetch_assoc($rs); mysql_free_result($rs); if ($row['user_count'] > 0) { $message = "<strong>$username</strong> already exists with us. Please try a new one."; } } //insert the values if (!isset($message)){ $result = mysql_query("UPDATE `grpgusers` SET `avatar`='".$avatar."', `quote`='".$quote."', `username`='".$username."' WHERE `id`='".$user_class->id."'"); echo Message('Your preferences have been saved.'); die(); } } ?> <? if (isset($message)) { echo Message($message); } ?> <tr><td class="contenthead"> Account Preferences </td></tr> <tr><td class="contentcontent"> <form name='login' method='post'> <table width='50%' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td height='28'><font size='2' face='verdana'>Avatar Image Location: </font></td> <td><font size='2' face='verdana'> <input type='text' name='avatar' value='<?= $user_class->avatar ?>'> </font></td> </tr> <tr> <tr> <td height='28' align="right"><font size='2' face='verdana'>Quote: </font></td> <td><font size='2' face='verdana'> <input type='text' name='quote' value='<?= $user_class->quote ?>'> </font></td> </tr> <tr> <tr> <td height='28' align="right"><font size='2' face='verdana'>Username: </font></td> <td><font size='2' face='verdana'> <input type='text' name='username' maxlength="20" value='<?= $user_class->username ?>'> </font></td> </tr> <tr> <tr> <td> </td> <td><font size='2' face='verdana'> <input type='submit' name='submit' value='Save Preferences'> </font></td> </tr> </table> </form> <? include 'footer.php'; ?> Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846691 Share on other sites More sharing options...
mastercjb Posted June 1, 2009 Author Share Posted June 1, 2009 anupamsaha: That code did not work, I could still have any name, and I could still type however many characters I wanted... br3nn4n: I put in the code and it threw an error, maybe it was my placement, where would you put it in my code? Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846697 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 anupamsaha: That code did not work, I could still have any name, and I could still type however many characters I wanted... br3nn4n: I put in the code and it threw an error, maybe it was my placement, where would you put it in my code? Did you try with the entire code that I provided? I added "maxlength" attribute with the username text box, which is running absolutely fine at my end. Please re-confirm. Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846699 Share on other sites More sharing options...
mastercjb Posted June 1, 2009 Author Share Posted June 1, 2009 Ok Sorry, it works for the max length, but I can still use other peoples names.... Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846719 Share on other sites More sharing options...
aschk Posted June 1, 2009 Share Posted June 1, 2009 Incidently you will still want to check the number of characters in PHP because HTML client side is easily alterable regardless of whatever markup you implement in the page. Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-846729 Share on other sites More sharing options...
mastercjb Posted June 1, 2009 Author Share Posted June 1, 2009 I Found a code that will check the database for me, but it wont change the name lol. Maybe this code will be easier to work with, it changes the sig also, heres that code... <?php include 'header.php'; if (isset($_POST['submit'])) { $avatar = $_POST["avatar"]; if($_POST['name'] == $user_class->username) { //Do nothing } else { $name = str_replace(array("<",">"),"&", $_POST['name']); $name = strip_tags($name); $name = htmlentities($name); if(mysql_num_rows(mysql_query("SELECT id FROM grpgusers WHERE username='".$name."'")) != 0) { echo Message('<center>The username you have entered already exist\'s. Go back and try again.</center><br>'); die; } } $quote = strip_tags($_POST["quote"]); $prosig = strip_tags($_POST["prosig"]); //insert the values if (!isset($message)){ $result= mysql_query("UPDATE `grpgusers` SET `username`='".$name."', `avatar`='".$avatar."', `quote`='".$quote."', `prosig`='".$prosig."' WHERE `id`='".$user_class->id."'"); echo Message('<center>Your preferences have been saved.<br /></center>'); die(); } } ?> <?php if (isset($message)) { echo Message($message); } print "<tr><td class='contenthead'> Account Preferences </td></tr> <tr><td class='contentcontent'> <form name='login' method='post'> <table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'> <tr> <td height='50' align='right'> <font size='2' face='verdana'> UserName </font> </td> <td> <font size='2' face='verdana'> <input type='text' name='name' value='$user_class->username'> </font> </td> </tr> <tr> </tr> <tr> <td height='50' align='right'> <font size='2' face='verdana'> Avatar (Image Location) </font> </td> <td> <font size='2' face='verdana'> <input type='text' name='avatar' value='$user_class->avatar'> (NB: This will also change your login name!) </font> </td> </tr> <tr> </tr> <tr> <td height='50' align='right'><font size='2' face='verdana'>Quote (60 Chars MAX) </font></td> <td><font size='2' face='verdana'> <input type='text' name='quote' maxlength='60' value='$user_class->quote'> </font></td> </tr> </tr> <tr> </tr> <tr> <td align='right'><font size='2' face='verdana'>Profile Signature </font></td> <td><font size='2'> <textarea type='text' name='prosig' cols='53' rows='7' maxlength='2500'>$user_class->prosig</textarea> </font></td> </tr> <tr> <td> </td> <td><font size='2' face='verdana'> <input type='submit' name='submit' value='Save Preferences'> </font></td> </tr> </table> </form>"; include 'footer.php'; ?> Link to comment https://forums.phpfreaks.com/topic/160444-limit-characters-in-a-field/#findComment-847177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.