Sending form contents to cgiemail as well as external page - php

I have a web page, and after the contact form is submitted, the contents are sent to me via cgiemail. The page then redirects to a "success" page that tells the user that their query went through successfully. I would like to personalize this success page. I was wondering how I would go about creating a form that submits:
a) All of the form content via cgiemail
and b) sends some information (such as name) to the success page.
Thank you in advance.
Code sample: (Form)
<form id="ContactForm" method="post" action="http://website.com/cgi-bin/cgiemail/template.txt">
<div>
<div class="wrapper">
<span>Your Name:</span>
<input name="yourname" class="input">
</div>
<div class="wrapper">
<span>Your E-mail:</span>
<input name="email" class="input">
</div>
<div class="textarea_box">
<span>Your Message:</span>
<textarea name="message"></textarea>
</div>
Send
</div>
<input type="hidden" name="success" value="http://website.com/successpage.php" />
</form>
I wish to submit the form to the email as done in theaction, and that works perfectly. The next part, which I don't know how to approach, is submitting the information also to the page successpage.php,and using it there in a banner.

You can user POST or GET for your main form to send additional data to your success page.
feedback.php -> POST -> thankyou.php
In thankyou.php
send email
display thanks and data from feedback.php
What software are you using?

You should not try to make the form send parameters to two location. Let it send the info only to the page that sends the email, and then let that page redirect to your success page, passing some of the info as parameters.

On successful form submission and after you send yourself the email, instead of redirecting the user to a success page, you need to "generate" a page with PHP which contains all the information you want to show.
Here's a basic version of how your form submission page (PHP) should look like:
<html>
<head><title>Form submission</title></head>
<body>
<?
/* validate data */
if (validated_data) {
send_email ();
?>
<b>Hi <? echo $_POST['yourname']; ?>, form submitted successfully!</b>
<?
} else {
?>
<b>Failed!</b>
<? } ?>
</body>
</html>

Related

How do I prefill some fields in a form with data from another form in Wordpress?

I've got a Wordpress site with an inquiry form on its own page and it works fine just fine, but now the marketing people want us to put another form on the Homepage with just name and email address fields. When the user clicks "continue" (the submit button) the site needs to load the Inquiry Form page, with the Name and Email fields pre-filled using the data from the Homepage form.
I can't seem to find a relevant example here or elsewhere online that relates specifically to Wordpress.
Anyone know how to do this?
This answer requires some HTML and JavaScript knowledge. If your WordPress theme allows you to add custom HTML and JS to your pages then this will work for you.
First of all, create your homepage form and set the action attribute to the path of your inquiry form page. I'm assuming it's /inquiry.
<!-- On the home page -->
<form action="/inquiry">
<input type="name" name="email">
<input type="email" name="email">
<button>submit</button>
</form>
When a user fills this form and hits submit it will load your inquiry page and place the homepage form data in the URL as URL parameters (/inquiry?name=John&email=test%40domain.com)
Because the data we want is in the URL, we can use JavaScript to extract it and populate the inquiry form. Make sure that the JavaScript is placed after the form on the page.
<!-- on the inquiry page -->
<!-- form -->
<form id="inquiry">
<input type="text" name="name">
<input type="email" name="email">
<input type="text" name="phone">
<button>submit</button>
</form>
<!-- custom javascript -->
<script>
// Get the query parameters
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries())
// Set the query parameters to the relevant form inputs
if (params.name) {
// ⭐️ Adjust this selector to select your name field
document.querySelector('#inquiry [name="name"]').value = params.name
}
if (params.email) {
// ⭐️ Adjust this selector to select your email field
document.querySelector('#inquiry [name="email"]').value = params.email
}
</script>

Multiple forms using the action variable?

