我的问题是:我正在尝试从复选框中获取值并将其发送到电子邮件中。现在,我得到的只是无。在stackoverflow上尝试了很多不同的答案但到目前为止都没有。
<?php
if($_POST)
{
$to_Email = "myemail@test.test"; //Replace with recipient email address
$subject = 'Poruka'; //Subject line for emails
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
$user_Docs = 'None';
if(isset($_POST['dokument']) && is_array($_POST['dokument']) && count($_POST['dokument']) > 0){
$user_Docs = implode('|', $_POST['dokument']);
}
//check $_POST vars are set, exit if any missing
//if(!isset($_POST["name"]) || !isset($_POST["email"]) || !isset($_POST["telefon"]) || !isset($_POST["objekt"]))
//{
//$output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
//die($output);
//}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_Phone = filter_var($_POST["telefon"], FILTER_SANITIZE_STRING);
$user_Obj = filter_var($_POST["obj"], FILTER_SANITIZE_STRING);
$user_Kva = filter_var($_POST["kva"], FILTER_SANITIZE_STRING);
$user_Izg = filter_var($_POST["god_izg"], FILTER_SANITIZE_STRING);
$user_Ces = filter_var($_POST["kat_ces"], FILTER_SANITIZE_STRING);
$user_Opc = filter_var($_POST["kat_opc"], FILTER_SANITIZE_STRING);
$user_Nam = filter_var($_POST["nam"], FILTER_SANITIZE_STRING);
$user_Nap = filter_var($_POST["nap"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
$output = json_encode(array('type'=>'error', 'text' => 'Only numbers allowed in phone field'));
die($output);
}
if(strlen($user_Obj)<3) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short obj! Please enter something.'));
die($output);
}
if(!is_numeric($user_Kva)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short kva! Please enter something.'));
die($output);
}
if(!is_numeric($user_Izg)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short izg! Please enter something.'));
die($output);
}
if(strlen($user_Ces)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short ces! Please enter something.'));
die($output);
}
if(strlen($user_Opc)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short opc! Please enter something.'));
die($output);
}
if(strlen($user_Nam)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short nam! Please enter something.'));
die($output);
}
/* if(strlen($user_Nap)<0) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short nap! Please enter something.'));
die($output);
} */
// Construct email body
$body_message = 'Od: ' . $user_Name . "\r\n";
$body_message .= 'E-mail: ' . $user_Email . "\r\n";
$body_message .= 'Telefon: ' . $user_Phone . "\r\n";
$body_message .= 'Nov: ' . $user_Ob . "\r\n";
$body_message .= 'Kva: ' . $user_Kva . "\r\n";
$body_message .= 'God: ' . $user_Izg . "\r\n";
$body_message .= 'Kat. čes: ' . $user_Ces . "\r\n";
$body_message .= 'Kat. opć: ' . $user_Opc . "\r\n";
$body_message .= 'Nam: ' . $user_Nam . "\r\n";
$body_message .= 'Nap: ' . $user_Nap . "\r\n";
$body_message .= 'Dodatno: ' . $user_Docs . "\r\n";
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sentMail = @mail($to_Email, $subject, $body_message .' -'.$user_Name, $headers);
if(!$sentMail)
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .' Thank you for your email'));
die($output);
}
}
?>
HTML复选框输入包含许多用户在stackoverflow上使用的括号
<label>
<input type="checkbox" value="Tlocrt objekta" name="dokument[]" > Tlocrt objekta </input>
</label>
<label>
<input type="checkbox" value="Građevinska dozvola" name="dokument[]" > Građevinska dozvola </input>
</label>
<label>
<input type="checkbox" value="Glavni projekt" name="dokument[]" > Glavni projekt </input>
</label>
<label>
<input type="checkbox" value="Izvedbeni projekt" name="dokument[]" > Izvedbeni projekt </input>
</label>
<label>
<input type="checkbox" value="Elaborat legalizacije" name="dokument[]" > Elaborat legalizacije </input>
</label>
<label>
<input type="checkbox" value="Elaborat etažiranja" name="dokument[]" > Elaborat etažiranja </input>
</label>
<label>
<input type="checkbox" value="Projekt preuređenja" name="dokument[]" > Projekt preuređenja </input>
</label>
<label>
<input type="checkbox" value="Završno izvješće nadzornog inženjera" name="dokument[]" > Završno izvješće nadzornog inženjera </input>
<script type="text/javascript">
$(document).ready(function() {
$("#submit_btn").click(function() {
//get input field values
var user_Name = $('input[name=name]').val();
var user_Email = $('input[name=email]').val();
var user_Phone = $('input[name=telefon]').val();
var user_Objekt = $('input[name=objekt]').val();
var user_Kvadratura = $('input[name=kvadratura]').val();
var user_Izgradnja = $('input[name=god_izgradnje]').val();
var user_Cestica = $('input[name=kat_cestica]').val();
var user_Opcina = $('input[name=kat_opcina]').val();
var user_Namjena = $('input[name=namjena]').val();
var user_Napomena = $('textarea[name=napomena]').val();
var user_Docs = $("input:checkbox[name=dokument]:checked");
//simple validation at client's end
//we simply change border color to red if empty field using .css()
var proceed = true;
if(user_Name==""){
$('input[name=name]').css('border-color','red');
proceed = false;
}
if(user_Email==""){
$('input[name=email]').css('border-color','red');
proceed = false;
}
if(user_Phone=="") {
$('input[name=telefon]').css('border-color','red');
proceed = false;
}
if(user_Objekt=="") {
$('input[name=objekt]').css('border-color','red');
proceed = false;
}
if(user_Kvadratura=="") {
$('input[name=kvadratura]').css('border-color','red');
proceed = false;
}
if(user_Izgradnja=="") {
$('input[name=god_izgradnje]').css('border-color','red');
proceed = false;
}
if(user_Cestica=="") {
$('input[name=kat_cestica]').css('border-color','red');
proceed = false;
}
if(user_Opcina=="") {
$('input[name=kat_opcina]').css('border-color','red');
proceed = false;
}
if(user_Namjena=="") {
$('input[name=namjena]').css('border-color','red');
proceed = false;
}
/* if(user_Napomena=="") {
$('texarea[name=napomena]').css('border-color','red');
proceed = false;
} */
//everything looks good! proceed...
if(proceed)
{
//data to be sent to server
post_data = {'name':user_Name, 'email':user_Email, 'telefon':user_Phone, 'objekt':user_Objekt, 'kvadratura':user_Kvadratura, 'god_izgradnje':user_Izgradnja, 'kat_cestica':user_Cestica, 'kat_opcina':user_Opcina, 'namjena':user_Namjena, 'napomena':user_Napomena};
//Ajax post data to server
$.post('contact_me.php', post_data, function(response){
//load json data from server and output message
if(response.type == 'error')
{
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$('#contact_form input').val('');
$('#contact_form textarea').val('');
}
$("#result").hide().html(output).slideDown();
}, 'json');
}
});
//reset previously set border colors and hide all message on .keyup()
$("#contact_form input, #contact_form textarea").keyup(function() {
$("#contact_form input, #contact_form textarea").css('border-color','');
$("#result").slideUp();
});
});
</script>
我最终需要的是获取一封包含复选框值的电子邮件。到目前为止没有得到它们。形式中的其他一切都有效。感谢所有回复
答案 0 :(得分:0)
尝试序列化()表单数据,而不是以这种方式连接它。
http://api.jquery.com/serialize/
所以你的帖子数据应如下所示:
post_data = $('form#yourForm').serialize();
答案 1 :(得分:0)
虽然您创建了变量,但您似乎并未在javascript中发送user_Docs
:
//data to be sent to server
post_data = {
'name':user_Name,
'email':user_Email,
'telefon':user_Phone,
'objekt':user_Objekt,
'kvadratura':user_Kvadratura,
'god_izgradnje':user_Izgradnja,
'kat_cestica':user_Cestica,
'kat_opcina':user_Opcina,
'namjena':user_Namjena,
'napomena':user_Napomena
};
// where is user_Docs??
如其他答案所示,您应该尝试将JQuery serialize
与表单内容一起使用。删除自己的json代码,然后使用:
post_data = $('form #form_id').serialize();
然后,在PHP脚本中,您应该使用表单中使用的名称获取$_REQUEST
数组中的所有内容。
答案 2 :(得分:0)
答案是序列化[link] http://api.jquery.com/serialize/
这意味着替换这部分代码
//data to be sent to server
post_data = {'name':user_Name, 'email':user_Email, 'telefon':user_Phone, 'objekt':user_Objekt, 'kvadratura':user_Kvadratura, 'god_izgradnje':user_Izgradnja, 'kat_cestica':user_Cestica, 'kat_opcina':user_Opcina, 'namjena':user_Namjena, 'napomena':user_Napomena, 'dokument[]':user_Docs};
这一个
post_data = $('form#yourForm').serialize();
好吧不完全一样。我更改了ID并添加了我自己的表单ID,实际是字段集ID