Iframe with a submit form in a Fancybox not posting data - php

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);

Related

PHP form validation and scroll up to top of form if there are errors [duplicate]

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">

PHP not mailing any form data

[Edit - added a complete form snippet] In my html - I have a single checkbox - which looks like this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />
<form class="contact-form row" id="feedbacks" method="POST" action="feedback.php">
<div class="col-xs-10 col-xs-offset-1">
<fieldset class="form-group">
<label for="full_name"></label>
<input type="text" class="form-control" placeholder="Your name and surname" name="full_name" id="full_name">
</fieldset>
<fieldset class="">
<label for="feedback_email"></label>
<input type="text" class="form-control" placeholder="Your Email address" name="feedback_email" id="feedback_email">
</fieldset>
<div class="checkbox">
<label class="c-input c-checkbox">
<input type="checkbox" name="subbed" id="subbed">
<span class="c-indicator"></span>
<span class="text-muted m-l-1">subscribe to <abbr class="msa"></abbr> notifcation service.</span>
</label>
</div>
<fieldset>
<label for="message"></label>
<textarea class="form-control" rows="9" placeholder="Your message here.." name="message" id="message"></textarea>
</fieldset>
<div class="row m-t-1">
<fieldset class="col-xs-4 col-xs-offset-4">
<button class="btn btn-primary btn-block btn-lg" name="submit" type="submit" id="send_feedback">Send <i class="ion-android-arrow-forward"></i>
</button>
</fieldset>
</div>
</div>
</form>
<input type="checkbox" name="subscribed" id="subscribed" value="sub_me">
In my PHP, I've created a variable $subscribe which links to the subscribed checkbox.
The PHP is supposed to send an email of "I would not like to recieve news emails" when the checkbox is left alone. To achieve this I have opted to use the following ternary inside the PHP form validation code:
[Edit - Supplying all the PHP]
<?php
$value = '';
$error = "";
$error_message = "";
$info = "";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$subscribe = 'Would ' . (isset($_POST['subbed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
$admin_email = "myemail_address#gmail.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#domain.ac.za>' . "\r\n";
$headers .= 'Reply-To: ' . $feedback_email . "\r\n";
'X-Mailer: PHP/' . phpversion();
$full_name = $_POST['full_name'];
$feedback_email = $_POST['feedback_email'];
$feedback = $_POST['message'];
$rep_message = "some thank you message to " . $full_name;
$message = 'another message which references ' . $subscribe;
$reply = 'some message consisting of ' . $full_name;
mail($admin_email,"Feedback",$message,$headers,"-fforwardguy#gmail.com");
mail($feedback_email,"Feedback",$reply,$headers);
}
?>
[Edit] The Problem is that the form is only acknowledged as having been sent, but no data is received along with it.
[After edit extra info - Could the following things be affecting the form submission?
`
There are 2 forms on the page in question.
Both forms are submitted via AJAX.
The first form functions as expected.
Two mail functions are being used in this single PHP file (handling only one form).
After taking taking a closer look - I have found that the email which gets sent contains none of the text entered into the form.
(The AJAX function works and the success functions are run.)
Try this:
$subscribe = 'Would ' . (isset($_POST['subscribed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
Here I am checking if the $_POST variable is actually set and then we are doing a loose comparison check to see if sub_me is the submitted value.
You could also try leaving the value attribute blank on the form element, then just check if $_POST['subscribed'] is actually set by just using isset($_POST['subscribed']). If the checkbox is NOT checked, isset($_POST['subscribed']) will return false.
Ok - so after I noticed what the actual problem was - I found out that My AJAX function was collecting data from the wrong form.

Jump to HTML anchor automatically when PHP form submits

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">

No values in $_POST variable after Submit HTML form

I have a html form, which is sending the data through POST to the php-file, and the php-file should process the data (make a good looking structure) and send it via mail.
But the $_POST variable is completely empty....
I have this small html form:
<form id="form" method="post" action="formmailer.php">
<div id="input1">
<input name="name" type="text" placeholder="Ihr Name"> <br>
<input name="email" type="email" placeholder="Ihre E-Mail"> <br>
</div>
<div id="input2">
<textarea name="nachricht" rows="10" cols="30"></textarea> <br>
<input type="submit" value="" name="submit" id="submit">
</div>
</form>
And formmailer.php uses these variables:
<?php //
if(isset($_POST['nt']) && isset($_POST['ntb']))
{
//
$an = "info#website.de";
$name = $_POST['name'];
$email = $_POST['email'];
$nachricht = $_POST['nachricht'];
// Mailheader UTF-8
$mail_header = 'From:' . $email . "n";
$mail_header .= 'Content-type: text/plain; charset=UTF-8' . "rn";
// create layout
$message = "
Name: $name
Email: $email
Nachricht: $nachricht
";
// send mail
mail($an, $message, $mail_header );
}
else {
echo("<p>Email delivery failed…</p>");
}
?>
If i use this if-statement
if (isset($_POST["submit"]))
the mail is sending, but completely empty.
Am i blind? It should be really easy, shouldn't it?
isset($_POST['nt']) && isset($_POST['ntb'])
In html I do not find nt and ntb field.
When you use isset($_POST["submit"]). In Post array php find submit value. But you did not enter any value in the form fields that is why you getting empty value in your mail.
Replace this line from formmailer.php
if (isset($_POST['nt']) && isset($_POST['ntb']))
to:
if ($_POST)
I hope this helps.

Please advice why the form is not redirecting to the url?

Please help with this php contact form. It works, however, I want to redirect the visitor to a different .html page instead of showing a thankyou message, but i do not want to lose the contact form info. Thanks !
I am using very simple contact form:
<form method="POST" action="sendmail.php" class="form">
<p class="yourName"> Submit Name:</p>
<input name="text" class="name" placeholder="Name" name="name">
<p class="yourEmail">Submit Email:</p>
<input type="text" class="email" placeholder="Email" name="email">
<p class="yourInquiry">Submit Inquiry:</p>
<input type="submit" value="Submit" class="Button">
</form>
<?php
$to = "xyz#hotmail.com";
$subject = "Inquiry from ". $_POST['name'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$html`enter code here` = "";
foreach ($_POST as $name => $value) {
$html .= "<strong>$name</strong><br>";
$html .= "<p>$value</p><br>";
}
if (mail($to, $subject, $html,$headers)) {
/* Success Message */
} else {
/* Error message */
}
header('Location:about.html');
} else {
echo "You didnt enter anything";
?>
header and related functions (in particular setcookie) MUST appear before ANY content on the page. In this case, your form is sent before it.
You can either rearrange your code so that the form processing appears before the form, or start your code with ob_start to bypass the restriction (but this is only a good idea if you actually intend to pass a callback to the function)
You must not output (or echo) anything before using function header()
Put all your logic on the top of your script and html markup on the bottom, so you would be able to perform header('Location:about.html');
First if you want to redirect using header location you can't show any message like Success Message or Error Message.
Second use header("Location:about.html");
Try it change the order of your code as show below
<?php
if(isset($_POST["yourName"])){
$to = "xyz#hotmail.com";
$subject = "Inquiry from ". $_POST['name'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$html`enter code here` = "";
foreach ($_POST as $name => $value) {
$html .= "<strong>$name</strong><br>";
$html .= "<p>$value</p><br>";
}
if (mail($to, $subject, $html,$headers)) {
/* Success Message */
} else {
/* Error message */
}
header("Location:about.html");
} else {
echo "You didnt enter anything";
}
?>
<form method="POST" action="sendmail.php" class="form">
<p class="yourName"> Submit Name:</p>
<input name="text" class="name" placeholder="Name" name="name">
<p class="yourEmail">Submit Email:</p>
<input type="text" class="email" placeholder="Email" name="email">
<p class="yourInquiry">Submit Inquiry:</p>
<input type="submit" value="Submit" class="Button">
</form>
You cannot output anything before your header statement and you have to change your header() statement to:
header('Location: http://www.example.com/about.html');

Categories