2017-07-12 52 views
0

大家好我對我創建的代碼存在問題。它是一個下拉選擇依賴表格,所以當你選擇一個下拉菜單時,文本框/輸入會改變。表單不能正確提交值

現在我得到了它的大部分工作,我會解釋下。我有三個文件form.php,JavaScript文件和formprocess.php文件。我在WordPress上有這種形式,就像我說過的那樣,我大部分都在使用它。下拉選擇是新建和使用,如果你選擇使用它會去使用的形式,如果你選擇新的它帶來了另一個選擇選擇租賃或購買,並根據你選擇一個不同的表格會顯示。現在每個保存USED,LEASED和PURCHASED的div都具有相同的值,除了兩個或三個不同的值之外。

所以,當我使用USED選擇提交表單時,我收到了所有填充字段的電子郵件,但是當我選擇租用或購買了一些時間時,我沒有填寫任何字段,有時候我會收到其中幾個字段在何時發送電子郵件。這裏是下面的文件我祈禱有人可以看到我不是。

form.php的(HTML表單)

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Offer Submission Form</title> 
    <link rel="stylesheet" type="text/css" href="css/mystyle.css"> 
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet"> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
    <script type="text/javascript" src="js/newForm.js"></script> 
    </head> 

<body> 
<div class="form-style-10"> 

    <div class="inner-wrap"> 

    <form action="" method="post"> 

    <label>Dealership Name:<em class="required-star">*</em></label><input id="dealerName" name="dealerName" type="text" placeholder="Dealership Name" style="background-color: white;"/> 

    <label>Which Marketing Medium?<em class="required-star">*</em></label><select id="market" type="select" name="marketing"> 
     <option id="market1" value="Facebook">Facebook</option> 
     <option id="market2" value="Website Banner">Website Banner</option> 
     <option id="market3" value="Radio">Radio</option> 
     <option id="market4" value="TV">TV</option> 
     <option id="market5" value="Email">Email</option> 
     <option id="market6" value="Direct Mail">Direct Mail</option> 
     <option id="market7" value="All Channels">All Channels</option> 
    </select> 

    <label>Offer Type?</label><select id="newused" type="select" name="offerType"> 
     <option id="option_1" value="New">New</option> 
     <option id="option_2" value="Used">Used</option> 
    </select> 

<div id="new" style="display:none;"> 
    <label>Type of Purchase?</label><select id="newPick" type="select" name="typeOfPurchase"> 
     <option id="newPick_1" value="Purchased">Purchased</option> 
     <option id="newPick_2" value="Leased">Leased</option> 
    </select> 
