Berto82 Posted April 3, 2013 Share Posted April 3, 2013 Hi, I am trying to create a page where users can update their information after it has been pulled from the MySQL database. Here is my code: <?php $host = "localhost"; $username = "USERNAME"; $password = "PASSWORD"; $database = "DATABASE"; $conn = mysql_connect($host, $username, $password) or die ("Could not connect". mysql_error()); $db = mysql_select_db($database, $conn) or die ("Could not select DB". mysql_error()); $uin = $_SESSION[user_id_number]; $uin = mysql_real_escape_string($uin); $sql = ("SELECT * FROM members WHERE id='$uin'"); $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo "<p class=\"lh30\"><span class=\"form\">Name: </span><input class=\"form\" name=\"user_name\" type=\"text\" value=\"" . $row['name'] . "\" /></p>"; echo "<p class=\"lh30\"><span class=\"form\">Email: </span><input class=\"form\" name=\"user_email\" type=\"text\" value=\"" . $row['email'] . "\" /></p>"; echo "<p class=\"lh30\"><span class=\"form\">Phone: </span><input class=\"form\" name=\"user_phone\" type=\"text\" value=\"" . $row['telephone'] . "\" /></p>"; echo "<p class=\"lh30\"><span class=\"form\">Town: </span><input class=\"form\" name=\"user_town\" type=\"text\" value=\"" . $row['town'] . "\" /></p>"; $_SESSION[county] = $row['county']; } ?> The problem that I have is that the inputs use value="" and the row data is set as the value. This means that even if a user blanks out the input field, it still actually contains a value. This results in my form validation thinking that the input field has a value even though the user has actually blanked it out. Below is a JS validation line example: if (document.create_account_form.user_name.value == "") { document.create_account_form.error.value = "Please enter your name."; document.create_account_form.user_name.focus(); return false; } Is using value="" the only way or it there a better way? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/276470-validating-form-after-pulling-data-from-mysql/ Share on other sites More sharing options...
trq Posted April 3, 2013 Share Posted April 3, 2013 the inputs use value="" and the row data is set as the valueSo which is it? Are they empty strings, or are set to the value already stored in your database? They cannot be both. Your code shows they originally display the data that was stored in the database. This means that even if a user blanks out the input field, it still actually contains a value.No it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/276470-validating-form-after-pulling-data-from-mysql/#findComment-1422618 Share on other sites More sharing options...
Berto82 Posted April 3, 2013 Author Share Posted April 3, 2013 My php code will do the following: echo "<p class=\"lh30\"><span class=\"form\">Name: </span><input class=\"form\" name=\"user_name\" type=\"text\" value=\"" . $row['name'] . "\" /></p>"; So if the $row['name'] field's value is ANDY then the input field will have value="ANDY". When the user is viewing the page that called this script they can then see the input field and it will be populated with ANDY. If the user then deleted the name ANDY the input field will look blank to the user but viewing the source code it still has value="ANDY". Therefore the JS validation will skip this input field as an issue, because it see it has a value, even though that value is not actually what is within the input field any more. I hope that makes my question a bit more clear. Quote Link to comment https://forums.phpfreaks.com/topic/276470-validating-form-after-pulling-data-from-mysql/#findComment-1422650 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.