Can't get the array to work in form php - php

I have an issue with the checkboxes to get it working.
If none of the checkboxes have been selected, it gives an errormessage, this works fine!
Also the submitting of all other information.
I just want the form now to submit the checkboxes which have been checked into the confirmationemail. I now only get: array.
The contact.php
<?php
/* Set e-mail recipient */
$myemail = "mukies#gmail.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Vul uw naam in aub");
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$comments = check_input($_POST['comments'], "Write your comments");
$formCampagne = check_input($_POST['formCampagne']);
foreach($formCampagne as $option) {
print $option."\n";
}
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Dit e-mail adres is niet juist, voer een juist e-mailadres in.");
}
/* If telephone is not valid show error message */
if (!preg_match("/[0-9\(\)+.\- ]/s", $telephone))
{
show_error("Voer een juist telefoon nummer in");
}
/* If verification code is not valid show error message */
if (strtolower($_POST['code']) != 'mycode') {die('Voer aub de juiste code in, in hoofdletters.');}
/* If no campaign mode is selected, show error message */
if(empty($formCampagne))
{
show_error ("U heeft geen selectie gemaakt uit de campagne opties, selecteer minimaal een van de opties.");
}
/* Let's prepare the message for the e-mail */
$message = "Hi Rick,
Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :)
Name: $yourname
E-mail: $email
Telefoon: $telephone
Offerte aanvraag? $formCampagne
Comments: $comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Gelieve de onderstaande foutmelding door te nemen om uw gegevens correct aan te leveren:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php exit();}
?> }
The Contact.htm where the visitor fills in his/her form:
<html>
<body>
<p>Benodigde velden zijn <b>vetgedrukt</b>.</p>
<form action="contact.php" method="post">
<p><b>Uw naam:</b> <input type="text" name="yourname" /><br />
<b>E-mail:</b> <input type="text" name="email" /></p>
<b>Telefoonnummer:</b> <input type="text" name="telephone" /></p>
<p>Welk soort campagne wilt u informatie over ?<br />
<input type="checkbox" name="formCampagne[]" value="sandwichborden driehoeksborden" />sandwichborden / driehoeksborden<br />
<input type="checkbox" name="formCampagne[]" value="drukwerk banners" />drukwerk / banners<br />
<input type="checkbox" name="formCampagne[]" value="evenementen outdoor" />outdoor promotie / evenemente<br />
<input type="checkbox" name="formCampagne[]" value="internet website social media" />internet / websites / social media <br />
<input type="checkbox" name="formCampagne[]" value="artwork video" />artwork / videopromotie <br />
<input type="checkbox" name="formCampagne[]" value="promo gadgets sampling" />promoteams / gadgets / sampling <br />
<input type="checkbox" name="formCampagne[]" value="mobiele reclame reclame frames" /> mobiele reclame / reclame frames <br /> </p>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p>Validatie code: <input type="text" name="code" /><br />
Vul de tekst <b>MYCODE</b> hierboven in. </p>
<p><input type="submit" value="Send it!"></p>
</form>
</body>
</html>
The thanks.htm page only contains standard text, saying, well... thank you. :)
Can anyone help me out here?
EDIT:
So this would be the correct code ?:
if(isset($formCampagne)) {
echo ".implode(',', $formCampagne)."; // this is the output in the confirmation mail
} else {
echo "U heeft geen selectie gemaakt uit de campagne opties, selecteer minimaal een van de opties.";
But where do I put it? Next to the "Offerte aanvraag?", beacuse this gives an error, as I am in the $message field already.
Sorry I have not yet worked with a isset function yet.

Try replacing
$message = "Hi Rick,
Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :)
Name: $yourname
E-mail: $email
Telefoon: $telephone
Offerte aanvraag? $formCampagne
Comments: $comments
End of message
";
with:
$message = "Hi Rick,
Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :)
Name: $yourname
E-mail: $email
Telefoon: $telephone
Offerte aanvraag? ".implode(',', $formCampagne)."
Comments: $comments
End of message
";
Notice that I've changed the plain $formCampagne with implode(',', $formCampagne), 'cause it is actually an array and you have to make it a string if you want to place it in your e-mail message.
Also notice that if none of the checkboxes are selected, $formCampagne will be NULL, so you should also check if isset($formCampagne)
EDIT:
in my example, $formCampagne should actually be $_POST['formCampagne'], I must admit I never used the check_input() function and do not really know how it behaves on arrays.
You could also try to var_dump() the content of both $formCampagne and $_POST['formCampagne']

