2014-07-04 67 views
0

我有幾個複選框具有相同的名稱,並用方括號所以要得到陣列

<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> 

我想從複選框得到的值與此

var user_Docs  = $("input:checkbox[name='dokument[]']:checked"); 

,並把它們交給PHP與此

//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}; 

這是我的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, 'dokument[]':user_Docs}; 

      //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(); 
    }); 

}); 

這是我的PHP

<?php 
if($_POST) 
{ 
    $to_Email  = "[email protected]"; //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 = ''; 
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_Objekt  = filter_var($_POST["objekt"], FILTER_SANITIZE_STRING); 
    $user_Kvadratura = filter_var($_POST["kvadratura"], FILTER_SANITIZE_STRING); 
    $user_Izgradnja = filter_var($_POST["god_izgradnje"], FILTER_SANITIZE_STRING); 
    $user_Cestica  = filter_var($_POST["kat_cestica"], FILTER_SANITIZE_STRING); 
    $user_Opcina  = filter_var($_POST["kat_opcina"], FILTER_SANITIZE_STRING); 
    $user_Namjena  = filter_var($_POST["namjena"], FILTER_SANITIZE_STRING); 
    $user_Napomena  = filter_var($_POST["napomena"], 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_Objekt)<3) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short objekt! Please enter something.')); 
     die($output); 
    } 
    if(!is_numeric($user_Kvadratura)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short kvadratura! Please enter something.')); 
     die($output); 
    } 
    if(!is_numeric($user_Izgradnja)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short izgradnja! Please enter something.')); 
     die($output); 
    } 
    if(strlen($user_Cestica)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short cestica! Please enter something.')); 
     die($output); 
    } 
    if(strlen($user_Opcina)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short opcina! Please enter something.')); 
     die($output); 
    } 
    if(strlen($user_Namjena)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short namjena! Please enter something.')); 
     die($output); 
    } 
    /* if(strlen($user_Napomena)<0) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short napomena! 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 .= 'Novi ili postojeći objekt: ' . $user_Objekt . "\r\n"; 
    $body_message .= 'Kvadratni metri: ' . $user_Kvadratura . "\r\n"; 
    $body_message .= 'Godina izgradnje: ' . $user_Izgradnja . "\r\n"; 
    $body_message .= 'Kat. čestica: ' . $user_Cestica . "\r\n"; 
    $body_message .= 'Kat. općina: ' . $user_Opcina . "\r\n"; 
    $body_message .= 'Namjena: ' . $user_Namjena . "\r\n"; 
    $body_message .= 'Napomena: ' . $user_Napomena . "\r\n"; 
    $body_message .= 'Dodatni dokumenti: ' . $user_Docs . "\r\n"; 
    $body_message .= 'Dodatni dokumenti: ' . $dokument . "\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); 
    } 
} 
?> 

我的形式工作,但在我的電子郵件,我沒有從複選框得到的值。 更確切地說,我試圖從表單收集數據併發送一封包含相同數據的電子郵件。所有數據都在電子郵件中,但複選框的值不顯示。不知何故,我沒有得到他們。有人可以幫我一把嗎?

+1

很清楚你用post_data數組做什麼。什麼郵件?請在你的問題中更具體的200%。 – Johan

+0

對不起,我更新了更多的細節 – ultranit

+0

如果你做var_dump($ _ POST);在你的PHP腳本中,dokument數組是否顯示? – Johan

回答

0

我想你正在尋找一個AJAX表單提交。請閱讀以下示例:Submitting HTML form using Jquery AJAX

或者您的表單無法在任何地方提交。爲了將它傳遞到$ _ POST,你需要有標籤這樣說

這output.php就是$ _ POST將被填充。

+0

表單工作,提交除複選框值之外的所有內容。不知何故,這個數據不會出現。 'var user_Docs = $(「input:checkbox [name ='dokument []']:checked」);'或這裏'//將數據發送到服務器 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};' – ultranit

+0

@ultranit使用console.log()將數據記錄到控制檯並檢查它是否包含值。 – Johan

+0

我不相信它確實包含值。第一次使用Firebug控制檯 – ultranit

0

序列化表單而不是手動創建要傳遞的數組。

http://jsfiddle.net/44H7b/

$("#form").submit(function (event) { 
     console.log($(this).serializeArray()); 
     event.preventDefault(); 
    }); 
0

答案被序列化[鏈接] http://api.jquery.com/serialize/ 這意味着與這一個 post_data = $('form#yourForm').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};

這部分以及不完全不變。我改變了ID,並把我自己的表單ID