, .
, PHP. (submit.php). , . submit.php, . "", , , , . , " ". , . , , URL- . $_GET, , .
Please remember to clear / delete data before inserting into the database.
Hope this helps.
<?php
if (isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$favoriteColor = $_POST['fav_color'];
$comment = $_POST['comment'];
$good_email = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
if(strlen($name) > 0 && strlen($email) > 0 && preg_match("/$good_email/", $email, $regs) && strlen($favoriteColor) > 0 && strlen($comment) > 0){
echo "Everything is okay";
}
else{
$error_message = "There are errors with your form.";
if(strlen($name) == 0){
$error_message .= "<br/>Name is required";
}
if(strlen($email) == 0){
$error_message .= "<br/>Email is required";
}
if(!(preg_match("/$good_email/", $email, $regs))){
$error_message .= "<br/>Email is not valid";
}
if(strlen($favoriteColor) == 0){
$error_message .= "<br/>Favorite color can not be left empty";
}
if(strlen($comment) == 0){
$error_message .= "<br/>Comment is required";
}
header("Location: submit.php?&submittedName=$name&error=$error_message&submittedEmail=$email&submittedColor=$favoriteColor&submittedComment=$comment");
}
}
else{
?>
<html>
<head>
</head>
<body>
<?php
if (isset($_GET['error']))
{
echo "<p style='color:red'>".$_GET['error']."</a>";
}
?>
<form name="my-form" id="my-form" method="post" action="submit.php">
Your name:
<input name="name" value="<?php if($_GET['error']) { echo $_GET['submittedName'];} //if there been an error with form submission display the name the user prevously submitted ?>" size="30" maxlength="255" />
Your email:
<input name="email" value="<?php if($_GET['error']) { echo $_GET['submittedEmail'];} //if there been an error with form submission display the email the user prevously submitted ?>" size="30" maxlength="255" />
Your favorite color:
<select name="fav_color">
<option value="">-select please-</option>
<option value="Black">Black</option>
<option value="White">White</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Yellow">Yellow</option>
</select>
Your comment:
<textarea name="comment" rows="6" cols="35"><?php if($_GET['error']) { echo $_GET['submittedComment'];} </textarea>
<input type="submit" value="Submit" name="submit"/>
</form>
</body>
</html>
<?php
}
?>
MK777 source
share