You can check for isset($formCampagne) if returns false, that means none of the checkbox is selected, else it will give you array of values.

$message = "Hi Rick,
Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :)
Name: $yourname
E-mail: $email
Telefoon: $telephone
Offerte aanvraag? $formCampagne
Comments: $comments
End of message
";
Instead of $formCampagne, you should try something like this:
$message = "Hi Rick,
Je hebt weer een offerte aanvraag ontvangen voor Limburg Media! :)
Name: $yourname
E-mail: $email
Telefoon: $telephone
Offerte aanvraag? ".implode(', ', $formCampagne)."
Comments: $comments
End of message
";
That should give a list of all the selected checkbox values separated by commas.

Related

Cannot POST php mail (Vue)

I am building a really small webapp in Vue with a simple contact form as component. I would like to use a really basic php script for sending the mail. I am not an expert on PHP but for some reason I can't figure out this bug I get when I try to submit the contact form. It says
Cannot POST /mail_contact.php
HTML/Vue component
<form class="contact" action="mail_contact.php" method="post">
<div class="container">
<input type="text" class="st-input" placeholder="Vul hier jouw naam in" name="name">
<input type="text" class="st-input" placeholder="Vul hier kort en bondig jouw probleem in" name="problem">
<input type="text" class="st-input" name="email" placeholder="Vul hier jouw e-mail adres in">
<input type="text" class="st-input" placeholder="Vul hier jouw telefoonnummer in" name="tel">
<select name="telOrEmail" required><option disabled>Maak een keuze</option><option value="Tel">Telefoon</option><option value="Email">Email</option></select>
</div>
<button class="send-button" type="submit">Verzenden!</button>
</form>
PHP Script
<?php
if(isset($_POST['submit'])) {
//getters from form
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$problem = $_POST['problem'];
$telOrEmail = $_POST['telOrEmail'];
//mail content
$formcontent=" Van: $name \n Telefoonnummer: $tel \n Emailadres: $email \n Problem: $problem \n Terugbellen of Emailen: $telOrEmail";
//ontvanger van mail
$recipient = "g.gijsberts#sqits.nl";
//Onderwerp
$subject = "Contact formulier Sqits-it";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Bedankt voor je bericht! Wij hopen zo snel mogelijk te reageren.";
}
?>
Is there something I just don't see?
Edit
Project structure
index.html (File)
src (Directory) -> App.vue, main.js, router.js, components (directory) -> Contact.vue
dist (Directory) -> build.js, build.js.map, index.html, mail_contact.php
mail_contact.php (File)
So as you can see I've place my php script in the same directory as the main file (Index.html), but also in the directory where the build file is placed. Good point to say is this is a Vue project.
For a weird reason I get a 404 error (on localhost:8080/mail_contact.php) when submitting the form before I am redirected to the blank page with the Cannot POST /mail_contact.php. How is this possible if the file is actually there in my directory?

Swiftmailer won't send my e-mail, but doesn't give any errors

