I am trying to add a form into my index page, so that when you click on submit it will automatically return to the form when the page reloads. Right now if there are any errors on the page it will display them right above the form as well as give a little thank you message.
I currently have the following for the index.html page:
<?php
include "check.php";
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name:<br/> <input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="30" /><br/><br/>
Email Address:<br/> <input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="30"/> <br/><br/>
Company Name:<br/> <input type="text" name="companyName" value="<?php echo $_POST['companyname']; ?>" size="30" /> <br/><br/>
Message:<br/>
<textarea style="resize: none;" name="message" rows="5" cols="30"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" />
</form>
When I submit the page it will run through the check.php file and verify all the data is good. It should then return the following If/Then statement if all the conditions are met.
if (!$errors) {
$mail_to = 'test#test.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Company Name: ' . $_POST['companyname'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($mail_to, $subject, $message);
echo "Thank you for your email!<br/><br/>";
$_POST = array(); //Clear form after submit
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
Is there anyway to add an auto-function during the "thank you" or "error" echos that will automatically bring the person back down to the contact form?
I hope I explained what I wanted to do correctly. If not please let me know and I will try to clarify it a bit better.
Add an ID to the form and append the anchor onto the form action URL
<form method="post" id="myform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#myform">
Related
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> " . " " . "$companyname" . "<br />" . "<strong>Company Website: </strong> " . " " . "$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 } ?>
I am trying to add a form into my index page, so that when you click on submit it will automatically return to the form when the page reloads. Right now if there are any errors on the page it will display them right above the form as well as give a little thank you message.
I currently have the following for the index.html page:
<?php
include "check.php";
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name:<br/> <input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="30" /><br/><br/>
Email Address:<br/> <input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="30"/> <br/><br/>
Company Name:<br/> <input type="text" name="companyName" value="<?php echo $_POST['companyname']; ?>" size="30" /> <br/><br/>
Message:<br/>
<textarea style="resize: none;" name="message" rows="5" cols="30"><?php echo $_POST['message']; ?></textarea>
<br/>
<input type="submit" name="Submit" />
</form>
When I submit the page it will run through the check.php file and verify all the data is good. It should then return the following If/Then statement if all the conditions are met.
if (!$errors) {
$mail_to = 'test#test.com';
$subject = 'New Mail from Form Submission';
$message = 'From: ' . $_POST['name'] . "\n";
$message .= 'Email: ' . $_POST['email'] . "\n";
$message .= 'Company Name: ' . $_POST['companyname'] . "\n";
$message .= "Message:\n" . $_POST['message'] . "\n\n";
mail($mail_to, $subject, $message);
echo "Thank you for your email!<br/><br/>";
$_POST = array(); //Clear form after submit
} else {
echo '<div style="color: red">' . $errors . '<br/></div>';
}
Is there anyway to add an auto-function during the "thank you" or "error" echos that will automatically bring the person back down to the contact form?
I hope I explained what I wanted to do correctly. If not please let me know and I will try to clarify it a bit better.
Add an ID to the form and append the anchor onto the form action URL
<form method="post" id="myform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#myform">
I'm trying to submit a form inside a fancybox. It is a simple contact us form with name and email and a submit button.
I set the fancybox to iframe and it loads "download.php". This is what "download.php" has:
<form action="sendmail.php" type="post">
<fieldset style="width: 300px;">
<legend>Please fill out completely:</legend>
<span style="color: red;">* required field</span>
<br>
<label for="txtName">Name:</label><br>
<input type="text" id="txtName" style="width: 200px;" name="txtName"><span id="lblName" style="color: red;">*</span><br>
<label for="txtEmail">Email:</label><br>
<input type="text" id="txtEmail" style="width: 200px;" name="txtEmail"><span style="color: red;">*</span><br><br>
<button id="btnSubmit">Submit</button>
<input type="text" id="txtH" style="display: none;" name="txtH">
</fieldset>
</form>
When it submits it works properly other than "txtName" and "txtEmail" being blank. I can't seem to get the values to send. It sends an email after submitting properly otherwise which is why I need to use PHP and not jQuery. Thanks for any help!
Here is the sendmail.php code.
ini_set('SMTP','mail.######.com' );
ini_set('smtp_port', '25');
ini_set('sendmail_from', 'info######.com');
$name = $_POST["txtName"];
$email = $_POST["txtEmail"];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$msgBody = 'The following information was submitted via the download form.' . PHP_EOL . PHP_EOL .
'Name: ' . $name . PHP_EOL .
'Email: ' . $email . PHP_EOL .
'URL: ' . $url . PHP_EOL;
$to = "me#me.com";
$subject = "A White Paper was downloaded";
$headers = "From: info#me.com" . "\r\n";
if (mail($to, $subject, $msgBody, $headers)) {
header('Location: download-success.php');
}
else {
header('Location: same.php');
}
}
You have a mistake here,
<form action="sendmail.php" type="post">
it should be method="post" not type="post"
<form action="sendmail.php" method="post">
and also in your submit button, add type="submit"
<button id="btnSubmit" type="submit">Submit</button>
and here you don't need to set display:none; you can use type="hidden" if you want to hide the input
<input type="hidden" id="txtH" name="txtH">
and In your PHP, use isset
if (isset($_POST['txtName']) {
$name = $_POST["txtName"];
//and rest of the code goes here
}
And always
Add error reporting to the top of PHP file which will help find errors.
error_reporting(E_ALL); //Error reporting enable
ini_set('display_errors',1);
So basically, I want to have someone complete a form, run a php script, and instead of pulling up the php file's web page, I want to append text at the end of the form.
Currently, I have a form:
<form action="scripts/email.php" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
which looks like this:
When 'Submit' is clicked, it leads to my PHP script:
<?php
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
print "Your message has been sent!";
?>
which leads to a blank web page that just says this in plain text:
"Your message has been sent!"
Basically, what I want is text added onto the submit form after the script is ran, such as:
http://i.stack.imgur.com/zha4d.png
How can I achieve running a PHP script on a web page, and instead of loading a new web page of the PHP script's path, but append text on the current HTML web page?
TRY THIS : WRITE THIS CODE INTO A SINGLE PAGE.
<form action="<?php $PHP_SELF ;?>" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'] ))
{
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
}
?>
you can use ajax for acheive more secure and fast execution of script.
HTML code:
<form method="POST">
<p>Name: <input type="text" id="name" name="name" placeholder="Name"></p>
<p>Email: <input type="text" id="email" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" id="subject" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea id="area" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit" onclick="mail_js();">
</form>
<div id="success_msg" style="display:none;"></div>
Just give proper style to this div.
JS code:
function mail_js()
{
var input_val=$('#name').val();
var email_val=$('#email').val();
var subject_val=$('#subject').val();
var message_val=$('#area').val();
jQuery.ajax({
type: 'post',
data:{
'input_val':input_val,
'email_val':email_val,
'subject_val':subject_val,
'message_val':message_val
},
url: 'email.php',
success:function(response){
$("#success_msg").append(response).show();
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
}
PHP script:
$from = $_POST['email_val'];
$to = "comp#c0mp.org";
$subject = $_POST['subject_val'];
$name = $_POST['input_val'];
$message = $_POST['message_val'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
I am trying to create three forms. The way it should work is a form should appear and the user is able to input their info. When the submit button is pressed an email should be sent to the supervisor and the supervisor should click on a link and another form should appear. When the supervisor fills the form and then clicks submit an email should be submitted to the client. the client will click on the link and fill the form out. The client should be then able to send an email to the employee and the both the supervisor and the original user should be able to get the response. However when I keep creating the form the php keeps breaking after the second form. I cant seem to figure out why it keeps breaking in the third form.
here is a snippit of the php code for the second form:
if ($_POST['token'] == "2") {
$m = new mysql($connection_information);
$m->update('hello',array('approval'=>$_POST['approval'],
'comment'=>$_POST['comment'],
'approved_by'=>$_POST['approval_by'],
'approved_date'=>time()),'uid=\''.$_POST['uid'].'\'');
$records = $m->row(array('table' => 'hello','condition' => 'uid=\''.$_POST['uid'].'\''));
$eemail = records['email'];
$supemail = $records['supervemail'];
$clemail = $records['cemail'];
$approvaltime = date("m/d/y g:i a",$records['approved_date']);
$subject = " " . $clemail;
$headers = 'From: ' . $supemail . "\r\n" .
'Reply-To: ' . $supemail . "\r\n" .
'MIME-Version: 1.0' . "\r\n";
if($records['approval'] == 1){
$travel_action = 'approved';
}else{
$travel_action = 'rejected';
}
$message = " Travel Estimation ".$travel_action." on ".$approvaltime." by ".$records['approved_by']. "\r\n" . "Comment: " .$records['comment']. "\r\n";
mail($eemail, $subject, $message, $headers);
Here is my html portion:
<?php if ($_POST['token'] == "2") { ?>
<h1>Approval Decision Submited.</h1>
<?php } else if ($_POST['token'] == "1") {
echo "<h1>Form has been submitted</h1>";
} else {
if (isset($_GET['uid']) && isset($records)){
?>
<form id="approvalForm" name="form2" action="hello.php" method="POST">
<input type="hidden" name="token" value="2">
<input type="hidden" name="uid" value="<?php echo $_GET['uid'] ?>">
<fieldset>
<legend>Manager Approval Required</legend>
Submitted on: <?php echo date("m/d/y g:i a",$records['submitted']) ?><br/>
By: <?php echo $records['email'] ?><br/>
<label for="email">Supervisor's Email: </label>
<input type="text" name="email" title="Email" value="<?php echo $records['supervemail'] ?>"><br>
<label for="email">Client's Email: </label>
<input type="text" name="email" title="Email" value="<?php echo $records['supervemail'] ?>"><br>
<label for="email">Employee's Email: </label>
<input type="text" name="email" title="Email" value="<?php echo $records['supervemail'] ?>"><br>
<label for="approval_by">Please Enter your name for approval: </label>
<input type="text" name="approval_by" id="approval_by" title="Approved By" ><br>
<label for="approval">Please select appropriate action: </label>
<select name="approval" id="approval">
<option value="">Please Select Action</option>
<option value="1">Approval</option>
<option value="0">Rejection</option>
</select>
<label for="comment" >Comment: </label>
<input type="text" name="comment" id="comment" title="comment"><br>
<input class="submit" type="submit" value="Submit"/>
</fieldset>
</form>
In my third form I want the supervisor to send an email to the client so the that the client will receive the link and approve or disapprove in the third form. From there they can submit an email to the user if they agree or disagree. I made it so the third form looks almost identical to the second form. Is that where my fault lies?
I notice that the records array for the eemail variable does not have a dollar sign.
$eemail = records['email'];
Should be
$eemail = $records['email'];