I have a page that has multiple php forms that send me different informations depending on the user and the form. How do I make php differentiat these? I saw something about the actions and I thought maybe it was like having a separate file, but when I created a "questions.php" and copied and pasted my php code into that and then added "action="questions.php"" to the tag, and ran it just as I had before it didn't work. So what is the correct way to do this?
My code is extremely long and filled with words which is why it would be nice to have separate files for it depending on the form instead of all in the top of my main page.
You could build one page as a standard page. in this page you could include the different forms. See the example below:
<!-- These are just example names -->
<div class="form-1">
<?php include "forms/form-1.php"; ?>
</div>
<div class="form-2">
<?php include "forms/form-2.php"; ?>
</div>
<div class="form-1">
<?php include "forms/form-3.php"; ?>
</div>
If you would like to save data that a user puts in a form in lets say a database you should use the <form method="post"> by adding an action to it. You're asking the form to send the user to another page when the submit button is clicked. an example of a form down below.
<form class="form-1" method="post" action="second_page.php">
<label>Name:
<input type="text" class="input" name="name">
</label>
<label>Email:
<input type="email" class="input" name="email">
</label>
<button type="submit">Send!</button>
</form>
Hope this helps!
I just added a / before the questions.php. apparently it had to do with file path? I don't understand it but it works now.
<form action="questions.php" method="post">
<!-- text boxes to get user inputs-->
<button type="submit" value="submit_btn">click here to go to page in action tag
</button>
</form>
If questions.php file is in same folder action="questions.php" is enough. If its inside 'x' folder, correct path should be given as action="x/questions.php"

Form in HTML Email Not Posting Data

