Salve, ho dei campi di tipo checkbox dove seleziono l'articolo che mi interessa
<form id="contactForm" action="php/contact-form-newsletter.php" method="POST">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari da parete">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari da scrivania">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari tascabili">
<input type="checkbox" id="calendari" name="calendari[]" value="calendaricd">
e dei campi corrispondenti text per ognuno in cui si inserisce la quantità di ciascun articolo
<input type="text" value="" name="quantity[1]" id="productqty">
<input type="text" value="" name="quantity[2]" id="productqty">
<input type="text" value="" name="quantity[3]" id="productqty">
<input type="text" value="" name="quantity[4]" id="productqty">
ho un file js view.newsletter-new.js che controlla il form
a questo script devo passare i dati delle checkbox associate al prodotto scelto e le loro rispettive quantità
come potrei fare?
lo script finale contact-form-newsletter.php(action del form) invia il tutto via email
la pagina in questione è on line qui:http://www.gasparella.it/newsletter.php ed è impostato in maniera non corretta sia il form che i js che il php.
grazie di eventuali spunti
<form id="contactForm" action="php/contact-form-newsletter.php" method="POST">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari da parete">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari da scrivania">
<input type="checkbox" id="calendari" name="calendari[]" value="calendari tascabili">
<input type="checkbox" id="calendari" name="calendari[]" value="calendaricd">
e dei campi corrispondenti text per ognuno in cui si inserisce la quantità di ciascun articolo
<input type="text" value="" name="quantity[1]" id="productqty">
<input type="text" value="" name="quantity[2]" id="productqty">
<input type="text" value="" name="quantity[3]" id="productqty">
<input type="text" value="" name="quantity[4]" id="productqty">
ho un file js view.newsletter-new.js che controlla il form
Codice:
(function($) {
'use strict';
/*
Contact Form: Basic
*/
$('#contactForm:not([data-type=advanced])').validate({
submitHandler: function(form) {
var $form = $(form),
$messageSuccess = $('#contactSuccess'),
$messageError = $('#contactError'),
$submitButton = $(this.submitButton);
$submitButton.button('loading');
// Ajax Submit
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
name: $form.find('#referente').val(),
email: $form.find('#email').val(),
products: $form.find('#articolo').is(':checked').val(),
subject: $form.find('#ditta').val(),
business: $form.find('#business').val(),
message: $form.find('#message').val()
},
dataType: 'json',
complete: function(data) {
if (typeof data.responseJSON === 'object') {
if (data.responseJSON.response == 'success') {
$messageSuccess.removeClass('hidden');
$messageError.addClass('hidden');
// Reset Form
$form.find('.form-control')
.val('')
.blur()
.parent()
.removeClass('has-success')
.removeClass('has-error')
.find('label.error')
.remove();
$("#privacy").attr("checked", false);
if (($messageSuccess.offset().top - 80) < $(window).scrollTop()) {
$('html, body').animate({
scrollTop: $messageSuccess.offset().top - 80
}, 300);
}
$submitButton.button('reset');
return;
}
}
$messageError.removeClass('hidden');
$messageSuccess.addClass('hidden');
if (($messageError.offset().top - 80) < $(window).scrollTop()) {
$('html, body').animate({
scrollTop: $messageError.offset().top - 80
}, 300);
}
$form.find('.has-success')
.removeClass('has-success');
$submitButton.button('reset');
}
});
}
});
/*
Contact Form: Advanced
*/
$('#contactFormAdvanced, #contactForm[data-type=advanced]').validate({
onkeyup: false,
onclick: false,
onfocusout: false,
rules: {
'captcha': {
captcha: false
},
'checkboxes[]': {
required: false
},
'radios': {
required: false
}
}
});
}).apply(this, [jQuery]);
come potrei fare?
lo script finale contact-form-newsletter.php(action del form) invia il tutto via email
Codice:
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Step 1 - Enter your email address below.
$to = '[email protected]';
// Step 2 - Enable if the server requires SMTP authentication. (true/false)
$enablePHPMailer = false;
$subject = "Richiesta Offerta";
if(isset($_POST['email'])) {
$products = $_POST['products'];
foreach($products as $cac) {
$prod .= $cac. "<br>\n";
}
$name = $_POST['referente'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Nome',
'val' => $_POST['referente']
),
1 => array(
'text' => 'Email',
'val' => $_POST['email']
),
2 => array(
'text' => 'articoli',
'val' => $prod
),
3 => array(
'text' => 'Messaggio',
'val' => $_POST['message']
)
);
if(isset($_POST['email'])) {
$message = "";
}
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n".$prod;
}
if($_POST['ids']){
foreach($_POST['ids'] as $item){
$items = $item." <br />";
}
}
// Simple Mail
if(!$enablePHPMailer) {
$headers = '';
$headers .= 'From: ' . $to . ' <' . $to . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
// PHP Mailer Library - Docs: https://github.com/PHPMailer/PHPMailer
} else {
include("php-mailer/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 0; // Debug Mode
// Step 3 - If you don't receive the email, try to configure the parameters below:
//$mail->Host = 'mail.yourserver.com'; // Specify main and backup server
//$mail->SMTPAuth = true; // Enable SMTP authentication
//$mail->Username = 'username'; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = $email;
$mail->FromName = $_POST['referente'];
$mail->AddAddress($to); // Add a recipient
$mail->AddReplyTo($email, $name);
$mail->IsHTML(true); // Set email format to HTML
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->Send()) {
$arrResult = array ('response'=>'error');
}
$arrResult = array ('response'=>'success');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
grazie di eventuali spunti