how to Keep input value after form submit?

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
itay3
New php-forum User
New php-forum User
Posts: 1
Joined: Sun May 13, 2018 10:59 pm

Sun May 13, 2018 11:10 pm

i tryng to keep input value after submit
and it not working
I attach the code
look in the input value
I tried to do it myself in several ways and it didn't work

index.php file:

Code: Select all

<?php include("form.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Form</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
    <link type="text/css" href="css/style.css" rel="stylesheet">
</head>
<body>

    <p>contact form</p>
    <p>All fields marked with an asterisk (*) are mandatory.</p>

     <?php
                      $fullurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                      if (strpos($fullurl, "?error=empty") == true) {
                        $error_msg = "msg";
                        echo '<div class="alert alert-danger" role="alert">'.$error_msg.'</div>';
                      }
                      elseif (strpos($fullurl, "?error=age") == true) {
                        $error_msg = "msg 2";
                        echo '<div class="alert alert-danger" role="alert">'.$error_msg.'</div>';
                        }
		              elseif (strpos($fullurl, "?error=phone") == true) {
						$error_msg = "msg 3";
                        echo '<div class="alert alert-danger" role="alert">'.$error_msg.'</div>';
					  }
					  elseif (strpos($fullurl, "?error=cellphone") == true) {
						$error_msg = "msg 4";
                        echo '<div class="alert alert-danger" role="alert">'.$error_msg.'</div>';
					  }
                      elseif (strpos($fullurl, "?mailsend") == true) {
                      $error_msg = 'Message sent successfully We will get back to you soon!';
                      echo '<div class="alert alert-success" role="alert">'.$error_msg.'</div>';
                      }
     ?>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="right">
        <div class="form-group">
            <label for="first_name">First name <span class="c1">*</span></label>
            <input name="first_name" type="text" class="form-control" id="first_name" value="<?php echo $_POST['first_name']; ?>">
        </div>
        <div class="form-group">
            <label for="last_name"> Last name</label>
            <input name="last_name" type="text" class="form-control" id="last_name" value="<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ?>">
        </div>
        <div class="form-group">
            <label for="age"> Age</label>
            <input name="age" type="text" class="form-control" id="age" value="<?php echo $age; ?>">
        </div>
        <div class="form-group">
            <label for="phone"> Phone</label>
            <input name="phone" type="text" class="form-control" id="phone" value="<?= isset($_POST['phone']) ? $_POST['phone'] : '' ?>">
        </div>
        <div class="form-group">
            <label for="cellphone"> Mobile</label>
            <input name="cellphone" type="text" class="form-control" id="cellphone">
        </div>
        <div class="form-group">
            <label for="mail">Email <span class="c1">*</span></label>
            <input name="mail" type="email" class="form-control" id="mail" aria-describedby="mail">
            <small id="mail" class="form-text text-muted"><?php
                 $fullurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                 if (strpos($fullurl, "?error=mail") == true) {
                 echo "email invalid";
                 }
                 ?>
            </small>
        </div>
        <div class="form-group">
            <label for="details">Details <span class="c1">*</span></label>
            <textarea name="details" class="form-control" id="details" rows="3"></textarea>
        </div>
        <button class="btn btn-success" id="submit" name="submit">submit</button>
</form>
</body>
</html>
forum.php file:

Code: Select all

<?php
  if(isset($_POST['submit'])) {
   $first_name = $_POST['first_name']; // required
   $last_name = $_POST['last_name'];// not required
   $age = $_POST['age']; // not required
   $phone = $_POST['phone']; // not required
   $cellphone = $_POST['cellphone']; // // not required
   $mail = $_POST['mail']; // required
   $details = $_POST['details']; // required 
   
    if(empty($first_name) || empty($mail) || empty($details)) {
      header("Location: ?error=empty");
	  exit();
  }
    elseif(!preg_match('/^[0-9]*$/', $age) && 1 > $age || $age > 120) {
      header("Location: ?error=age");
	  exit();
     }
	elseif((!empty($phone)) && !(preg_match('/^0?[7][234678]?(\d[0-9]{7})$/', $phone) || preg_match('/^0?[23489]?(\d[0-9]{7})$/', $phone))) {
      header("Location: ?error=phone");
	  exit();
	}
    elseif(!empty($cellphone) && !preg_match('/^0?[5][23457]?(\d[0-9]{7})$/', $cellphone)) {
      header("Location: ?error=cellphone");
	  exit();
     }
	
    elseif(!preg_match('/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/', $mail)) {
      header("Location: ?error=mail");
	  exit();
     }


  else {
    			$mailTo = "mail@mail2342.com";
    			$headers = "From: ".$mail;
    			$subject = "Message";
    			$txt = "First name: ".$first_name.".\nLast name: ".$last_name.".\nEmail: ".$mail.".\nAge: ".$age.".\nPhone: ".$phone.".\nMobile: ".$cellphone.".\ndetails: ".$details;
    			mail($mailTo, $subject, $txt, $headers);
    			header("Location: ?mailsend");
    			exit();
  }
  }
 ?>
Thank you in advance

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed May 30, 2018 10:49 am

Hi,

This might be a typo on your post, but your index.php file includes form.php.
But you've included forum.php?

Otherwise is there one input in particular you having a problem with?

Post Reply