I've been looking for an answer for hours, and have tried multiple solutions from these boards and others, and I still can't find the answer.
I'm really only looking to receive the checkbox data from the form, by email.
Here's the relevant code from dibs.php:
<form action="mailer.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('from','','RisEmail','subject','','R','verif_box','','R','message','','R');return document.MM_returnValue">
a004 <input type="checkbox" name="dibs[]" value="a004"><br />
z004 <input type="checkbox" name="dibs[]" value="z004"><br />
m003 <input type="checkbox" name="dibs[]" value="m003"><br />
<p>your e-mail address:</p>
<input name="from" type="text" id="from" style="padding:2px; border:1px solid #666666; width:180px; height:12px;" value="<?php echo $_GET['from'];?>">
<input type="hidden" name="subject" value="shoe dibs">
<p>please retype the image below into the box:</p>
<input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid #666666; width:180px; height:12px;">
<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" >
<!-- if the variable "wrong_code" is sent from previous page then display the error field -->
<?php if(isset($_GET['wrong_code'])){?>
<div style="border:1px solid #666666; background-color:#990000; color:#FFFFFF; padding:2px; width:280px;"><b>WRONG VERIFICATION CODE</b></div><br>
<?php ;}?>
<input name="submit" type="submit" style="margin-top:10px; display:block; border:1px solid #666666; width:100px; height:20px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; color="#666666;" value="send your dibs">
</form>
And the code from mailer.php:
<?php
// load the variables form address bar
$subject = $_REQUEST["subject"];
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$verif_box = $_REQUEST["verif_box"];
foreach($_POST["dibs"] as $value) {
$dibs .= "$value\n";
}
// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("email#mydomain.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else {
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<head>
I'm getting "invalid argument" error messages on the result page, and nothing in my email except the sender's IP.
I'm sure I've put the foreach code in the wrong place, but for all intents and purposes, I'm a PHP virgin. This is someone else's basic code.
Thanks so much.
Related
I'm having a very hard time trying to get a php code integrated into a form I've created on wordpress. I've trying 3 different php plugins to no avail. Here is the html and css. Does anyone have any idea where I should be putting the php on wordpress and how I should properly call it through the "action" in the form?
css:
<style>
input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%;
border: 1px solid #E5E5E5; padding: 10px 20px;}
input[name=ffirstname] {width:49%; margin-right:1%; }
input[name=lastname] {width:49%; margin-left:1%; }
input[name=address] {width:65.6667%; margin-right:1%; }
input[name=unit] {width:32.3337%; margin-left:1%; }
input[name=city] {width:49%; margin-right:1%; }
select[name=province] {width:24%; margin-left:1%;}
input[name=postal] {width:24%; margin-left:1%; }
input[name=email] {width:49%; margin-right:1%; }
input[name=phone] {width:49%; margin-left:1%;}
input[class=submit] {
background-color: #f05a28;
color: white;
padding: 8px 20px;
margin-left: 85%;
border-radius: 4px;
border: none; !important;
outline: none; !important ;
cursor: pointer;
box-shadow: none; !important;
}
</style>
html:
<form name"infoform" method="post" action="form-to-email.php" >
<br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/>
<br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br>
<br><input type="text" name="city" placeholder="City"/><select name="province" form="form1">
<option value="ab">AB</option>
<option value="BC">BC</option>
<option value="BC">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="PE">PE</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select><input type="text" name="postal" placeholder="Postal Code"/></br>
<br><input type="text" name="country" placeholder="Country"/></br>
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br>
<br> <br><input type="submit" value="Next"/>
php:
<?php
$name = $_POST['ffirstname'];
$email = $_POST['lastname'];
$message = $_POST['address'];
$to = "joeblow#hotmail.com";
$subject = "My contact form";
$body = " You have received a new";
mail($to,$subject,$body);
?>
Usually I use footer.php to add JS and PHP code, just before the closing tag of the body. But if that does not work try using header.php. Maybe your code needs to be included before the end.
The HTML part you can put inside a page or post without a problem.
If you want to use a plug-in, you can easily do that with Contact form 7.
Check here and here for tutorials.
UPDATE - to add PHP code into Wordpress:
You can add the code between <?php tags, so open your header.php, footer.php or page.php and write your code inside the tag
<?php
//PHP Code here
?>
This way you will see this code in every Wordpress page on your site.
Header and footer are always included on all pages.
If you only need it in post or page use page.php or post.php
If you want control over the placement of php within a Wordpress page then have a look at page templates. You could create a form page template that had the form and the necessary php to deal with it, and then apply that to a given url e.g. /sign-up. That way you wouldn't have to have the form code cluttering up the header.php or footer.php of every page.
With the form itself, you could just have it post to itself, and then check to see whether the form variables are set when the page is loaded using isset($_POST['field_name']). If they're present then you can assume the form was posted, and you need to therefore send the email, otherwise if they're not present then you can assume you need to display the form.
e.g.
<?php if(isset($_POST['ffirstname'])){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields, and then send email
// could drop out of php here and show a thank you message
?>
<p>Thank you for submitting the form</p>
<?php
} else {
// $_POST['ffirstname'] not set, so show form ?>
<form method="post" action="">
<input type="text" name="ffirstname" />
<input type="submit" value="Submit" />
</form>
<?php } ?>
If you want to hook into Wordpress for this, then one way to do it is to add a hidden input called action and set its value to the name of a function in your functions.php file. You'll also need to add some javascript to make an ajax submission to the function that could then do the logic and email sending:
e.g.
// Form Page
<form method="post" action="" id="emailForm">
<input type="text" name="ffirstname" />
<input type="hidden" name="action" value="sendEmail" />
<input type="submit" value="Submit" />
</form>
// Somewhere further down the same page as the form
<script type="text/javascript">
jQuery('#emailForm').on('submit', function() {
e.preventDefault();
jQuery.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: jQuery('#emailForm').serialize(),
success: function(data) {
// Perhaps some UI update here to let the user know
// the submission was successful
},
error: function(error) {
// Do something with the error
}
});
});
</script>
Then in your themes functions.php file:
// Functions.php
function sendEmail(){
// Do some appropriate sanitization for your use case
$ffirstname = htmlspecialchars(trim($_POST['ffirstname']));
//.. repeat for other fields as necessary, and then send email
return wp_mail($recipient, $subject, $body, $headers);
}
add_action('wp_ajax_sendEmail', 'sendEmail');
I am a customer service assistant and I wanted to make a simple form to check the price of products on the webpage, without having to load the whole homepage.
The website is www.homebase.co.uk, and the search URL is http://www.homebase.co.uk/en/homebaseuk/searchterm/.
I want to make a form where it will add the text entered in the form after /searchterm/ without the question mark.
EG if I type in 775199 and press submit/search it will navigate to http://www.homebase.co.uk/en/homebaseuk/searchterm/775199.
Thanks so much for your help all :)
I really appreciate it!
Assuming you are using PHP. I think what you want to do is this:
<?php
//THIS IS THE SAME PAGE WHERE YOUR SEARCH FORM EXISTS
$searchPage = "http://www.homebase.co.uk/en/homebaseuk/searchterm/";
$formAction = "./"; //FORM IS SUBMITTED BACK TO ITSELF...
if(isset($_POST['search_term']){
//BUILD THE REDIRECTION LINK BASED ON THE DEFAULT SEARCH PAGE: $searchPage . $_POST['search_term'])
//AND THEN REDIRECT TO THE REDIRECTION LINK
header("location: " . $searchPage . htmlspecialchars(trim($_POST['search_term'])) );
exit;
}
Your HTML Form might look something like this:
<form method="POST" action="<?php echo $formAction; ?>" >
<input type="text" name="search_term" id="search_term" value="" class="search_term" />
<input type="submit" name"submit" id="submit" class="submit" value="Search" />
</form>
Okay, so I solved the problem:
<style type="text/css">
.biga {
font-family: Arial, Helvetica, sans-serif;
font-size: 36px;
color: #F90;
background-color: #FFF;
border: medium solid #000;
}
}
.centerpoint {
text-align: center;
}
</style>
<title>Product Search</title>
<p>
<div class="centerpoint"> <span class="centerpoint">
<input name="prog_site" type="text" class="biga" id="prog_site" value="" />
<a href="http://"
onclick="this.href=(
'http://www.homebase.co.uk/en/homebaseuk/searchterm/' + document.getElementById('prog_site').value)"
target="_blank">
<input name="so_link" type="button" class="biga" value="Search Product">
</a>
</p>
</span></div>
This is the code I used. Thanks for all of your help!
It always redirects to the success page, but I never receive the email. I've tried rewriting both the PHP and the HTML, but it won't fix anything and I can never find google pages about how to fix this situation (everything appears to be fine, and most people have problems with receiving an email and not a redirect page). I'm not very familiar with PHP but can't find any errors. This is a contact page for a business website.
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$to = "arsilhavy#gmail.com";
$subject = "Contact From $name";
$mailheader = "From: $email \n Phone: $phone \n Message $message \r\n";
mail($recipient, $subject, $mailheader, $formcontent);
if (mail($to, $subject, $body))
header('Location: success.html');
else
print "Something went wrong" ;
?>
HTML:
<div id="copy">
<form action="mail.php" method="POST" name="contact">
<div class="name">Name</div>
<input required type="text" name="name" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="email">Email</div>
<input required type="email" name="email" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="tel">Phone</div>
<input type="tel" name="phone" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="message">Message</div>
<textarea required name="message" rows="6" cols="25" style="border: solid 1px #081B26; width: 200px;" value=""></textarea>
<br />
<input type="submit" value="Send" class="send">
<input type="reset" value="Clear" class="clear">
</form>
</div>
If you want to send Mails from PHP you will never be happy with this way. I strongly recommend to you to use something like PHPMailer (Download and Instructions here: https://github.com/PHPMailer/PHPMailer ) If you have an existing and properly working E-Mail use this one and configure PHP Mailer to log in with this adress and send Mails via SMTP. If you need an example I can provide one to you, but it is pretty strqaight foreward and very well explained on the given link.
You did not receive any E-Mail because (most likely) your E-Mail did NOT pass the spam filters on your destination e-mail adress. There are quite a lot of meta information and in general also certificates required for your mail to pass the spamfilters of most to all e-mail provider.
Are you trying to run this from your development computer? Chances are your PHP installation isn't configured to send mails.
Open your php.ini file.
Search for [mail function]
Edit settings to your needs
I am trying to incorporate an invisible CAPTCHA into my contact form, however I am not sure if it works.
My code is as follows...
HTML
<span class="test-field"><label for="test">Please leave blank</label>
<input type="text" name="test" id="test"/></span>
CSS
.test-field {Display: none}
PHP
<?php
if($_POST["test"]!=""){
header("Location: {$_SERVER[HTTP_REFERER]}");exit;
}
// Get Data
$f_name = strip_tags($_POST['f_name']);
$f_email = strip_tags($_POST['f_email']);
$f_message = strip_tags($_POST['f_msg']);
// Send Message
mail( "me#website.com", "Website Contact",
"Name: $f_name\nEmail: $f_email\nMessage: $f_message\n",
"From: My Website" );
?>
To test it, I did a few tests changing the...
f($_POST["test"]!="")
...to...
f($_POST["test"]="")
My PHP is not very good, but I am guessing this should not send the form through if the field is blank, but it does come through. Is there anything wrong with my PHP?
Thanks in advance.
As far as your specific question it doesn't make any sense. A user CAPTCHA is absolutely useless if it's hidden.
The term CAPTCHA stands for ...
Completely Automatic Public Turing Test to Tell Computers and Humans Apart
Notice it says humans ...
How can we test for human interaction if they can't see the CAPTCHA challenge? You Can't. Never-the-less .... here's a quick mock up of your code ... the CAPTCHA I left out since it make no sense. Take it from here ...
<?php
if ($_POST['test'] == "") {
header("Location: {$_SERVER['HTTP_REFERER']}");
} else {
// Get Data
$f_name = strip_tags($_POST['f_name']);
$f_email = strip_tags($_POST['f_email']);
$f_message = strip_tags($_POST['f_msg']);
}
?>
<html>
<head>
<style type="text/css">
#demo {
width: 250px;
height: 250px;
}
.test-field {
display: none;
width: 200px;
height: 200px;
}
#form label {
width: 50px;
float: left;
}
#form input {
width: 150px;
float: right;
}
</style>
</head>
<body>
<div id="demo">
<form name="form" id="form" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label for="test">Empty </label><input type="text" name="test" id="test"/><br/>
<label for="f_name">Name</label><input type="text" name="f_name" id="f_name"/><br/>
<label for="f_email">Email</label><input type="text" name="f_email" id="f_email"/><br/>
<label for="f_msg">Message</label><textarea rows="15" cols="30" name="f_msg" id="f_msg"></textarea>
<input type="submit" id="submit">
<div class="test-field"></div>
</form>
</div>
</body>
</html>
<?php var_dump($_POST) ?>
Try changing
if($_POST["test"]!=""){
to
if(isset($_POST["test"]) && $_POST["test"]){
I have a problem that I really cannot figure out how to solve. I have 2 different pages of the same project. It allows users to create a form and submit it to a different webpage. And there are 2 versions, one that just request the fields types, names and values, and another that allows users to input the full form manually.
So, as my question stated, in the first case it works perfectly, and in the second case it comes back to the same page, and it's driving me crazy! I will put the code at the end, but now the steps to reproduce the error. So far it's a page hosted for free as I'm using it for testing purposes:
Go to http://saveyourself.vacau.com/form/
Put this in the fields (this is actually a test account for this page):
Page: http://newfutureuniversity.org/
Method: "Post"
Name 1: "User"
Value 1: "User"
Type 1: "text"
Name 2: "Pass"
Value 2: "Password"
Type 3: "Password"
Name 3: "Login"
Value 3: "1"
Type 3: "Hidden".
Press "Show form", and the form will be displayed
Now see the source code of the page, and copy from "<form...>" to "</form>"
Go to this page http://saveyourself.vacau.com/form/manual/ and paste it there.
Press "Show form", and you should get to EXACTLY the same page as you got after step 3 (check the html, it's 100% the same, the only change is the address, /manual/).
Now, here is the trick. Despite both pages have exactly the same html, if you press the button "Send form" after step 3, you would be logged into the new page, but if you press the button "Send form" after step 6 (in /form/manual/ ), that page is refreshed and the form that you pasted is deleted. How is this possible? As far as I know, there is no PHP involved here, as when the form is displayed, it's just plain html and the new page should handle the whole PHP. So, if it receives the same form, it should answer in the same way, right? In the second page it's not used the "$_SERVER['HTTP_REFERER'];" (where the visitor comes from), nor anything similar. Also, there's 0 javascript. I post now the html in case it helps, and some of the PHP (not to put too much code).
So the question is, does anyone have any idea of why the second form doesn't work?
Thank you so much.
HTML code (it's the same in both pages after step 3 or 6):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send form</title>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body style="background: #DDFFFF; width: 80%; height:100%; margin: 0 10% 0 10%; text-align: center;">
<h1>Send form</h1>
<form action="http://newfutureuniversity.org/" method="POST">
Web page: http://newfutureuniversity.org/<br>
Method: POST<br><br>
<table style="margin-left: auto; margin-right: auto;">
<tr>
<td style="text-align: right;">
User:</td><td><input type='text' name='User' value=''></td>
</tr>
<tr>
<td style="text-align: right;">
Pass:</td><td><input type='password' name='Pass' value=''></td>
</tr>
<tr>
<td style="text-align: right;">
Login:</td><td><input type='text' name='Login' value='1'></td>
</tr>
</table><br>
<input type="submit" value="Send form">
</form>
</body>
</html>
Simplified PHP code from the page working ( http://saveyourself.vacau.com/form/ ):
//Header, body and others go before of this
<?php
if ($_POST['sent']==1&&empty($_POST['source'])) //If form is submited
{
$i = 1;
?>
<form action="<?php echo $_POST['page']; ?>" method="<?php echo $_POST['method']; ?>">
Web page: <?php echo $_POST['page']; ?><br>
Method: <?php echo $_POST['method']; ?><br><br>
<table style="margin-left: auto; margin-right: auto;">
<?php
while (!empty($_POST['nam'.$i]))
{ ?>
<tr>
<td style="text-align: right;">
<?php echo $_POST['nam'.$i].":</td><td><input type='".$_POST['typ'.$i]."' name='".$_POST['nam'.$i]."' value='".$_POST['val'.$i]; ?>'></td>
</tr>
<?php
$i++;
}
?>
</table><br>
<input type="submit" value="Send form">
</form>
<?php
}
elseif (!empty($_POST['source'])) //If user just wants to create a form and see the code
{
//I think this bit is irrelevant codereview, so I skipped it.
}
else //Body of the first page shown
{ ?>
<form method="post">
Web page: <input type="text" name="page">
Method to send: <select name="method"><option value="POST">Post</option><option value="GET">Get</option></select><br>
<table style="border: 1px solid #AAA; margin-left: auto; margin-right: auto;">
<tr>
<td>Number</td>
<td>Name</td>
<td>Value</td>
<td>Type</td>
</tr>
<?php for ($i = 1; $i <= 10; $i++)
{ ?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="nam<?php echo $i; ?>"></td>
<td><input type="text" name="val<?php echo $i; ?>"></td>
<td>
<select name="typ<?php echo $i; ?>">
<option value="text">Text</option>
<option value="password">Password</option>
<option value="radio">Radio</option>
<option value="checkbox">Checkbox</option>
<option value="hidden">Hidden</option>
<option value="reset">Reset</option>
<option value="submit">Submit</option>
</select>
</td>
</tr>
<?php } ?>
</table><br>
Don't worry, a submit button is automatically added.<br>
<input type="hidden" name="sent" value="1">
<input type="submit" value="Show form">
<input type="submit" name="source" value="Form code">
</form><?php } ?>
</body>
</html>
Full PHP code from the page not working ( http://saveyourself.vacau.com/form/manual/ ):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send form</title>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body style="background: #DDFFFF; width: 80%; height:100%; margin: 0 10% 0 10%; text-align: center;">
<h1>Send form</h1>
<?php
if ($_POST['sentbrr']==1) //If form is submited
echo stripslashes($_POST['code']);
else //First time
{ ?>
<form method="post">
Enter the code for the form manually<br>
<textarea name="code" style="width: 100%; min-height: 450px;"></textarea><br>
<input type="hidden" name="sentbrr" value="1">
<input type="submit" value="Show form">
</form>
<?php
} ?>
</body>
</html>
If you look at the second (not working) page, you have:
<form method="post">
with no action="" attribute attached to it. Therefore your form does not know where to submit the form to.
Short answer: It's the browser's fault (bug?).
Long answer: Behavior varies in different browsers. After testing on IE9, Firefox, and Safari, this issue seems to be a web-kit bug.
This hypothesis is also backed by the numerious "form/action" issues reported at crbug.com and bugs.webkit.org
Research:
Kypros seems to be right, though the answer is not as obvious.
If you go to the faulty page (the one that does not send the right "Request URL" when form submitted), and try to inspect it (for instance with "chrome Developer Tools") you will see that the form "action" attribute is indeed empty.
This is in contrast with the source code view which shows the attribute is correctly set!
It seems PHP (or the browser?) is protecting us by eliminating the "action" field from the actual DOM, though I can't find the reason why anywhere.
To verify this I changed the code to:
echo '<form action="http://newfutureuniversity.org/" method="POST">';
echo $_POST['code'];
..and pasted in the rest of the form. This submits well. It doesn't seem to be coming from "stripslashes". You could also just pass in "action" as another form input (instead of inside "code") and it also just works:
echo '<form action="'.$_POST['action'].'" method="POST">';
echo stripcslashes($_POST['code']);
After I ran the same scenario in IE9 - the "problem" does not seem to exist! This makes me think that browser security is the culprit. Assuming it's a bug with chrome/web-kit, I opened a bug in crbug.com, updates will be POSTed here.