2015-07-21 52 views
0

我在PHP中有以下形式的操作。我已使用檢查清單來獲取項目名稱。如果我選擇列表中的項目,它將以Array,Array而不是原始值(即Tatvam,Amairo,Vedam)出現。HTML與PHP的檢查清單

我收到的消息是這樣

This is the message I am getting if I select one project

jQuery(".form-js-contact").submit(function() { 
    var thisform = jQuery(this); 
    jQuery('.required-error',thisform).remove(); 
    var aname = jQuery("#aname").val(); 
    var amail = jQuery("#amail").val(); 
    var aphone = jQuery("#aphone").val(); 
    var acomments = jQuery("#acomments").val(); 
    var color = jQuery("input[name='color[]']").serializeArray(); 

    var psubject = jQuery("#psubject").val(); 

    var data = {'aname':aname,'amail':amail,'aphone':aphone,'acomments':acomments,'psubject':psubject,'color':color}; 
    if (aname == "") { 
     jQuery("#aname").after('<span class="form-description required-error">Required field.</span>'); 
    }else { 
     jQuery("#aname").parent().find('.required-error').remove(); 
    } 
    if (amail == "") { 
     jQuery("#amail").after('<span class="form-description required-error">Required field.</span>'); 
    }else { 
     jQuery("#amail").parent().find('.required-error').remove(); 
    } 
    if (aphone == "") { 
     jQuery("#aphone").after('<span class="form-description required-error">Required field.</span>'); 
    }else { 
     jQuery("#aphone").parent().find('.required-error').remove(); 
    } 
    if (acomments == "") { 
     jQuery("#acomments").after('<span class="form-description required-error">Required field.</span>'); 
    }else { 
     jQuery("#acomments").parent().find('.required-error').remove(); 
    } 



    if (aname != "" && amail != "" && aphone != "" && acomments != "") { 
     jQuery.post("contact_us_contact.php",data,function (result) { 
      if (result == "done") { 
       thisform.prepend("<div class='alert-message success-contact'><p><strong>Thank you "+name+"!</strong> We'll be in touch real soon .</p></div>"); 
       jQuery("#aname").val(""); 
       jQuery("#amail").val(""); 
       jQuery("#aphone").val(""); 
       jQuery("#acomments").val(""); 

      } 
     }); 
    } 
    return false; 
}); 

這是我的HTML代碼

<form class="form-style form-js-contact" action="contact_us_contact.php" method="post"> 
         <input type="hidden" name="psubject" id="psubject" value="Enquiry from Contact Page"> 
<div class="col-md-6 col-lg-6" > 
<input class=required-item value="" name=aname id=aname aria-required=true placeholder="Your Name*"></div> 
<div class="col-md-6 col-lg-6" ><input type=email class=required-item id=amail name=amail value="" aria-required=true placeholder="Your Email*"></div> 
<div class="col-md-6 col-lg-12" ><input class=required-item aria-required=true id=aphone name=aphone value="" placeholder="Your Phone*"></div> 

<div class="col-md-3" style="margin-bottom:10px; "><h4>Projects Interested</h4></div> 
<div class="col-md-2" ><input name="color[]" id="color" class="check" type="checkbox" value="Amairo">Amairo</div> 
<div class="col-md-2" ><input name="color[]" id="color" class="check" type="checkbox" value="Tatvam">Tatvam</div> 
<div class="col-md-2" ><input name="color[]" id="color" class="check" type="checkbox" value="Vedam">Vedam</div> 

<div class="col-md-6 col-lg-12" > 
<textarea name="acomments" id="acomments" class=required-item aria-required=true placeholder="Type Your Message*"></textarea></div> 
<div class="col-md-6 col-lg-6" > 
<input style=font-size:16px name=submit type=submit value="Send Enquiry" class="submit_buttom buttonColor" id="Main_Contact_Form"></div> 
</form> 

