Answers for "php form"

PHP
1

php form

/* POST */
<?php
/* Receiving form Varibles $_POST */
if(isset($_POST)) {
  echo("Name : ".$_POST["name"]."<br>E-mail :".$_POST["email"]."<br><br>");
}
?>
      
<!-- POST -->   
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>


</body>
</html>
/* GET */
<?php
/* Receiving form Varibles $_GET */
if(isset($_GET)) {
  echo("Name : ".$_GET["name"]."<br>E-mail :".$_GET["email"]."<br><br>");
}
?>
      
<!-- GET -->   
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>


</body>
</html>
<!-- POST is the secure way of using forms ! -->
Posted by: Guest on December-13-2020
0

php forms

<?php 
$errors=['email'=>'','title'=>'','ingredients'=>''];
$email=$title=$ingridients='';

if(isset($_POST['submit'])){
    if(empty($_POST['email'])){
        $errors['email']= 'email is empty <br>';
    }else{
        $email = $_POST['email'];
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $errors['email']= 'email is not valid <br>';
        }
    }
    if(empty($_POST['title'])){
        $errors['title']= 'title is empty <br>';
    }else{
        $title = $_POST['title'];
        if(!preg_match('/^[a-zA-Z\s]+$/',$title)){
            $errors['title']='title is not valid <br>';
        }
    }
    if(empty($_POST['ingredients'])){
        $errors['ingredients']= 'ingredients are empty';
    }else{
        $ingridients = $_POST['ingredients'];
        if(!preg_match('/^([a-zA-Z\s]+)(,\s*[a-zA-Z\s]*)*$/',$ingridients)){
            $errors['ingredients']= 'invalid type <br>';

        }
    }
    if(array_filter($errors)){
        echo 'there is/are an error/s';
    }else{
        header('Location: ../index.php');
    }
}



?>






<!DOCTYPE html>
<html>
	
	<?php include('../template/header.php'); ?>

	<section class="container grey-text">
		<h4 class="center">Add a Pizza</h4>
		<form class="white" action="add2.php" method="POST">
			<label>Your Email</label>
			<input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>">
            <div class="red-text"><?php echo $errors['email']; ?></div>
			<label>Pizza Title</label>
			<input type="text" name="title" value="<?php echo htmlspecialchars($title); ?>" >
            <div class="red-text"><?php echo $errors['title']; ?></div>
			<label>Ingredients (comma separated)</label>
			<input type="text" name="ingredients" value="<?php echo htmlspecialchars($ingridients); ?>">
            <div class="red-text"><?php echo $errors['ingredients']; ?></div>
			<div class="center">
				<input type="submit" name="submit" value="Submit"  class="btn brand z-depth-0">
			</div>
		</form>
	</section>

	<?php include('../template/footer.php'); ?>

</html>
Posted by: Guest on September-20-2021

Browse Popular Code Answers by Language