I'm trying to send out a mail using Swiftmailer. I have the following code in which I send out a mail:
if(isset($_POST['submit'])) {
$token = substr(sha1(mt_rand()),0,22);
$resetAccount = "UPDATE members
SET token='".$token."',
token_expire='".date("Y-m-d H:i:s", strtotime('+1 hours'))."'
WHERE username='".$_POST['username']."'
AND email='".$_POST['email']."'";
$exec = $db->query($resetAccount);
if($exec->rowCount()) {
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$recoverylink = "<a href='http://www.".$_SERVER['SERVER_NAME']."/admin/recover.php?token=".$token."'>wachtwoordherstel voor ".$_POST['username']."</a>";
$mailbody = 'Beste '.$_POST['username'].',<br><br>U heef een token voor wachtwoordherstel aangevraagd. Het komend uur kunt u uw wachtwoord veranderen via deze link: '.$recoverylink;
$message = Swift_Message::newInstance()
->setSubject("Wachtwoordherstel voor ".$_POST['username'])
->setFrom(array('cms#'.$_SERVER['SERVER_NAME'] => "Q Sites CMS"))
->setTo(array($_POST['email'] => $_POST['username']))
->setBody($mailbody, 'text/html')
;
$result = $mailer->send($message);
echo "<div class='alert'>Een e-mail met instructies is verzonden naar ".$_POST['email'].".</div>";
}
else {
echo "<div class='alert'>Uw account is niet gevonden.</div>";
}
}
The code get's past the if($exec->rowCount()) { and thus echoes echo "<div class='alert'>Een e-mail met instructies is verzonden naar ".$_POST['email'].".</div>"; but the mail isn't sent. What am I doing wrong?
The form from which input is used:
<form id="login-form" method="post" class="form">
<input type="text" name="username" placeholder="Gebruikersnaam" required >
<input type="email" name="email" placeholder="E-mail" class="email" required >
<input type="submit" name="submit" value="Herstel">
</form>

Php form gives nothing

I'm quite new to php and am trying to make a simple form that mails the form-data to a specified mail address. I used this script here and modified it to my needs but when I try it out it just does nothing. It will go to the php page but nothing else happens and the page is empty.
Here is my HTML form:
<form class="contact" name="contact" method="post" action="./files/php/contact_send.php">
<table id="form">
<tr>
<td class="data-right"><label for="naam"><b>NAAM</b></label></td>
<td class="data-left">
<input type="text" name="naam" size="50" style="border-style:inset"/>
</td>
</tr>
<tr>
<td class="data-right"><label for="mailadres"><b>E-MAILADRES</b></label></td>
<td class="data-left">
<input type="text" name="mailadres" size="50" style="border-style:inset"/>
</td>
</tr>
<tr>
<td class="data-right"><label for="boodschap"><b>BOODSCHAP</b></label></td>
<td class="data-left">
<textarea name="boodschap" cols="39" rows="4" style="border-style:inset"></textarea>
</td>
</tr>
</table>
<input type="image" src="./files/img/stuur.png"
onmouseover="this.src='./files/img/stuur-hover.png'"
onmouseout="this.src='./files/img/stuur.png'"
alt="Stuur" width="150px" name="submit" value="Submit" />
</form>
and my php code:
<?php
if(isset($_POST['mailadres'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "blabla#gmail.com";
$email_subject = "Contact";
function died($error) {
// your error code can go here
echo "Het spijt ons maar er is iets fout gelopen bij het versturen van het formulier";
echo "Hieronder zijn de fouten weergegeven:<br /><br />";
echo $error."<br /><br />";
echo "Verbeter de fouten en probeer opnieuw.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['naam']) ||
!isset($_POST['mailadres']) ||
!isset($_POST['boodschap'])) {
died('Het spijt ons maar het lijkt er op dat er iets mis is gelopen met de gegevens die u heeft ingevuld.');
}
$naam = $_POST['naam']; // required
$mailadres = $_POST['mailadres']; // required
$boodschap = $_POST['boodschap']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$mailadres)) {
$error_message .= 'Het e-mailadres dat u heeft opgegeven is geen geldig e-mailadres.<br />';
}
$naam_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($naam_exp,$naam)) {
$error_message .= 'De naam die u heeft opgegeven is geen geldige naam.<br />';
}
if(strlen($boodschap) < 5) {
$error_message .= 'De opgegeven boodschap is niet lang genoeg, gelieve minstens 5 letters te gebruiken.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Formulier details:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Naam: ".clean_string($naam)."\n";
$email_message .= "E-mail: ".clean_string($mailadres)."\n";
$email_message .= "Boodschap: ".clean_string($boodschap)."\n";
// create email headers
$headers = 'From: '.$mailadres."\r\n".'Reply-To: '.$mailadres."\r\n" .'X-Mailer: PHP/' phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Bedankt om ons te contacteren. We proberen zo spoedig mogelijk met u contact op te nemen.
<?php
}
die();
?>
You have a syntax error at this line:
$headers = 'From: '.$mailadres."\r\n".'Reply-To: '.$mailadres."\r\n" .'X-Mailer: PHP/' phpversion();
You forgot a dot in your string concatination at phpversion(). Change it to:
$headers = 'From: '.$mailadres."\r\n".'Reply-To: '.$mailadres."\r\n" .'X-Mailer: PHP/'. phpversion();
The reason why you got an empty screen is because php is probably not displaying the error. This is a security measure. You can force php to report and display the errors by adding the next lines to the top of your script:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
You should remove these lines in a production environment because it is a security risk.
Replace:
if(!isset($_POST['naam']) ||
!isset($_POST['mailadres']) ||
!isset($_POST['boodschap'])) {
died('Het spijt ons maar het lijkt er op dat er iets mis is gelopen met de gegevens die u heeft ingevuld.');
}
with:
if ( (!isset($_POST['naam'])) ||
(!isset($_POST['mailadres'])) ||
(!isset($_POST['boodschap'])) ) {
died('Het spijt ons maar het lijkt er op dat er iets mis is gelopen met de gegevens die u heeft ingevuld.');
}
You were missing a lot of brackets for this statement.
Also, change the bottom part from:
?>
Bedankt om ons te contacteren. We proberen zo spoedig mogelijk met u contact op te nemen.
<?php
}
die();
?>
To:
echo "Bedankt om ons te contacteren. We proberen zo spoedig mogelijk met u contact op te nemen.";
}
die();
?>

