2014-01-27 156 views
0

好吧我有一個由PHP動態生成的下拉菜單,並填充從數據庫中獲取的數據。在這方面,它的工作很好,我遇到的問題是使用JS驗證,JavaScript不太好('不喜歡它'),但爲了我的迷你項目的目的,我必須使用它。爪哇腳本下拉菜單驗證

問題是,我用JS檢查用戶是否選擇了下拉菜單中可用的選項之一,因此可以提交表單,否則顯示警告。

所以我想要做的是這個.....給第一個選項顯示「PLEASE SELECT workshop」的值爲「f」,所以如果在提交表單時這個下拉的值菜單== f返回false否則提交表單。

現在,如果在提交值== f錯誤顯示,但如果該值不是== f,我從下拉菜單中選擇一個值我不能繼續提交它不允許我

PHP代碼:

function getForex($link){ 
$timestamp = date("Y-m-d"); 

    $sql = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Forex%' OR course LIKE '%forex%' AND status=5"; 
    $query = mysqli_query($link, $sql); 
     $option='<select id="Forex" name="workshop">'; 
     $option.='<option id="fx" value="Forex">Select Forex Workshop</option>'; 
     $option.=''; 
      while($result = mysqli_fetch_assoc($query)) 
      { 
       if($timestamp < $result['endingDate']) 
       { 
        $option.='<option id="'.$result['id'].'" value='.$result['endingDate'].'>'.$result['course']." ".$result['schedule'].'</option>'; 
       } 
      } 
      $option.='</select>'; 
      return $option;    
    } 

JS代碼:許多非常有用的答案的

var sel=document.getElementById('fx').value="Forex"; 
    if(sel.match("Forex")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('fx').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('fx').style.borderColor = "green"; 
} 

OK傢伙組合幫助,但是當我添加另一個下拉菜單一樣

PHP代碼:

function getBinary($link){ 
$timestamp = date("Y-m-d"); 

    $sql2 = "SELECT id, course, status, endingDate, schedule FROM courses WHERE course LIKE '%Binary%' OR course LIKE '%binary%' AND status=5"; 
    $query2 = mysqli_query($link, $sql2); 
     $option2='<select id="Binary" name="workshop">'; 
     $option2.='<option id="bi" value="Binary">Select Binary Workshop</option>'; 
     $option2.=''; 
      while($result2 = mysqli_fetch_assoc($query2)) 
      { 
       if($timestamp < $result2['endingDate']) 
       { 
        $option2.='<option id="'.$result2['id'].'" value="'.$result2['endingDate'].'">'.$result2['course']." ".$result2['schedule'].'</option>'; 
       } 
      } 
      $option2.='</select>'; 
      return $option2; 
} 

JS代碼:

var selbi=document.getElementById('Binary').value; 
    if(selbi.match("Binary")){ 
     document.getElementById('error').innerHTML = "Choose Binary Workshop Date"; 
     return false; 
} 

的問題變成內在HTML消息只顯示爲外匯,如果沒有選擇外匯避開即扭曲它顯示的信息選擇外匯車間,如果我選擇外匯車間,然後顯示選擇二元車間:D

回答

0

您沒有使用選擇ID使用選擇ID

var sel=document.getElementById('Forex').value; 
    if(sel.match("Forex")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('fx').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('fx').style.borderColor = "green"; 
} 
+0

對不起@Srinu MI不明白你的答案,你可以打破它,我以前使用的是 – Tomazi

+0

這樣VAR SEL =的document.getElementById ( 'FX')值= 「外匯」;所以你使用的選項ID使用選擇ID,因爲值將只存儲在選擇ID,所以使用這個var sel = document.getElementById('Forex')。值;和其餘的代碼是相同的 –

+0

嗨那裏如果我使用你的答案似乎工作,但如果我添加另一個下拉菜單的東西變得混亂,請檢查我編輯的問題 – Tomazi

0

我做的javaScript下拉驗證。當在下拉值「中選擇一個」在那段時間給你的消息「請選擇你的價值」,在github上。項目鏈接。我的孔項目 是Javascript drop-down value validation

+0

如果你想我可以在這裏添加代碼 –

0

,你必須使用下拉ID「外匯」用於獲取下拉選項沒有ID的選擇的值,如下面

var sel = document.getElementById('Forex').value; 
0

HTML代碼

<form id="send" name="myfrom" action="confrimationPage.php" method="POST" onsubmit="return validateForm(this);" > 

       <p> 
        <label for="company">Gender</label> 
        <select id="gender" name="gender"> 
         <option value="Select One">Select One</option> 
         <option>Male</option> 
         <option>Female</option> 
        </select> 
       </p> 

       <p> 
        <label for="country">Postal Code</label> 
        <input id="Country" type="text" name="postalCode" /> 
       </p> 

       <p> 
        <button id="submit" type="submit">Submit</button> 
       </p> 

      </form> 

* 的JavaScript *

<script type="text/javascript"> 

function validateForm(form) 
{ 
    // Gender empty or not 
    var selectGender=document.getElementById('gender').value; 
    if(selectGender=="Select One") 
    { 
     alert("Please Select gender"); 
     return false; 
    } 
    //Postal code empty or not 
    if(form.postalCode.value=="") 
    { 
     alert("Filled the Postal Code Field"); 
     form.postalCode.focus(); 
     return false; 
    } 
} 

</script> 
0
Check It 


var sel=document.getElementById('Forex').value; 
var binary=document.getElementById('Binary').value; 



if(sel.match("Forex")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('Forex').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('error').innerHTML = ""; 
    document.getElementById('Forex').style.borderColor = "green"; 
} 
if(binary.match("Binary")){ 
     document.getElementById('error').innerHTML = "Choose Forex Workshop Date"; 
     document.getElementById('Binary').style.borderColor = "red"; 
     return false; 
}else{ 
    document.getElementById('error').innerHTML = ""; 
    document.getElementById('Binary').style.borderColor = "green"; 
} 
+0

謝謝你嘗試這也不工作不讓我提交...即使所有的條件都滿足,當我點擊提交按鈕什麼也沒有發生 – Tomazi

+0

您是使用 use <輸入類型=「提交」> –

+0

是的,它提交,我現在正在做PHP後端驗證,但我需要前端驗證以及那我不是很擅長,所以我不知道我要去哪裏錯了它代碼看起來不錯,對我來說很有意義:/ – Tomazi