hey all, ive been working on a php contact form script but i have problem i cant succses to do the "all fields are required" thing, ill be very glad if u guys could complete my script PLEASE!!

this is my php script:

<?php
$recipient = "Email";
$subject = "Contact form";
$location = "sent.html";
$sender = $recipient;
$body .= "Full Name: ".$_REQUEST['Fullname']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";
$body .= "Subject: ".$_REQUEST['choose']." \n";
$body .= "Message: ".$_REQUEST['Text']." \n";
mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
header( "Location: $location" );
?>

THANKS IN ADVENCE!!!

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

@kfirlankry

hey all, ive been working on a php contact form script but i have problem i cant succses to do the "all fields are required" thing, ill be very glad if u guys could complete my script PLEASE!!

What is wrong with the code?

Did you write this code or find it online?

If you found this code online can you post that link so I can actually see the code. So I can have a better understanding what you are having.

ive found it online but weird i cant find it now, ill copy here the html and php scripts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="Shortcut Icon" type="image/x-icon" href="http://globalchatapp.com/favicon.ico">
<title>Globalchat | Coming Soon</title>
<style type="text/css">
.cx {
    font-family: Arial, Helvetica, sans-serif;
}
.fd {
    font-size: 10px;
    font-family: Arial, Helvetica, sans-serif;
    color: #FFF;
    text-align: center;
}
body {
    background-image: url();
    background-color: #FFF;
    background-repeat: repeat;
    text-align: left;
    color: #F00;
}
.fd {
    text-align: left;
}
.fd {
    text-align: left;
    color: #4E6372;
    font-size: 13px;
}
.fjkdfjd {
    font-size: 10px;
}
</style>
</head>
<body text="#999999">
<left>
<form method="POST" action="contactform.php">
  <table width="403" border="0">
    <tr>
      <td width="90"><span class="fd">Full name:</span></td>
      <td width="303"><span class="fd">
        <input name="Fullname" type="text" size="50" />
      </span></td>
    </tr>
    <tr>
      <td><span class="fd">Email: </span></td>
      <td><span class="fd">
        <input name="Email" type="text" size="50" />
      </span></td>
    </tr>
    <tr>
      <td><span class="fd">Subject:</span></td>
      <td><select name="choose" id="choose">
        <option>Choose Subject:</option>
        <option>Bug Report</option>
        <option>Feedback</option>
        <option>Improvements</option>
        <option>Suggestion</option>
      </select></td>
    </tr>
    <tr>
      <td><span class="fd">Message:</span></td>
      <td><textarea name="Text" id="Text" cols="36" rows="5">

the php script

<?php
$recipient = "info@globalchatapp.com";
$subject = "Globalchat Contact Form";
$location = "sent.html";
$sender = $recipient;
$body .= "Full Name: ".$_REQUEST['Fullname']." \r\n";
$body .= "Email: ".$_REQUEST['Email']." \n";
$body .= "Subject: ".$_REQUEST['choose']." \n";
$body .= "Message: ".$_REQUEST['Text']." \n";
mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
header( "Location: $location" );
?>
Member Avatar for Zagga

Hi kfirlankry,

Your form is using the POST method so you should really retrieve the variables using POST instead of REQUEST.
The following code should check to see if any fields were left blank and display a message ($error) if they were. You will need to provide a way for the user to get back to the form to try again.

<?php
$error = "";
if (!isset($_POST['Fullname']) || $_POST{'Fullname'} == ""){
    $error .= "<br />Fullname is required";
}
if (!isset($_POST['Email']) || $_POST{'Email'} == ""){
    $error .= "<br />Email is required";
}
if (!isset($_POST['choose']) || $_POST{'choose'} == ""){
    $error .= "<br />Subject is required";
}
if (!isset($_POST['Text']) || $_POST{'Text'} == ""){
    $error .= "<br />Messgae is required";
}
if ($error == ""){
    $recipient = "info@globalchatapp.com";
    $subject = "Globalchat Contact Form";
    $location = "sent.html";
    $sender = $recipient;
    $body .= "Full Name: ".$_POST['Fullname']." \r\n";
    $body .= "Email: ".$_POST['Email']." \n";
    $body .= "Subject: ".$_POST['choose']." \n";
    $body .= "Message: ".$_POST['Text']." \n";
    mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
    header( "Location: $location" );
    exit();
} else {
    echo $error;
}
?>
commented: You beat me to it! +7
Member Avatar for LastMitch

@kfirlankry

ive found it online but weird i cant find it now, ill copy here the html and php scripts

I don't see any issue at all?

The only curious issue is the header() function? Is this part of the code. You don't really need the header() function for this contact form at all.

I assume the php script is contactform.php?

Regarding about this

$location = "sent.html";

It should be your website domain name not a html file.

Try change it from this:

$location = "sent.html";

to this:

$location = $_POST['-.-.-'];

Instead of $_REQUEST try $_POST

From this:

$body .= "Full Name: ".$_REQUEST['Fullname']." \r\n";
$body .= "Email: ".$_REQUEST['Email']." \n";
$body .= "Subject: ".$_REQUEST['choose']." \n";
$body .= "Message: ".$_REQUEST['Text']." \n";

to this:

$Fullname .= "Full Name: ".$_POST['Fullname']."\n";
$Email .= "Email: ".$_POST['Email']."\n";
$choose .= "Subject: ".$_POST['choose']."\n";
$Text .= "Message: ".$_POST['Text']."\n";

Create a new web page and name it contactform.php. Your contactform.php script should be like this:

<?php

        if(isset($_POST['Fullname'])) && $_POST['Fullname'] !='')
        $fullname=$_POST['Fullname'];

        if(isset($_POST['Email'])) && $_POST['Email'] !='')
        $email=$_POST['Email'];

        if(isset($_POST['choose'])) && $_POST['choose'] !='')
        $subject=$_POST['choose'];

        if(isset($_POST['Text'])) && $_POST['Text'] !='')
        $message=$_POST['Text'];

        mail("receiver's email address",$subject,
        $message\r\n\r\n
        $fullname

        ,"From: $email");

  ?>

In the mail function, where i typed receiver's email address, type the actual email address of the intended receiver inside the quotes e.g myjv@gmail.com. This should be the email address of the organization that the site's visitor is trying to submit his contact details to. The email address typed in the contact form is supposed to be the email address of whoever is filling the contact form.

As simple as that.

thanks everybody u helped me alot and open my knowledge even more :) thanks !

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.