I have this question about why my form is not posting data to my PHP script. To unsubscribe from an emailing list I've set up, I send the user that has unsubscribed an email with a form that posts some data.
<html>
<body>
<div>
<p>This is a confirmation message to confirm that you unsubscribed from the sci-eng email list.</p>
<p>Click on the button below to confirm your unsubscription.</p>
<form id="unsubform">
<div class="form-in">
<input type="hidden" id="emailkey" name="emailkey" value="key">
<input type="hidden" id="email" name="email" value="email#domain.com">
<button class="btn" id="submit" type="submit" formaction="http://redlinks.ca/sci-eng/db/unsubscribe.php" formmethod="post">Unsubscribe</button>
</div>
</form>
</div>
</body>
</html>
When on the page that the form is POSTed to, I get the "no data" error that I have set up if there is no data in the $_POST["email"] variable. I have tried using var_dump($_POST), but that just returns with array(0) { } and that isn't working for me.
The thing that confuses me the most is when I copy the exact html from the email, and paste it into a blank page, when I click on submit/unsubscribe it posts the data just fine, and elsewhere on my site I have the exact same script, the only different being the formaction is without the /db in it. The page that one is sent to behaves how it should, showing the propper array of data instead of nothing. That's the only difference.
If it makes any difference, I use Thunderbird for Windows 10 as my email client.
Anyone who thinks they can help would be appreciated :)
Due to security issues forms are not supported and not recommended within emails. Most of email clients will warn the user of a risk or simply will disable it and it will not work, like in your case.
The best practice for enabling unsubscribing is by using a link. You can pass any requierd parameter for unsubscribing on the link, for example http://www.example.com/unsubscribe.php?usermail=mail#gmail.com.
TRY WITH:
<html>
<body>
<div>
<p>This is a confirmation message to confirm that you unsubscribed from the sci-eng email list.</p>
<p>Click on the button below to confirm your unsubscription.</p>
<form id="unsubform" action="http://redlinks.ca/sci-eng/db/unsubscribe.php" method="post">
<div class="form-in">
<input type="hidden" id="emailkey" name="emailkey" value="key">
<input type="hidden" id="email" name="email" value="email#domain.com">
<button class="btn" id="submit" type="submit">Unsubscribe</button>
</div>
</form>
</div>
</body>
</html>
IF you won't use POST method so how your form values could be posted to phpScript. Try the following code.
<html>
<body>
<div>
<p>This is a confirmation message to confirm that you unsubscribed
from the sci-eng email list.</p>
<p>Click on the button below to confirm your unsubscription.</p>
<form id="unsubform" method="POST" action="http://redlinks.ca/sci-eng/db/unsubscribe.php">
<div class="form-in">
<input type="hidden" id="emailkey" name="emailkey" value="key">
<input type="hidden" id="email" name="email" value="email#domain.com">
<button class="btn" id="submit" type="submit" >Unsubscribe</button>
</div>
</form>
</div>
</body>
The default method is GET, not POST; you need to specify
<form method="post" ...>
Maybe a link to this form hosted online might be a better idea; some mail clients do not support showing mails; and maybe more clients might refuse to submit or post them due to security concerns (e.g. webmail interfaces).
Do not send form for confirmation.Try these steps :
Generate unique encrypted key for those email who unsubscribe and store in database
Send confirmation link to particular user email with key as (http://domainurl/confirm.php?key=1533c67e5e70ae7439a9aa993d6a3393)
Now check key for corresponding email and unsubscribe and also remove key from database

How to save form post to session and stay in the same html page?

I have a form in html that post income to incomesave.php where session is initiated and save all the form contents to session.
The problem is that I want to stay in the same page when submitting form all the while saving data to session and use that session values to populate the income history on the same page. How can I stay in the same page and process the data with php?
This is the form inside html:
<form action="lib/php/incomesave.php" method="post">
$: <input type="number" name="price">
type: <input type="text" name="source" >
<input type="submit" value="Submit">
</form>
<div class="row">
<div class="twelve columns">
<div class="title">
Income history
</div>
<div id="incomehistory">
</div>
</div>
</div>
and this is the incomesave.php to save the form data
<?php
session_start();
$price = $_POST['price'];
$source = $_POST['source'];
$_SESSION['price'] = $price;
$_SESSION['source'] = $source;
echo $_SESSION['price']; // print price
echo $_SESSION['source'];
?>
I want to stay on the same page, run the incomesave.php and populate the income history area of the html page with $_SESSION data. How can I do that? any help would be appreciated!
Submit a AJAX requet to the server and modify the DOM with the result.
xhttp.open("GET", "incomesave.php", false);
xhttp.send();
document.getElementById("incomehistory").innerHTML = xhttp.responseText;
Use ajax to post the data form without changing the page and after the ajax call, save the data inside incomesave.php and after that, use callback or success method to change the html using .innerHTML
thank you to all the comments to lead up to the answer!
If you don't want the page to refresh you have to use ajax to complete this. I'm not gonna post a code example since I don't know how familiar you are with ajax post.
Do a research on ajax posting and result processing. If you are familiar with ajax post, let me know so I can do some code example..
IF you just want to stay on same page, check the answer I posted earlier on sessions
Simple session example not working
if you set form action it will redirect you to action url page. On that page you initialize all session data and then redirect it to same page using header like
header('URL');
url will be html page url.

How to stop redirecting to the php page when submitting an HTML form

I've got your run of the mill form, with a PHP script to validate and email it off.
<form id="contactForm" action="contact.php" method="post">
<fieldset>
<p>
<label>NAME:</label>
<input name="name" id="name" type="text" />
</p>
<p>
<label>EMAIL:</label>
<input name="email" id="email" type="text" />
</p>
<p>
<label>COMMENTS:</label>
<textarea name="comments" id="comments" rows="5" cols="20" ></textarea>
</p>
<p><input type="submit" value=" " name="submit" id="submit" /></p>
</fieldset>
<p id="error" class="warning">Message</p>
</form>
The problem is, that when I click submit, it takes me off the page I was on (filling out the form) and takes me to a blank white page - contact.php.
Is there any way I can stay on the original contact.html page after clicking submit and just let the emailing happen in the background?
Ajax is your best best. If not, the quick and dirty way would be to put a php block at the top of you page and put all the stuff from your contact.php in an if(isset($_post['data'])) statement.
Brief example
<?php
if(isset($_POST['variable_from_form']))
{
// send mail, insert in database, etc. stuffs
}
?>
Basically, if there is post data, do something, if not, just write the page as normal
Php is doing the job assigned: it is redirecting to the page you asked for in the action="" tag.
If you need to validate an email or login, you should use the same contact.php form to do both: enter user email and validate and/or login or whatever you want to do.
At the begining of the contact.php use php script to receive submitted fields, validate, sanitize and insert or whatever.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$email=$_POST['email'];
$email=filter_var($id_post, FILTER_SANITIZE_EMAIL);
if(empty($email) )
{$error=['email']="enter a valid email"}
else {
$user_email=$email;
}
}
if(empty($errors) && $_POST)
{
//with $user_email defined and sanitized you can do whatever you want: insert, edit, query, email.
}
elseif ($errors || !$_POST)
{
?>
Here, put all the html form, at the end, close the open php.
<?php
}
?>
You could put an iframe on your page that initially loads a blank page within it and have the form's target be the iframe so it will post into the iframe.
Have the iframe large enough so that you can put a simple message like "Your information has been accepted" can be printed by contact.php
Here's a link that tells you how to do it.
How do you post to an iframe?
you might use javascript (Ajax) to communicate with your server, that way it will continue using the same html file.
Another way it is to rename your html file to .php extension and work on this file.
Good luck!

Categories