2017-02-21 166 views
1

我收到一個錯誤,它說它是空的。 但是當我的文本字段爲空時,它不顯示錯誤。我的意思是,它允許空字段,而不是在所選選項不允許選擇的值被選擇

這裏是我的html:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<form role="form" id="cakechoices" name="cakechoices" method="post"> 

<label for="dedicationT"> Dedication Text: </label> <input type="text" name="dedicationT" id="dedicationT" size="20" placeholder="Dedication" /> 

<label for="service_date"> Date: </label> <input type="date" id="service_date" name="service_date" value="M-D-YY"/> 

<br> <label> Branch: <select name="branch_name" id="branch_selection" /> 
<option disabled selected value>-- Select one --</option> 
<option value="B1">B1 </option> 
<option value="B2">B2 </option> 
<option value="B3">B3 </option> 
<option value="B4">B4 </option> 
</select> </label> 
</br> 

<input type="submit" id="submit" class="btn btn-default" value="Order Now" /> 

</form> 

這裏是我的PHP:

<?php 

    $link = mysql_connect('localhost', 'root', ''); 
    if (!$link) { die('Could not connect: ' . mysql_error()); } 
    if (!mysql_select_db('myrnas')) { die('Could not select database: ' . mysql_error()); } 

    session_start(); 
$dedicationT = $_POST['dedicationT']; 
$service_date = $_POST['service_date']; 
$branch_name = $_POST['branch_name']; 

$query1 = "INSERT INTO order_cake (dedicationT, service_date, branch_name) VALUES ('$dedicationT', '$service_date', '$branch_name'); 

if(@mysql_query($query1,$link)) 
    { echo "Done!"; } 
    else { print '<p> <h1> Somethings wrong, failed to connect!!!. GAD WHY!?! </h1> '.mysql_error().'</p>'; } 

?> 

這是我的錯誤: 未定義指數:Branch_name在D:\ Xampp \ htdocs \ system \ customize \ ordersent.php on line

這是我的Ajax在HTML文件中相同

function submit_order($form) { 
    $.ajax({ 
     url : "process/ordersent.php", 
     type : "POST", 
     cache : false, 
     data : $form.serialize(), 
     success: function(response) { 

     alert(response); 
     if(response == "Done") { 
      window.location.href = "home.php"; 
      } 
     } 
    }); 
    } 


     $('#submit').click(function(){ /* when the submit button in the modal is clicked, submit the form */ 
     swal({ 
      title: "Are you sure?", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#ff5c33", 
      confirmButtonText: "Order Now!", 
      closeOnConfirm: false 
     }, 


     function(isConfirm){ 
      if (isConfirm) { 

      submit_order($('#cakechoices')); 
      //swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
      } 
     }); 

     return false; 
     }); 

希望有人能幫助我!

回答

1

這是由於「禁用」屬性。只需將其刪除,後綴「branch_name」將以空值發送。

您正在將它設置爲禁用,結果是根本沒有發送$ _POST ['branch_name']變量。

將其更改爲:

<option selected value="">-- Select one --</option> 
+0

ohgad,我爲什麼不試試.. *隱藏*永遠感謝你了! – Khyana

+0

不客氣DownCrow。如果您認爲正確,請接受答案 –