</div> 

    <!--This div section is for when client select puchased as their new opiton....---> 
    <div id="purchased" style="display:none;"> 
     <label>Start Date:<em class="required-star">*</em></label><input id="pur_startDate" name="startDate" type="text" placeholder="01/01/2012" style="background-color: white;"/> 
     <label>End Date:<em class="required-star">*</em></label><input id="pur_endDate" name="endDate" type="text" placeholder="05/07/2015" style="background-color: white;"/> 
     <label>Year:<em class="required-star">*</em></label><input id="pur_vehicleYear" name="yearOfVehicle" type="text" placeholder="Vehicle Year" style="background-color: white;"/> 
     <label>Make:<em class="required-star">*</em></label><input id="pur_make" name="makeOfVehicle" type="text" placeholder="Make" style="background-color: white;"/> 
     <label>Model:<em class="required-star">*</em></label><input id="pur_model" name="modelOfVehicle" type="text" placeholder="Model" style="background-color: white;"/> 
     <label>Trim:<em class="required-star">*</em></label><input id="pur_trim" name="trimOfVehicle" type="text" placeholder="Trim of Vehicle" style="background-color: white;"/> 
     <label>Model #:<em class="required-star">*</em></label><input id="pur_input_5" name="modelNumber" type="text" placeholder="14325" style="background-color: white;"/> 
     <label>Stock #:<em class="required-star">*</em></label><input id="pur_input_5" name="stockNumber" type="text" placeholder="1234" style="background-color: white;"/> 
     <label>MSRP:<em class="required-star">*</em></label><input id="pur_input_5" name="msrpNumber" type="text" placeholder="15995" style="background-color: white;"/> 
     <label>Selling Price:<em class="required-star">*</em></label><input id="pur_input_5" name="sellingPrice" type="text" placeholder="12895" style="background-color: white;"/> 
     <label>Down Payment:<em class="required-star">*</em></label><input id="pur_input_5" name="downPayment" type="text" placeholder="1000" style="background-color: white;"/> 
     <label>$XXX/ Monthly Payment:<em class="required-star">*</em></label><input id="pur_input_5" name="monthlyPayment" type="text" placeholder="$198" style="background-color: white;"/> 
     <label>Last 6 of VIN:<em class="required-star">*</em></label><input id="pur_input_5" name="lastVin" type="text" placeholder="123456" style="background-color: white;"/> 
     <label>Rebate:</label><input id="pur_input_5" name="rebateInfo" type="text" placeholder="Rebate on Vehicle" style="background-color: white;"/> 
     <label>APR:<em class="required-star">*</em></label><input id="pur_input_5" name="aprAmount" type="text" placeholder="ARP on Vehicle" style="background-color: white;"/> 
     <label>Term:<em class="required-star">*</em></label><input id="pur_input_5" name="termOfVehicle" type="text" placeholder="Residual Payment on Vehicle" style="background-color: white;"/> 
     <label>Other Notes(Rebate Info, Special Details etc):</label> <textarea id="pur_textArea" name="msg" placeholder="Notes" style="background-color: white;" rows=2></textarea> 
    </div> 

    <!--This div section is for when client select leased as their new opiton....---> 
    <div id="leased" style="display:none;"> 
     <label>Start Date:<em class="required-star">*</em></label><input id="lsd_startDate" name="startDate" type="text" placeholder="01/01/2012" style="background-color: white;"/> 
     <label>End Date:<em class="required-star">*</em></label><input id="lsd_endDate" name="endDate" type="text" placeholder="05/07/2015" style="background-color: white;"/> 
     <label>Year:<em class="required-star">*</em></label><input id="lsd_vehicleYear" name="yearOfVehicle" type="text" placeholder="Vehicle Year" style="background-color: white;"/> 
     <label>Make:<em class="required-star">*</em></label><input id="lsd_make" name="makeOfVehicle" type="text" placeholder="Make" style="background-color: white;"/> 
     <label>Model:<em class="required-star">*</em></label><input id="lsd_model" name="modelOfVehicle" type="text" placeholder="Model" style="background-color: white;"/> 
     <label>Trim:<em class="required-star">*</em></label><input id="lsd_trim" name="trimOfVehicle" type="text" placeholder="Trim of Vehicle" style="background-color: white;"/> 
     <label>Model #:<em class="required-star">*</em></label><input id="lsd_model#" name="modelNumber" type="text" placeholder="14325" style="background-color: white;"/> 
     <label>Stock #:<em class="required-star">*</em></label><input id="lsd_stock#" name="stockNumber" type="text" placeholder="1234" style="background-color: white;"/> 
     <label>MSRP:<em class="required-star">*</em></label><input id="lsd_msrp" name="msrpNumber" type="text" placeholder="$15,995" style="background-color: white;"/> 
     <label>Selling Price:<em class="required-star">*</em></label><input id="lsd_selling_price" name="sellingPrice" type="text" placeholder="12895" style="background-color: white;"/> 
     <label>Down Payment:<em class="required-star">*</em></label><input id="lsd_down_payment" name="downPayment" type="text" placeholder="1000" style="background-color: white;"/> 
     <label>$XXX/ Monthly Payment:<em class="required-star">*</em></label><input id="lsd_monthly_payment" name="monthlyPayment" type="text" placeholder="198" style="background-color: white;"/> 
     <label>Last 6 of VIN:<em class="required-star">*</em></label><input id="lsd_vin" name="lastVin" type="text" placeholder="123456" style="background-color: white;"/> 
     <label>Rebate:</label><input id="lsd_rebate" name="rebateInfo" type="text" placeholder="Rebate on Vehicle" style="background-color: white;"/> 
     <label>Term:<em class="required-star">*</em></label><input id="lsd_term" name="termOfVehicle" type="text" placeholder="ARP on Vehicle" style="background-color: white;"/> 
     <label>Residual Value:<em class="required-star">*</em></label><input id="lsd_residual_value" name="residualValue" type="text" placeholder="Residual Payment on Vehicle" style="background-color: white;"/> 
     <label>Miles Per Year:<em class="required-star">*</em></label><input id="lsd_miles_year" name="milesPerYear" type="text" placeholder="Residual Payment on Vehicle" style="background-color: white;"/> 
     <label>Other Notes(Rebate Info, Special Details etc):</label> <textarea id="lsd_textArea" name="msg" placeholder="Notes" style="background-color: white;" rows=2></textarea> 
    </div> 

    <!-- IF users picks USED it will go to this section of the form...--> 
    <div id="used" style="display:none;"> 
     <label>Start Date:<em class="required-star">*</em></label><input id="fin_startDate" name="startDate" type="text" placeholder="01/01/2012" style="background-color: white;"/> 
     <label>End Date:<em class="required-star">*</em></label><input id="fin_endDate" name="endDate" type="text" placeholder="05/07/2015" style="background-color: white;"/> 
     <label>Year:<em class="required-star">*</em></label><input id="fin_vehicleYear" name="yearOfVehicle" type="text" placeholder="Vehicle Year" style="background-color: white;"/> 
     <label>Make:<em class="required-star">*</em></label><input id="fin_make" name="makeOfVehicle" type="text" placeholder="Make" style="background-color: white;"/> 
     <label>Model:<em class="required-star">*</em></label><input id="fin_model" name="modelOfVehicle" type="text" placeholder="Model" style="background-color: white;"/> 
     <label>Trim:</label><input id="fin_trim" name="trimOfVehicle" type="text" placeholder="Trim of Vehicle" style="background-color: white;"/> 
     <label>Stock #:<em class="required-star">*</em></label><input id="fin_input_5" name="stockNumber" type="text" placeholder="1234" style="background-color: white;"/> 
     <label>Selling Price:<em class="required-star">*</em></label><input id="fin_input_5" name="sellingPrice" type="text" placeholder="12895" style="background-color: white;"/> 
     <label>Down Payment:<em class="required-star">*</em></label><input id="fin_input_5" name="downPayment" type="text" placeholder="1000" style="background-color: white;"/> 
     <label>APR:<em class="required-star">*</em></label><input id="fin_input_5" name="aprAmount" type="text" placeholder="ARP on Vehicle" style="background-color: white;"/> 
     <label>Term:<em class="required-star">*</em></label><input id="used_term" name="termOfVehicle" type="text" placeholder="" style="background-color: white;"/> 
     <label>Other Notes(Rebate Info, Special Details etc):</label><textarea id="fin_textArea" name="msg" placeholder="Notes" style="background-color: white;" rows=2></textarea> 
    </div> 

     <div class="button-section"><br /> 
      <input type="submit" name="submit" value="Submit Offer" /> 
     </div> 


