简体   繁体   中英

Hide html when submit button clicked, echo results staying on same page, email form results EXAMPLE PROVIDED

I searched for a basic explanation and example on how to hide my html form "onsubmit" with basic php, while staying on the same page. I also needed to email the form results. I found bits here and there usually complicated and outside of my beginner abilities. So I am going to share a basic example of what I think is the easiest way to obtain this.

HTML Form with php:

<?php
session_start();
//if you require login start session first thing at top

?>
<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script type="text/javascript">


</script>

</head>

<title>Example Form </title>

<?php

//my database connection is in insert.php

include_once 'insert.php';

//Set up email to receive the desired form results

$submit = $_POST['submit'];

$to = "youremail@yourdomain.com";
$email = "youremail@yourdomain.com";
$user = $_SESSION['yoursession']; #this is if require user login
$companyname = $_POST['companyname'];
$companyurl = $_POST['companyurl'];
$ipaddress = $_SERVER['REMOTE_ADDR']; #capture user's ip address
$subject = $companyname;

if(isset($submit) && !empty($companyname || $companyurl)){


$headers = 'From:'. $email . "\r\n"; // Sender's Email
//$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender

$body = "

Company Name: $companyname \n\n Company URL: $companyurl \n\n User IP Address: $ipaddressadvertise

";

if(mail($to, $subject, $body, $headers)){

//What will show after submit button selected...

echo "Successfully submitted!" . "<br />";

echo "<br />" . "<strong>Company Name:  </strong>  " . "&nbsp;" . "$companyname" . "<br />" . "<strong>Company Website:  </strong>  " . "&nbsp;" . "$companyurl" . "<br />";
}else {

    echo "Oops something went wrong. Try again or come back later. " . "<br />";

}
}
?>

<body>

<?php

//This is where you start to wrap what you want to hide onsubmit.

$submit = $_POST['submit'];
if(!isset($submit)){

//Do NOT put closing curly brace leave open and see below.

?>


<form id="form" name="form" action="" method="post" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>


<table border="0">

<tr>Company Name: <align = "center"><input type = "text" id = "companyname" name = "companyname" value = "" placeholder = "Required" required><br /><br />

<tr>Company Website: <align = "center"><input type = "text" id = "companyurl" name = "companyurl" value = "" placeholder = "Required" required><br /><br />


<input type="submit" name="submit" id="submit" onclick = "location.href='mailto:youremail@yourdomain.com';" value="Submit!">

<tr></tr><br /><br />
<tr></tr>
<tr></tr>

</form>

<?php

  } //This is where you put your closing curly brace wrapping all of the information you want to hide when submit button is clicked.

?>
</body>
</html>

When the submit button is clicked the form should disappear, the message displays staying on the same page (without re-direct to another page) while emailing you the results.

NOTE: I tried this in the snippet and it didn't work. But I loaded it to a live site and it works perfectly without errors.

A basic structure can be something like that:

<?php
  if (isset($_POST['submit']))  {   // if submit - send email
    $you = "YOUREMAIL";
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $comment = $_REQUEST['comment'];
    mail($you, $subject, $comment, "From:" . $email);   //send email
    echo "Thank you for contacting us!";    //Email response
  }


  else  {   // else display the form:
?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
      <label for="email">Email: 
        <input name="email" id="email" type="email" />
      </label>
      <label for="subject">Subject: 
        <input name="subject" id="subject" type="text" />
      </label>
      <label for="comment">Comment:
        <textarea name="comment" id="comment" rows="15" cols="40"></textarea>
      </label>
      <input type="submit" value="Submit" />
    </form>
<?php  } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM