Jump to content

Validating form after pulling data from MySQL


Berto82

Recommended Posts

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.

Link to comment
Share on other sites

the inputs use value="" and the row data is set as the value

So 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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.