</form> 
</body> 
    </div> 
</div> 
</html> 

form.js(JavaScript文件)

$(document).ready(function() { 
$select = $('#newused'); 
$('#newused').on('change',function(){ 

    if($(this).val() == "New"){ 
     if($('#new').is(":hidden")){ 
      $('#new').show(); 
      $('#purchased').show(); 
      $('#leased').show(); 
     } 
     else{ 
      $('#used').hide(); 
      } 
    } 

    if($(this).val() == "Used"){ 
     if($('#used').is(":hidden")){ 
      $('#used').show(); 
     } 
     else{ 
      $('#new').hide(); 
      $('#leased').hide(); 
      $('#purchased').hide(); 
     } 
    } 

}); 
    }); 

$(document).ready(function() { 
$select = $('#newPick'); 
$('#newPick').on('change',function(){ 

    if($(this).val() == "Purchased"){ 
     if($('#purchased').is(":hidden")){ 
      $('#purchased').show(); 
     } 
     else{   
      $('#leased').hide(); 
      $('#used').hide(); 
     } 
    } 

    if($(this).val() == "Leased"){ 
     if($('#leased').is(":hidden")){ 
      $('#leased').show(); 
     } 
     else{   
      $('#purchased').hide(); 
      $('#used').hide(); 
     } 
    } 

    }); 
    }); 

formprocess.php(PHP文件)

<?php 

//THIS SECTION IS FOR WHEN THE PURCHASED SECTION OF THE FORM IS FILLED OUT..........////   
     if(isset($_POST["purchasedSubmit"])) 
     { 

      // Checking For Blank Fields.... 
      if($_POST['dealerName']=="") 
       { 
?> 
        <script type="text/javascript"> 
         alert("Please complete the text marked with an *."); //java script telling user to fill in all required fields.. 
        </script> 
<?php 
       } 

//THIS IS THE INFO THAT WILL BE SENT TO INDIVIDUALS IN THE EMAIL SECTION BELOW AS LONG AS ALL THE REQUIRED FIELDS ARE FILLED IN.....//////    
     else{ 

      $offer = $_POST['offerType']; 
      $typeOfPurchase = $_POST['typeOfPurchase']; 
      $marketing = $_POST['marketing']; 
      $dealerName = $_POST['dealerName']; 
      $startDate = $_POST['startDate']; 
      $endDate = $_POST['endDate']; 
      $vehicleYear = $_POST['yearOfVehicle']; 
      $vehicleMake = $_POST['makeOfVehicle']; 
      $vehicleModel = $_POST['modelOfVehicle']; 
      $vehicleTrim = $_POST['trimOfVehicle']; 
      $modelNumber = $_POST['modelNumber']; 
      $stockNumber = $_POST['stockNumber']; 
      $msrp = $_POST['msrpNumber']; 
      $sellingPrice = $_POST['sellingPrice']; 
      $downPayment = $_POST['downPayment']; 
      $monthlyPayment = $_POST['monthlyPayment']; 
      $lastVin = $_POST['lastVin']; 
      $aprAmount = $_POST['aprAmount']; 
      $vehicleTerm = $_POST['termOfVehicle']; 
      $rebate = $_POST['rebateInfo']; 
      $message = $_POST['msg']; 
      $message = wordwrap($message, 70); 

      $msrpAmount = number_format($msrp, 2, '.', ',') 
      $sellPrice = number_format($sellingPrice, 2, '.', ',') 
      $dwnPayment = number_format($downPayment, 2, '.', ',') 
      $monthPayment = number_format($monthlyPayment, 2, '.', ',') 
      $apr = number_format($aprAmount, 2, '.', ',') 
      $rebateAmount = number_format($rebate, 2, '.', ',') 

      $form_content = 
      "What Type of Offer: $offer 
      If your purhcase was new what was your type of purchase: $typeOfPurchase 
      Which Marketing Medium: $marketing 
      Dealership Name: $dealerName 
      Start Date: $startDate 
      End Date: $endDate 
      Year of Vehicle: $vehicleYear 
      Make of Vehicle: $vehicleMake 
      Model of Vehicle: $vehicleModel 
      Trim of Vehicle: $vehicleTrim 
      Model #: $modelNumber 
      Stock #: $stockNumber 
      MSRP: $msrpAmount 
      Vehicle Selling Price: $sellPrice 
      Down Payment on Vehicle: $dwnPayment 
      '$'XXX/ Monthly Payment: $monthPayment 
      Last 6 of VIN: $lastVin 
      Vehicle Rebate: $rebateAmount 
      Vehicle APR: $apr 
      Other Notes (Rebate Info, Special Details etc): $message"; 

      $email_subject = "Online Offer Submission Form"; 

      //THIS IS THE ARRAY THAT WILL HOLD EVERYONE THAT WILL GET THE EMAIL....////// 
      $arrEmail = array('Jonathan <[email protected]>'); 



       foreach($arrEmail as $key => $email_to) 
       mail($email_to, $email_subject, $form_content); 

     header('http://www.directionwebsite.com'); 
     } 
     } 
     ?> 

當我提交形式與下拉選擇使用在這裏挑選是我回來的結果記住我把任何東西都粘貼到字段快速c現在沒有驗證。下面的空字段沒有任何內容:分號是沒有發送任何值的空字段,但是使用選擇的那些值可能沒有任何存儲。它是我購買和租賃的新選擇,我遇到了麻煩。