這是我的PHP

 <?php 
    function clean_text($text='') { 
    $text = trim($text); 
    $text = strip_tags($text); 
    $text = addslashes($text); 
    $text = htmlspecialchars($text); 
    return $text; 
    } 
    if (!$_POST) { 
    die(); 
    }else { 
    if (empty($_POST["aname"]) && empty($_POST["aphone"]) &&  empty($_POST["amail"])&& empty($_POST["acomments"])) { 
    echo "all_empty"; 
}else if (empty($_POST["aname"])) { 
    echo "empty_name"; 
}else if (empty($_POST["amail"])) { 
    echo "empty_mail"; 
}else if (empty($_POST["aphone"])) { 
    echo "empty_phone"; 
}else if (empty($_POST["acomments"])) { 
    echo "empty_Comments"; 
}else {  
    $your_email = "[email protected]";  
    $aname = clean_text($_POST["aname"]); 
    $amail = clean_text($_POST["amail"]); 
    $aphone = clean_text($_POST["aphone"]); 
    $acomments = clean_text($_POST["acomments"]); 
    $wname = $_POST['color']; 
    $psubject = clean_text($_POST["psubject"]); 
    $subject = "$psubject"; 

    $headers = "From: [email protected]". "\r\n"; 
    $headers .= 'Content-type: text/html; charset=UTF-8'. "\r\n"; 
    $msg  = "New Message \n\r <br>"; 
    $msg  .= "Name : \t $aname \r\n <br>"; 
    $msg  .= "Email : \t $amail \r\n<br>";  
    $msg .= "Phone : \t $aphone \r\n<br>"; 
    $msg .= "Message : \t $acomments \r\n<br>"; 
    if (isset($wname)) { 
    $msg .= "Project(s) Interested: \r\n"; 
    $msg .= "<ul> \r\n"; 
    foreach ($wname as $color){ 
    $msg .="<li>" .$color. "</li> \r\n"; 
} 

    $msg .= "</ul>"; 

    } // isset 

    echo "done"; 
    $done = @mail($your_email, $subject, $msg, $headers); 
    } 
} 

?>

+0

您可能需要提供你想要什麼來實現或者你有什麼具體問題的詳細信息。否則,將很難幫助你解決這個問題。 – jrenk

+0

嗨jrenk,感謝您的快速回復。我確實需要的是檢查清單工作不正常。我沒有從清單中得到值,即使它也被檢查。你能不能更新我一樣的。 – Suthakar

回答

3

您正在使用的ID jQuery("#color").val();和取色值的jQuery根據HTML規則,只有一個具有相同名稱的標識,因此您只能獲得一個值。你必須更換喜歡

> jQuery 

var color = jQuery("input[name='color[]']").serializeArray(); 

,然後你會得到PHP代碼顏色的所有值$wname = $_POST['color'];

+0

您能告訴我爲什麼值將以數組而不是我在值標記中給出的原始值發送。在此先感謝 – Suthakar

+0

因爲您將獲得數組中的顏色數據。你需要在ul li中解析數組 –

+0

Hi,Pathik我更新了jQuery和PHP中的新代碼。請檢查並讓我知道問題。謝謝 – Suthakar

0

在你的PHP代碼

$wname  = implode(",", $_POST['color']); 

替換此

$wname  = $_POST['color']; 

因爲你是從HTML傳遞name="color[]"作爲數組。

+0

嗨賽義德,我用同樣的事情,但我得到他的消息沒有項目 名稱:測試儀 電子郵件:[email protected] 電話:123456789 消息:測試 – Suthakar

0

您的代碼出現了錯誤

下似乎是錯誤的

if (isset($_POST['wname'])) {...} 

我相信它應該是

if (isset($wname)) {...} 
+0

拉茲您好,我使用了相同的事情,但我是否讓他在沒有該項目的情況下發送消息名稱:測試人員電子郵件地址:[email protected]電話:123456信息:測試 – Suthakar