0

Its been hours, I am trying to add validations like Email, area must not be blank but none have worked for me. Here's the files I am working on

This is Register.php file

<form action="finish.php" method="post"/>
<p>First & Last Name: <input type="text" name="Name"/></p>
<p>Country: <input type="text" name="Country"/></p>
<p>Email Adress <div class="comment">a confirmation email will be sent<br> to you at this address</div><input type="text" name="Email"/></p>
<p>How You Hear About us? <select name="How" id="how" class="how">
    <option value="From a Friend">From a Friend</option>
    <option value="From Google">From Google</option>
    <option value="Advertisements">Advertisments</option>
    <option value="Other">Other</option>
    </select>
        </p>

<input id="submit" type="submit" value="Mail It!" />
</form>

Finish.php File

<?php

define('DB_NAME', 'temp');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
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());
     }

$value = $_POST['Name'];
$value2 = $_POST['Country'];
$value3 = $_POST['Email'];
$value4 = $_POST['How'];
$sql ="INSERT INTO tempr (Name, Country, Email, How) VALUES ('$value', '$value2', '$value3', '$value4')";
if (!mysql_query($sql)) {
   die('Error: ' .mysql_error());
}

mysql_close();



?>

The form is connected to the Database, so if I add validation it makes it harder to get the data in the database? can anyone please provide me a solution to add validation?

Thanks

12
  • 1
    Use FILTER_VALIDATE_EMAIL to check for properly formed Email plus you can check if fields are empty by using if(empty($_POST['Name'])) { die("Enter your name"); } that is a VERY BASIC method. There are many other ways of doing this, by simply Googling "php form validation" Commented Oct 5, 2013 at 15:56
  • @Fred-ii- I have done that even saw videos but none worked here. IF it works, it doesn't sends the information to the database. Commented Oct 5, 2013 at 15:58
  • 1
    Plus I strongly suggest that you sanitize your inputs. You are open to injection. Commented Oct 5, 2013 at 15:58
  • @SyedFaizanAli: Protip: enable error reporting -- add ini_set('display_errors',1); error_reporting(E_ALL); to the top of your PHP handler. Commented Oct 5, 2013 at 16:01
  • You didn't mention that "problem" in your question. Pretty sure you didn't say anything about it not being entered in DB. Commented Oct 5, 2013 at 16:01

2 Answers 2

1

You actually face many challenges, which include:

Is the email form element filled in at all?

isset()

Does the email in the form element match the pattern of an email?

filter_var()

How do you protect your db from SQL injection attacks?

escaping for mysql (read the big warning and follow the links)

You'd better get yourself geared up to protect your server and your clients from all kinds of attacks, so google the term FIEO to better understand when to Filter Input and Escape Output.

Sign up to request clarification or add additional context in comments.

Comments

0

I realize that you have asked for a PHP solution, but honestly you should validate the data on teh client side (javascript/jQuery) so that you can return control to the user if necessary without refreshing the page and forcing them to re-enter data.

Are you familiar with this jQuery plugin:

http://jqueryvalidation.org/documentation/

To use it, you will need to load both the jQuery library and the jQueryValidation plugin in the head tags of your document:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
</head>

See this demo

5 Comments

This question isn't tagged jQuery and I don't know how jQuery came into the picture. Anyway, this does not answer the question.
I spent almost 5 hours on PHP but nothing work'd at all. With jQuery, it took less than 10 mins. Thanks!
@gibberish I am not sure but this doesn't validate if the email is correct. You can input anything in the text area and it accepts it? Any solution?
Whilst a client side JS solution can be used to provide a nicer user experience, JS can be turned off, and therefore it is incumbent upon us to also do some server side work. Escaping the data before putting it in the db AT AN ABSOLUTE MINIMUM.
@SyedFaizanAli You will see that the demo correctly validates that the email address is well-formed. Examine their javascript and see how they do it. Alternately, post your code into a new question and we'd be happy to help you (for more points, of course, and a bottle of Prosecco) Help with humor; what more can you ask...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.