What Type of Offer: Used 
         If your purhcase was new what was your type of purchase: -1 
         Which Marketing Medium: All_Channels 
         Dealership Name: duh duh duh 
         Start Date: 05/07/1983 
         End Date: 03-11-2017 
         Year of Vehicle: 1983 
         Make of Vehicle: Ford 
         Model of Vehicle: Ford 
         Trim of Vehicle: Ford 
         Model #: 
         Stock #: 12345 
         MSRP:$ 
         Vehicle Selling Price:$40,000.00 
         Down Payment on Vehicle:$ 4,000.00 
         '$'XXX/ Monthly Payment:$ 
         Last 6 of VIN: 
         Vehicle Rebate:$ 
         Residual Value:$ 
         Vehicle APR:$175.00 
         Miles Per Year: 
         Other Notes (Rebate Info, Special Details etc): fffffffffffffffffffffffffffrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 

現在,當我拿起新的從選擇另一個選擇下拉有兩個選擇要麼租用或購買,這裏是結果我從這些二送出現。再次,下面的字段在分號後沒有任何內容:空字段。

What Type of Offer: New 
         If your purhcase was new what was your type of purchase: Purchased 
         Which Marketing Medium: Facebook 
         Dealership Name: new purchased newest try 
         Start Date: 
         End Date: 
         Year of Vehicle: 
         Make of Vehicle: 
         Model of Vehicle: 
         Trim of Vehicle: 
         Model #: 
         Stock #: 
         MSRP:$ 
         Vehicle Selling Price:$ 
         Down Payment on Vehicle:$ 
         '$'XXX/ Monthly Payment:$ 
         Last 6 of VIN: 
         Vehicle Rebate:$ 
         Residual Value:$ 


         Vehicle APR:$ 
         Miles Per Year: 
         Other Notes (Rebate Info, Special Details etc): 

JFIDDLE

+0

未提交的字段是否使用'.hide()'隱藏?請參閱https://stackoverflow.com/questions/8318428/submit-form-fields-inside-displaynone-element – Barmar

+0

是的,請參閱說使用的選擇我有startDate和endDate變量與make,model等相同,他們顯示罰款用於選擇。所以,如果是這樣的話@Barmar不會在二手車上出現,而不僅僅是租賃和購買選擇。原因我問,因爲我也有used.hide(),我的意思是你可以看到我的代碼上面。 –

+0

很難理解這個問題,因爲它非常含糊。請具體說明您正在做什麼,以及哪些字段正在發送,哪些字段未被髮送。 – Barmar

回答

1

的問題

值正在由於您有相同的形式相同name屬性多個輸入覆蓋。

可能的解決方案

  • 每個實例給每個輸入的唯一名稱值
  • 放入自己的形式顯示和隱藏整個表單
  • 通過Ajax序列化只值提交是可見的
  • 使用方括號表示法將值推送到一個數組,這裏是一個example
+0

問題如果我選擇解決方案1併爲輸入提供唯一的名稱值,我仍然可以將這些值存儲到php頁面中的相同變量中。所以,說我有一年的車。我會爲輸入類型做一些簡單的事情,比如year1,year2和year3。在php文件中,我可以將$ _POST ['year1']和year2以及year3放入一個名爲year的變量中。或者也會錯過它。第二個問題是將每個實例放入它自己的形式。我可以對每個表單使用相同的提交按鈕嗎?唯一的問題是如何在表單提交時如何獲取報價類型值 –

+0

我基本上使用了三種不同的表單,因此我不必更改我的PHP文件。我之前做過這件事,但由於某種原因,它不起作用,我沒有心情來弄清楚這個問題,所以從一開始我就是對的。謝謝@KennethStoddard提示和幫助。我一直在用wordpress工作,所以我想知道一旦我保存了html IDK後會發生什麼,但爲了以後的幫助,再次使用上面的多表單解決方案,它對我來說工作得很好。 –

+0

你也許可以在後端捕獲這些值,並根據你的建議發送它們,但我認爲使用唯一名稱是最有意義的;這將促進數據的完整性。 –