Can someone please help me get a simple working form to work on my site.

Dani AI

Generated

A compact, practical HTML+PHP example follows. It fixes the missing method/action, adds HTML5 input types, a CSRF token, server-side validation/sanitization, and basic header-injection protection. This builds on the quick markup posted by and the tutorial pointers noted by and . Put the front-end on a page (contact.php) and the processor on the server (process.php).

<?php
session_start();
if (empty($_SESSION['csrf_token'])) {
    if (function_exists('random_bytes')) {
        $_SESSION['csrf_token'] = bin2hex(random_bytes(16));
    } else {
        $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(16));
    }
}
?>
<!doctype html>
<html>
<body>
<form method="post" action="process.php" novalidate>
  <label>Name <input type="text" name="name" required></label>
  <label>Email <input type="email" name="email" required></label>
  <label>Phone <input type="tel" name="phone"></label>
  <label>Address <input type="text" name="address"></label>
  <label>Comments <textarea name="comments" rows="5"></textarea></label>
  <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($_SESSION['csrf_token']); ?>">
  <button type="submit">Send</button>
</form>
</body>
</html>
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') exit;
if (!hash_equals($_SESSION['csrf_token'] ?? '', $_POST['csrf_token'] ?? '')) {
    http_response_code(400); echo 'Invalid request'; exit;
}
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = preg_replace('/\D/', '', $_POST['phone'] ?? '');
$address = trim($_POST['address'] ?? '');
$comments = trim($_POST['comments'] ?? '');

$errors = [];
if ($name === '') $errors[] = 'Name is required';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Valid email required';
if (strlen($comments) < 3) $errors[] = 'Comments too short';
if ($errors) { foreach ($errors as $e) echo htmlspecialchars($e)."<br>\n"; exit; }

// prevent header injection
$safeEmail = str_replace(["\r","\n"], '', $email);
$to = 'you@example.com';
$subject = 'Contact form';
$body = "Name: $name\nEmail: $safeEmail\nPhone: $phone\nAddress: $address\n\nComments:\n$comments\n";
$headers = "From: $safeEmail\r\nReply-To: $safeEmail\r\nContent-Type: text/plain; charset=utf-8\r\n";
mail($to, $subject, $body, $headers);
echo 'Thank you, message sent';
?>

Notes: enable PHP error reporting while developing, confirm process.php is reachable and the server runs PHP, and check mail delivery/SMTP on your host. For production, always validate server-side (clients can bypass HTML constraints), use HTTPS, and consider an SMTP library (PHPMailer) instead of raw mail() to improve deliverability and security. If a hosted form builder is preferred, pointed that option out earlier.

Recommended Answers

All 7 Replies

You need to explain better

I need a php and html code for a form that has the fields ,name,email,phone,comments,address

Yeah Farhan386 is absolutely rite......

shucksss... this guy so lazy, you asked for help with just making a simple form?

fine.....here ill give you a chance, but do you homework next time kiddo

<html>
<head>
</head>
<body>
<form>
<table>
<tr>
<td>Name</td><td><input type="text" name="name" /></td>
</tr>
<tr>
<td>E-mail</td><td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Phone</td><td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>Address</td><td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Comments</td><td><textarea name="comments"></textarea></td>
</tr>
</table>
<input type="submit" name="btnsubmit" value="submit" />
</form>
</body>
</html>
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.