Hi:
i have contact form that i thought the validation (.js + php) worked fine because i received emails
that were only submitted w/ all fields correctly filled in. i added submission to mysql database, then
found out that empty fields were being submitted to database -- but not email..
this is how validation behaves::

fill in name only == js blocks

fill in valid email only == js blocks

fill in bad email format == js doesnt block

fill in name + email wrong format == js blocks

fill in name + email correct format == js blocks

this link has both .js and php validation files..
http://jsbin.com/ixupuk/1/edit

i dont understand how validation would work for email but not database...
thanx for any help

Recommended Answers

All 13 Replies

Could you post some code from where the insertion into the database happens?

So I suppose this script is not working:

//  usable constants  
define('DB_NAME', 'yyy_ContactForm');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'zzz');
define('DB_HOST', 'localhost');

$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);

if(!$link) {
    die('Could not connect: ' .mysql_error());
}

$db_selected = mysql_select_db(DB_NAME,$link);

if(!$db_selected) {
    die('Can\'t use' . DB_NAME . ':' .mysql_error());
}

//echo 'Connected successfully';

/* $fullname = $_POST['fullname'];
$email = $_POST['email'];
$comment = $_POST['comment']; */

$fullname = mysql_real_escape_string ($_POST['fullname']);
$email = mysql_real_escape_string ($_POST['email']);
$comment = mysql_real_escape_string ($_POST['comment']);

/*~~~~  $error = array(); 
if($_POST[$fullname, $email, $comment] == '') { 
    $error[] = ''; 
}  ~~~~*/

$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', 
'$email', '$comment')";

if (!mysql_query($sql)) {
    die('Error: ' . mysql_error());
}

mysql_close();

Have you checked if any $_POST values are actually submitted to the file in which the code above resides? In other words: are you 100% sure that your $_POST values have been set when you insert them into your database? You could check by, for example, adding the following line:

echo $sql;

underneath this line:

$sql = "INSERT INTO admin_table (fullname, email, comment) VALUES ('$fullname', '$email', '$comment')";

and then see if the values are actually in your query.

k, i did add your line. when i reload form before filling any fields, this output appears at the very top::
INSERT INTO admin_table (fullname, email, comment) VALUES ('', '', '') /**/ .. if i fill out two boxes>submit, i still get that same output. thanx

And what happens if you put this somewhere at the top of your document? (After your session_start(); call, if you use that)

echo '<pre>'; var_dump($_POST); echo '</pre>';

Do you see any $_POST values on your screen when the file is loaded?

at top & before any variables are declared, i put your line:echo '<pre>';
then i reload the form to get this on top :
array(0) {}/**/
thanx for your attention to this..

Well if you are not getting any POST data, there must be something else going on. Could you verify that your <form>'s method property is set to "post"? E.g.: <form method="post" ...>

Then, the action property of your <form> element must be set to the location of the file in which you process your data and insert the record into your database. If the form is then submitted, it sends its data via POST to that file. If that process succeeds, you should not see "array(0) {}" on the next page, but an array filled with data.

So, example:

Page form.php:

<form action="newpage.php" method="post"> (Rest of your HTML)

Page newpage.php:

<?php
// A lot of stuff happens.

// Let's see if the form has successfully been submitted, did we get any data?
echo '<pre>'; var_dump($_POST); echo '</pre>';

// More of your own PHP.

i have <form action="contact.php" method="post" id="comments_form">
comments_form is for js. validation <script>inside the <head>i will add your new line
thanx

Then contact.php should be the file that contains the code to insert stuff into your database. Is that the case here?

yes, the form page
(contact.php)has the code to insert stuff into the database and the php thanx

i moved the entire block that submits to database to the very bottom of all the php code?
i thought there was improvement...i was wrong..

i just now discover that i get completely blank submission to database when the form loads or refreshes..it seems now that is what is really what is going on..even thought i still have a problem, my validation seems like it works, im really sorry that ive wasted your time about this.the form autosubmits upon loading the form..again, im really sorry

Haha no problem of course, you are not to blame :). Stuff like this happens all the time, that's what makes it so damn handy to have a community at your disposal to help you out when you can't seem to figure out the problem by yourself.

Is there anything else you need help with?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.