0

I got a php form that needs to send info to another page from contact_form.php to user_input.php without losing the validation capabilities.

Here's the form :

<form action="user_input.php" method="post">
<table style="width:35%">
<tr>
<td><p>Name:</td><td><input type="text" name="name" value="<?php echo $_SESSION["name"];?>"><span class="error"> * <?php echo $_SESSION["nameErr"];?></span></p></td></tr>
<td><p>Email:</td><td> <input type="text" name="email" value="<?php echo $_SESSION["email"];?>"><span class="error"> * <?php echo $_SESSION["emailErr"];?></span></p></td></tr>
<td><p>Address:</td><td> <input type="text" name="address" value="<?php echo $_SESSION["address"];?>"><span class="optional"> Optional</span></p></td></tr>
<td><p>Phone: </td><td><input type="tel" name="phone" value="<?php echo $_SESSION["phone"];?>"><span class="error"> * <?php echo $_SESSION["phoneErr"];?></span></p></td><tr>
<td><p>Message: </td><td><textarea name="message" rows="5" cols="40"><?php echo $_SESSION["message"];?></textarea><span class="error"> * <?php echo $_SESSION["messageErr"];?></span></p></td></tr>

Here's the validation part:

if($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["name"])) {
    $_SESSION["nameErr"] = "Name required";
} else {
    $_SESSION["name"] = input($_POST["name"]);
}

if(empty($_POST["email"])) {
    $_SESSION["emailErr"] = "Email required";
} else {
    $_SESSION["email"] = input($_POST["email"]);
    // check if email is valid
    if(!filter_var($_SESSION["email"], FILTER_VALIDATE_EMAIL)) {
        $_SESSION["emailErr"] = "Invalid email";
    }
} 

if(empty($_POST["phone"])) {
    $_SESSION["phoneErr"] = "Phone number required";
} else {
    $_SESSION["phone"] = input($_POST["phone"]);
    if(!preg_match("/^[0-9]/", $_SESSION["phone"]) || strlen($_SESSION["phone"]) > 20) {
        $_SESSION["phoneErr"] = "Invalid phone number";
    }
}
if (empty($_POST["message"])) {
    $_SESSION["messageErr"] = "Message required";
} else {
    $_SESSION["message"] = input($_POST["message"]);
    }
  }
function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

what am I doing wrong it seems that when I send the data on user_input.php the data validation part disappears. And I want on user_input.php a button to redirect the user to the form.

2 Answers2

0

In user_input.php push the received data into session and in contact_form.php prefill form if data in session available. Other option is to do the common validatiin part into a separate file that you include in both files. Or just use Silex from Sensiolabs

RedPoppy
  • 573
  • 4
  • 11
0

CONTACT FORM

<?php session_start();?>
<form action="user_input2.php" method="post">
<table style="width:35%">
<tr>
<td><p>Name:</td><td><input type="text" name="name" value="<?php echo $_SESSION["name"];?>"><span class="error"> * <?php echo $_SESSION["nameErr"];?></span></p></td></tr>
<td><p>Email:</td><td> <input type="text" name="email" value="<?php echo $_SESSION["email"];?>"><span class="error"> * <?php echo $_SESSION["emailErr"];?></span></p></td></tr>
<td><p>Address:</td><td> <input type="text" name="address" value="<?php echo $_SESSION["address"];?>"><span class="optional"> Optional</span></p></td></tr>
<td><p>Phone: </td><td><input type="tel" name="phone" value="<?php echo $_SESSION["phone"];?>"><span class="error"> * <?php echo $_SESSION["phoneErr"];?></span></p></td><tr>
<td><p>Message: </td><td><textarea name="message" rows="5" cols="40"><?php echo $_SESSION["message"];?></textarea><span class="error"> * <?php echo $_SESSION["messageErr"];?></span></p></td></tr></p>
<td><input type="submit" name="btn" value="Submit"></td>
</table>
</form>

USER_INPUT2.PHP

<?php

if($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "skdjksjd";
if(empty($_POST["name"])) {
    $_SESSION["nameErr"] = "Name required";
} else {
    $_SESSION["name"] = input($_POST["name"]);
}

if(empty($_POST["email"])) {
    $_SESSION["emailErr"] = "Email required";
} else {
    $_SESSION["email"] = input($_POST["email"]);
    // check if email is valid
    if(!filter_var($_SESSION["email"], FILTER_VALIDATE_EMAIL)) {
        $_SESSION["emailErr"] = "Invalid email";
    }
} 

if(empty($_POST["phone"])) {
    $_SESSION["phoneErr"] = "Phone number required";
} else {
    $_SESSION["phone"] = input($_POST["phone"]);
    if(!preg_match("/^[0-9]/", $_SESSION["phone"]) || strlen($_SESSION["phone"]) > 20) {
        $_SESSION["phoneErr"] = "Invalid phone number";
    }
}
if (empty($_POST["message"])) {
    $_SESSION["messageErr"] = "Message required";
} else {
    $_SESSION["message"] = input($_POST["message"]);
    }
  }

function input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<a href="img.php">get to back</a>
Arijit
  • 1,506
  • 14
  • 23