Simple php form validation

How can I validate a form using PHP; I want text to appear next to the field with missing input, and stop the page from submitting. Cheers

You can’t stop the page submitting with php. As to where to put error messages, you build the page with php on the server, so you can put them wherever you want.

so aside from that whats the code?


if(isset($form_field_error)) { echo '!!!error'; }

If you want to hire someone to write code, hire someone. If you want help with your php code, then perhaps post what you’ve got already :slight_smile:

Well here’s the html:

<form action="" method="post">
		<label>Name:</label><br/>
		<input type="text" id="name" name="name" /><br/>
		<label>Email:</label><br/>
		<input type="text" id="email" name="email" /><br/>
		<label>Feedback:</label><br/>
		<textarea name="message" id="com" cols="60" rows="10">
		</textarea><br/>
		<input type="submit" name="sub" id="sub" value="Send"/>
		
</form>

This is my php…

<?php
if(!isset($_POST['name'])){
    include 'feedback.html.php';
    exit();
}

if (isset($_POST['sub'])) {
    // process $_POST variables
    $name = $_POST['name'];
    $email = $_POST['email'];
    $feedback = htmlspecialchars($_POST['message']);
    

    //build message
    $message = "<b>Name:</b> $name\\r\
";
    $message .= "<b>Email:</b> $email\\r\
";
	$message .= "<b>Feedback:</b>";
	$message .= "\\r\
\\r\
 <hr/>";   
	$message .= "$feedback";   
	
    include 'redirect.html.php';
    exit();
    
}

//handling a redirect
if (isset($_GET['redir'])) {
	include 'feedback.html.php';
    exit();
}
?>

I’m altogether new to php and would certainly not afford to hire someone either. I guess thats why I subscribed to these forums. Thanks you.

To start with, you need to check if the values are set at all:


 $name = $_POST['name'];

could be something like:


if(empty($_POST['name'])) {
    $errors['name'] = 'Please enter your name.'
}

Once you’ve checked everything, and if there are errors, include the form again and next to every field (in the html), check to see if there are errors for that field.


<label>Name:</label><br/>
<input type="text" id="name" name="name" /><br/>

<?php
if(isset($errors['name'])) {
    echo '<em>' . $errors['name'] . '</em>';
}
?>

However, if you’re very new to PHP, I suggest you spend a while doing some tutorials or maybe even picking upa php book.

We’ll help you if you try to write your own code and need someone to look at it, but we won’t write your code for you. :slight_smile:

I got ‘Build your own database driven website using PHP & mysql’ by kevin yank. But thought I should experiment on my own, this particular area… failed couple times & that’s why I brought my sample code … Initially I just presented the problem & was advised to include my code. I guess I should just go along with the book. Just thought that’s what the forums are for… didn’t mean for people to write entire program for me but rather to show me how so I can apply the things to my own code (I wrote the code I’ve presented).

I think the problem here is the fact that I did not include the code to begin with and probably the way I asked my question too.

Anyways, many thanks I think I’ll see what I can do… case closed!!