Jump to content
Chuks

PHP Contact Form Validation

Recommended Posts

Please, I desiged a contact form with validation, but on running the form the PHP validation codes occupy the  input fields,and as well this  Input:; echo $name; echo"
"; echo $email; echo"
"; echo $website; echo"
"; echo $message; echo"
"; echo $gender; ?> s

appeared at the bottom .

 

The following are the HTML and PHP codes:

<!DOCTYPE html>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// Define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $website = $message = $gender = "";

if ($_SERVER("REQUEST_METHOD") == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {    
        $name="test_input($_POST["name"]);
    }
        // Check if name only contains letters and white space
        if (!preg_match("/^[a-zA-Z]*$/" ,$name} {
            $nameErr ="Only letters and white space allowed";
        }
    }    
        
    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {        
        $email = "test_input($_POST["email"];
    }
        // Check whether email address is well formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr="Invalid email format";
        }
    }
    
    if (empty($_POST["website"])) {
        $website == "";
    } else {
        $website = test_input($_POST["website"]);
    }    
        // Check if the URL address syntax is valid (this regular expression also allows dashes in the URL
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
            $websiteErr = "Invalid URL";
        }
    }    
    if (empty($_POST["message"])) {
        $message = "";
    } else {
        $messsage = test_input($_POST["message"]);
    
    if (empty($_POST["gender"])) {
        $genderErr = "Gender is required";
    } else {
        $gender = tesr_input($_POST["gender"]));
    }
}    
function test_input($data) {
    $data = trim($data);    
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}    
?>

<h2>PHP Form Validation </h2>
<p><span class="error">* required field.</span></p>
<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    Name: <input type="text" name="name" value="<?php echo $name; ?>">
    <span class="error">* <?php echo $nameErr;?></span><br><br>
    
    Email: <input type="text" name="email" value="<?php echo $email; ?>">
    <span class="error">* <?php echo $emailErr;?></span><br><br>
    
    Website: <input type="text" name="website" value="<?php echo $website; ?>">
    <span class="error"> <?php echo $websiteErr; ?></span><br><br>
    
    Comment: <textarea name="message" rows="4" cols="40"> <?php echo $message; ?>"></textarea><br><br>
    
    Gender:
    <input type="radio" name="gender"
    
    <?php if (isset($gender) && $gender=="male") echo "checked";?> Male<br><br>
    
    <input type="radio" name="gender"
    
    <?php if (isset($gender) && $gender=="female") echo "checked";?> Female<br><br>
    <span class="error">* <?php echo $genderErr;?></span><br><br>
    
    <input type="submit" name="submit" value="Submit">
</form>    

<?php
    echo <h2>Your Input:</h2>;
    echo $name;
    echo"<br>";
    echo $email;
    echo"<br>";
    echo $website;
    echo"<br>";
    echo $message;
    echo"<br>";
    echo $gender;       
?>

</body>
</html> 

 

And here follows the output on running the above code:

PHP Form Validation

* required field.

"> Name: *

Email: *

Website:

Comment: <?php echo $message; ?>">

Gender: Male

Female

*
 

Your Input:; echo $name; echo"
"; echo $email; echo"
"; echo $website; echo"
"; echo $message; echo"
"; echo $gender; ?>

Please, I don"t know where my mistake is and need help to get the code work well.

 

Share this post


Link to post
Share on other sites

It sounds like PHP isn't running at all.  Are you testing this on a web server with PHP configured, or are you just double-clicking on the file or something?

Share this post


Link to post
Share on other sites
Quote

please what is wrong with this coding? am getting this error "Fatal error: Uncaught Error: Call to undefined function test_input() in C:\xampp\htdocs\Signup\www\index.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Signup\www\index.php on line 30"

 <!DOCTYPE html>
<html>
<head>
<style>
    .error{color: red;}
</style>
</head>
<body>
   <link rel="stylesheet" type="text/css" href="CSS/default.css">
    <?php
    ini_set('display_error', 1);
    // define variables and set to empty values
    $nameErr = $emailErr = $passwordErr = $websiteErr = $country = $state = $city ="";
    
    $name = $email = $password = $website = $country = $state = $city ="";
    
     function test_jnput($data)
        {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars ($data);
            return $data;
        } 
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        
        if(empty($_POST["name"])) {
            $nameErr = "Name is required";
        } else { 
           $name = test_input($_POST["name"]);
    // check if name only contains letters and white space
                   
    if (!preg_match("/^[a-zA-Z]*$/",$name)){
        $nameErr = "Only letters and white space allowed";
            }
        }
    }
     if(empty($_POST["email"])) {
      $emailErr = "Email is required";  
     } else { 
         $email = test_input($_POST["email"]);
      // check if e-mail address is well formed
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $emailErr = "invalid email format"; 
        }
    }
        
    if(empty($_POST["website"])) {
       $website = "";
    } else {
       $website = test_input($_POST["website"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
            $websiteErr = "invalid URL";
        }
    }
        
    if(empty($_POST["country"])) {
       $country = "";
    } else {
       $country = test_input($_POST["country"]);
    }
    
    ?>
    
    <p><span class ="error">* required field.</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name: <input type="text" name="name" value="<?php echo $name;?>">
    <span class="error">*<?php echo $nameErr;?></span>
    <br><br>
    E-mail: <input type="text" name="email" value="<?php echo $email;?>">
    <span class="error">*<?php echo $emailErr;?></span>
    <br><br>
    Website: <input type="text" name="website" value="<?php echo $website;?>">
    <span class="error"><?php echo $websiteErr;?></span>
    <br><br>
    Password: <input type="password" name="password" value="<?php echo $password;?>">
    <span class="error"><?php echo $passwordErr;?></span>
    <br><br>
    Country: <input type="text" name="country" value="<?php echo $country;?>">
    <span class="error"><?php echo $country;?></span>
    <br><br>
    State: <input type="text" name="state" value="<?php echo $state;?>">
    <span class="error"><?php echo $state;?></span>
    <br><br>
    City: <input type="text" name="city" value="<?php echo $city;?>">
    <span class="error"><?php echo $city;?></span>
    <br><br>
    <input type="submit" name="submit" value="submit">
    </form>
    
    <?php
    echo "<h2>Your Input</h2>";
    echo $name;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $password;
    echo "<br>";
    echo $country;
    echo "<br>";
    echo $state;
    echo "<br>";
    echo $city;
    echo "<br>";
    
    
    ?>
      
    <?php 
    $servername = "localhost";
    $username = "root";
    $password = "adeshina2344";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password);
    
    // Check connection
    
    if ($conn->connect_error) {
        die("Connection failed: ". $conn->connect_error);
        
    }
    
    echo "Connection successful";
    ?> 
        
    
    
</body>
</html>

 

Share this post


Link to post
Share on other sites

In your function declaration you wrote J instead of I.

Quote

function test_jnput($data)

 

  • Haha 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...