Im trying to write a contact form and so far everything works only theres a multiple checkbox in the form and im unsure how to call all data and so my email returns 'array' for the variable 'service'
My code is...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$service = $_POST['service'];
$description = $_POST['description'];
$location = $_POST['location'];
$to = "liam#.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Number: $number,
Service: $service,
Description: $description
Location: $location
";
//end of message
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $email\r\n";
mail($to, $subject, $message, $headers);
?>
My form for the service input is...
<dt><input type="checkbox" value="Static guarding" name="service[]" class="cbox">Static guarding</dt>
<dt><input type="checkbox" value="Mobile Patrols" name="service[]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[]"class="cbox">Alarm response escorting</dt>
<dt><input type="checkbox" value="Alarm response/ Keyholding" name="service[]"class="cbox">Alarm response/ Keyholding</dt>
<dt><input type="checkbox" value="Other" name="service[]"class="cbox">Other</dt>
<dt><input type="hidden" value="Other" name="service[]"class="cbox"></dt>
You can process the checkbox values into a string with implode():
$checkboxes = implode(',', $_POST['service']);
$message = <<<EOL
...
Service: $checkboxes
EOL;
Note the use of a heredoc - it's a MUCH nicer way of defining a multi-line string than using a regular quoted string.
Because you appended your form input names with brackets, PHP will form an indexed array out of them.
$sCount = count($service);
for ($s=0; $s<$sCount; $s++) {
echo $service[$s];
}
You might be better off creating an associative array:
<dt><input type="checkbox" value="Mobile Patrols" name="service[mobile]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[escorting]"class="cbox">Alarm response escorting</dt>
That way, in your script, you could access them as:
echo $service['mobile'];
echo $service['escorting'];
Note that empty checkboxes aren't submitted so you'll want to first check if the array element is set.
Related
I am trying to send value of a variable named "topic" from one php file to another php file ... there is no error in the code but the value of variable is not displaying in the other php file ...
please help ... thank you
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$topic = $_POST['topic'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = "From:" . $from;
session_start();
$_SESSION['top'] = $topic;
mail($to,$subject,$message,$headers);
?>
another php file is
<?php
session_start();
$topic = $_GET['top'];
?>
<h1><center>Meeting Invitation</center></h1>
<form action="my.php" method="post">
You are invited for the meeting on <?php echo $topic;?>
proposed dates are :<br><br>
Date Time<br>
10 jan,2015 10.00 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val1") echo "checked";?> value = "val1" checked="true" ><br>
12 feb,2015 12.15 am<input type = "radio" name = "dat" <?php if
(isset($dat) && $dat=="val2") echo "checked";?> value = "val2" ><br><br>
Proposed locations are :<br>
Location 1 : Islamabad <input type = "radio" name = "location" <?php
if (isset($location) && $location=="val1") echo "checked";?> value = "val1" checked="true" >
<br>
Location 2 : Rawalpindi <input type = "radio" name = "location" <?php if
(isset($location) && $location=="val2") echo "checked";?> value = "val2" ><br><br>
Do you want travel facility ?
<input type="checkbox" name="check_list1" value="yes"> <br><br>
Do you want hotel facility ?
<input type="checkbox" name="check_list2" value="yes"> <br><br><br>
<input type="submit" name="send" value="Send Response">
<input type="reset" >
</form>
<?php
?>
It looks like you are using $_GET instead of $_SESSION in the second file.
Change
$topic = $_GET['top'];
to
$topic = $_SESSION['top'];
I have a simple form, inside it there is check-boxes and multi selection inputs.
The problem I am having is:
The multi selection input only submits the last selection!
The checkboxes only works if I set a value="" but shouldn't work without it?
Here is the form:
<fieldset>
<p>Multi selection</p>
<select name="dropdown2[]" class="select multi-select " data-placeholder="Choose an option" multiple="multiple">
<optgroup label="Section">
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
</optgroup>
<optgroup label="Section">
<option>Drop Down Option A</option>
<option>Drop Down Option B</option>
</optgroup>
</select>
</fieldset>
<fieldset>
<p>Optional checkboxs</p>
<label class="checkbox">
<input name="11" type="checkbox"><span><i></i></span>
<p>CheckA</p>
</label>
<label class="checkbox">
<input name="11" type="checkbox"><span><i></i></span>
<p>CheckB</p>
</label>
</fieldset>
Here is the PHP:
<?php
// Only start sessions if they haven't been already to prevent errors
if (empty($_SESSION)){session_start();}
// If 'data' var was received via POST from form-validation.js
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// There's not really a need for this line with modern browsers
ob_start();
// Open the div around the message
$message = "<div style=\"styling stuff\">";
// Loop through every single post value
foreach ($_REQUEST as $key => $value) {
// If it's not empty
if (!empty($value)) {
// Change the name attributes to look a bit more human-readable
$thisKey = str_replace("-", " ", str_replace("_", "|", $key));
// Populate the message var
$message .= "<strong>" . $thisKey . ":</strong> " . $value . "<br />";
}
}
// Close the div around the message
$message .= '</div>';
// Mail variables
$to = 'email#email.com';
$subject = 'New Message';
$headers = "From: email#email.com\r\n";
$headers .= "Reply-To: email#email.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Attempt to send
$sendMail = #mail($to, $subject, $message, $headers);
// If it fails...
if (!$sendMail) {
// Terminate processing with error
die("There was a problem sending the email");
} else {
// Terminate processing with success msg
die("Email was sent!");
}
// As above, no real need for this line with modern browsers
ob_flush();
// Terminate
die();
}
?>
Live preview: http://loaistudio.com/contact
If you want to send multiple values via checkboxes, then they should be used in this way
<input name="11[]" type="checkbox">
<input name="11[]" type="checkbox">
instead of
<input name="11" type="checkbox">
if you need to submit multi check box then
use like this
<input name="11[]" type="checkbox" value="someval">
<input name="11[]" type="checkbox" value="someval" >
this will post checkbox elements as array
to retrieve
$chk = $_POST['11']; // this will be array
iterate through this array you will get all checked elements of name 11
still you need to provide some values to check box to work this
When i send some email, it only sends the first word from the subject:
Example: I send an email with the name of "Send email", it only sends the word "Send", how can i prevent that?
Code:
The subject is selected in a select box
<td><a>
<?= ' '.$val['email'].' '; ?>
<input type="text" value="<?= $val['email']; ?>" name="to"/>
</a></td>
<td>
<select name="subject">
<?php
foreach($templatesarr as $val){
echo '<option value='.$val['name'].'>'.$val['name'].'</option>';
}
echo '</select></td>
<td><input type="submit" name="send" value="Send e-mail" /></td>
</td></tr>
';
?>
</form>
Check if the button was clicked
if(isset($_POST['send'])){
$sendername = 'Company';
$from = 'noreplay#compaty.com';
$to = safe($_POST['to']);
$subject = safe($_POST['subject']);
$message = 'teste';
$headers = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
$headers .= "From: $sendername <$from>".PHP_EOL;
mail($to, $subject, $message, $headers);
}else{
$to = NULL;
}
Safe function
function safe($value) {
return mysql_real_escape_string($value);
}
The main problem is caused by not escaping variables and not properly encapsulating your attribute values; use this instead:
foreach($templatesarr as $val){
printf('<option value="%1$s">%1$s</option>',
htmlspecialchars($val['name'], ENT_QUOTES, 'UTF-8')
);
}
It now uses "" to delimit the value attribute of the option element.
I have a form with the following code in a php file:
<select id="choice" name="choice" class="ddl" style="width: 121px">
<?php
foreach($xml->children() as $pizza){
?>
<option value="<?php echo $pizza; ?>" selected=""><?php echo $pizza; ?></option>
<?php }?>
and the following Jquery code
$("#addbt").click(function () {
$('#choice').clone()
.attr('id', 'choice' + $('.ddl').length)
.attr('name', 'choice' + $('.ddl').length)
.insertAfter(".ddl:last"); });
I need to send the data in the form (Other data isn't a problem) of an email. The form has an add button that creates new drop down lists with different names (choices, choices1, choices2 and so on depending on how much the button was clicked).
The mail.php where the info is send is:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$choice = $_POST['choice'];
$phone= $_POST['phone'];
$town= $_POST['town'];
$formcontent="
Name: $name \n
Number: $phone \n
Choice: $choice \n
Town: $town \n
Email: $email" ;
$recipient = "courses#os4u.eu";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
I don't know how to add the new choices (choices1, chocies2..etc) into the form in a functional way.
You can loop through the post values:
foreach($_POST as $key => $value) {
$options .= ucfirst($key).": ".$value."\n";
}
Then add the contents of $options to you mail body.
Just a quick solution..
Be aware, all post elements will be added to the body this way..
I have set up a contact form, and set it to email the response to an email account. Part of the form is a series of checkboxes, and I need to get these to display in the email as a list. This is the code I have below, which at the moment returns 'Array' instead of the values of the checkboxes. Any suggestions?
HTML:
<h3>Service required:</h3>
<input type="text" id="name" name="name" placeholder="Name" required />
<input type="email" id="email" name="email" placeholder="Email" required />
<input class="check-box styled" type="checkbox" name="service[]" value="Service / repairs" /><label> Service / repairs</label>
<input class="check-box styled" type="checkbox" name="service[]" value="MOT" /><label> MOT</label>
<input class="check-box styled" type="checkbox" name="service[]" value="Cars for sale" /><label> Cars for sale</label>
Here's the php:
<?php
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
}
$formcontent= "From: $name \n Service(s) required: $service \n";
$recipient = "name#email.com";
$subject = "You have a new message from $name";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as we can.";
?>
Thanks,
Jason
You should join (for example implode with ', ') your array elements to a string.
<?php
$formcontent= "From: $name \n Service(s) required: ".implode(", " ,$service)." \n";
?>
Why don't you loop through the array to get the desired results into a String?
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
$service_string = "";
for($i=0;$i<count($service);$i++)
{
if($i!=0)
{
$service_string = $service_string . ", ";
}
$service_string = $service_string . $service[$i];
}
}
You will then get an output of a comma separated list of each ticked item as $service_string.
Since several checkboxes are stored in $_POST['service'], it is an array itself and has become two-dimensional. Its different indexes are accessible like this: $_POST['service'][0].
To do something with $_POST['service'], you can use foreach to loop through all indexes:
foreach($_POST['service'] as $post){
//Do stuff here
}
Alternatively, use implode() to simply concatenate all indexes.
Your input type checkbix must have unique names. Otherwise last checkbox will be found in $_POST. Or you can loop through as discuss above. Make your email html format and write a string of html to $formcontent. e.g.
$formcontent = "<html><head></head><body>";
$formcontent .= "<ul><li>".$_POST["checkbox1"]."</li>";
$formcontent .= "<li>".$_POST["checkbox2"]."</li>";
$formcontent .= "</ul></body></html>";
To write email in html format see mail function on php website.