My form, regarding the email bit

I've searched stack but couldn't find something similar. I found a nice php script with a robot attached to the form. It works when I send a message and I do receive it. BUT, the email bit doesn't show up in the mail. Maybe you can find something more that is wrong. It's all in Swedish so nevermind the text :P
<?php
if($_POST){
$to = 'my#mail.com';
$subject = 'Portfolio-mail';
$from_name = $_POST['name'];
$from_email = $_POST['email'];
$message = $_POST['message'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "* Misstänkt för att vara en robot, vad god försök igen!";
else{
if($from_name && $from_email && $message){
$header = "Från: $from_name, $from_email";
if(mail($to, $from_email, $from_name, $message))
$success = "Ditt meddelande har skickats!";
else
$error = "* Du är mänsklig, men det var ett fel med din förfrågan!";
}else
$error = "* Alla fält måste vara ifyllda!";
}
}
?>
And for the input bit:
<form action="" method="POST" autocomplete="off">
<div class="formLeft">
<label>För- & Efternamn:</label>
<input type="text" id="name" name="name" placeholder="John Doe" required="required">
<label>Email-adress:</label>
<input type="text" id="email" name="email" placeholder="Fyll i din email-adress!" required="required">
</div>
<div class="formRight">
<label>Skriv gärna in några ord angående projektet.</label>
<textarea id="textarea" name="message" placeholder="Klicka i denna ruta för att börja skriva..." required="required"></textarea>
<p class="robotic" id="pot">
<label>If you're human leave this blank:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
</p>
<input type="submit" value="Skicka!" />
</div>
</form>
That's it! If you need more information, don't hesitate to ask!
You are using the php mail function incorrectly:
mail($to,$subject,$message,$headers);
You have
if(mail($to, $from_email, $from_name, $message))
With your code you have many of these variables you just aren't using them, it should be
if(mail($to, $subject, $message, $header))
In the future please at least look at the php documentation before posting a question. http://php.net/manual/en/function.mail.php

add extra field to form not working

I have a working recommend to script, I just wanted to add a extra field named fename, now I get an error like this:
Parse error: syntax error, unexpected $end in
this is the html form:
<form action="action.php" method="post">
Uw naam:<br />
<input type="text" name="name" size="25"><br />
<br />
Uw e-mail adres:
<br />
<input type="text" name="email" size="25"><br />
<br />
De naam van uw kennis of vriend:
<br />
<input type="text" name="fename" size="25"><br />
<br />
Het e-mail adres van uw vriend of kennis:<br />
<input name="femail" type="text" size="25"><br />
<br />
Bijzonderheden:<br>
<textarea rows="5" name="recon" cols="75">
</textarea><br />
<input type="submit" name="submit" value="Aanbevelen!">
</form>
This is the action.php
<?
if (!$_POST['name']) {echo "Je moet wel je naam invullen a.u.b."; } else {
if (!$_POST['email']) {echo "Je moet wel je e-mail adres invullen a.u.b."; } else {
if (!$_POST['fename']) {echo "Naam van uw vriend of vriendin invullen."; } else {
if (!$_POST['femail']) {echo "Je moet wel het e-mail adres van uw vriend/familie of kennis invullen. a.u.b."; }
else{
$name=$_POST['name'];
$email=$_POST['email'];
$fename=$_POST['fename'];
$femail=$_POST['femail'];
$recon=$_POST['recon'];
$recon=htmlspecialchars($recon);
$headers = "From: $email\r\nReply-To: $femail\r\n";
PRINT "Bedankt $name dat u ons heeft aanbevolen.";
mail("$femail", "Computerhulp is nabij!", "
Beste $name , $fename heeft u ons aanbevolen.
$recon
",$headers);
}
}
}
?>
You need another } since you added another nested if-else.
However, it'd be far better to refactor using elseif's since those are designed exactly for this type of chaining:
<?php
if (!$_POST['name']) {echo "Je moet wel je naam invullen a.u.b."; }
elseif (!$_POST['email']) {echo "Je moet wel je e-mail adres invullen a.u.b."; }
elseif (!$_POST['fename']) {echo "Naam van uw vriend of vriendin invullen."; }
elseif (!$_POST['femail']) {echo "Je moet wel het e-mail adres van uw vriend/familie of kennis invullen. a.u.b."; }
else {
$name=$_POST['name'];
$email=$_POST['email'];
$fename=$_POST['fename'];
$femail=$_POST['femail'];
$recon=$_POST['recon'];
$recon=htmlspecialchars($recon);
$headers = "From: $email\r\nReply-To: $femail\r\n";
PRINT "Bedankt $name dat u ons heeft aanbevolen.";
mail("$femail", "Computerhulp is nabij!", "
Beste $name , $fename heeft u ons aanbevolen.
$recon
",$headers);
}
?>
When you write a code, keep the block minimum. If you using the IDE, then it will tell you when you missed a }. Use a consistent bracket style and indentation to help you spot a bug.
Here is your code above but will be more readable and easier to find the bug:
<?php
$error = 0;
if (!$_POST['name']) {
echo "Je moet wel je naam invullen a.u.b.";
$error = 1;
}
if (!$_POST['email']) {
echo "Je moet wel je e-mail adres invullen a.u.b.";
$error = 1;
}
if (!$_POST['fename']) {
echo "Naam van uw vriend of vriendin invullen.";
$error = 1;
}
if (!$_POST['femail']) {
echo "Je moet wel het e-mail adres van uw vriend/familie of kennis invullen. a.u.b.";
$error = 1;
}
if ( $error == 0 ) {
$name=$_POST['name'];
$email=$_POST['email'];
$fename=$_POST['fename'];
$femail=$_POST['femail'];
$recon=$_POST['recon'];
$recon=htmlspecialchars($recon);
$headers = "From: $email\r\nReply-To: $femail\r\n";
PRINT "Bedankt $name dat u ons heeft aanbevolen.";
mail("$femail", "Computerhulp is nabij!", "
Beste $name , $fename heeft u ons aanbevolen.
$recon
",$headers);
}
See how I use $error to prevent nested if-else.

Categories