2017-03-28 6 views
0

私はアプリを持っており、ラジオボタンとチェックボックスを取得してDBに情報を入力しようとしています。現時点では、テキストフィールドのみが情報をデータベースに正しく入力しますが、チェックボックスとラジオボタンは最初のラジオボタンの値をDBに入力します。たとえば、ラジオボタンの場合、「いいえ」が選択されていても「はい」がDBに入力されます。私は何が間違っているのか分かりません。ここでラジオボタン/チェックボックスがAJAXを使用してMySQL DBに入力されない

は、関連するHTML機能です:ここ

function saveUserInfo(userName, userEmail, optedIn, currentUser, catDog, otherBrand) 
    { 
      if(optedIn === "Yes") 
      { 
       finalAnswers = userName + "yes" + "~" + userEmail + "~" + optedIn + "~" + currentUser + "~" + catDog + "~" + otherBrand + "~"; 
      }else{ 
       finalAnswers = userName + "no" + "~" + userEmail + "~" + optedIn + "~" + currentUser + "~" + catDog + "~" + otherBrand + "~"; 
       // finalAnswers = userName + "~" + userEmail + "~0" + optedIn + "~1" + currentUser + "~2" + catDog + "~3" + otherBrand + "~4"; 
      } 

     try 
     { 
      $.ajax({ 
      url: surveyServer + finalAnswers, 
      type: 'post', 
      success: function(evt){ 
       console.log('success saving ' + finalAnswers); 
      } 
      }); 
     } 
     catch(err) 
     { 
     alert(err.message); 

     } 
    } 


function saveUser() 
{ 
    $('.wrapper').find("#userEmail").blur(); 
     if (($('#userName').val().length > 0) && ($('#userEmail').val().length > 0)) 
     { 
      var testEmail = /^[A-Z0-9._%+-][email protected]([A-Z0-9-]+\.)+[A-Z]{2,4}$/i 
      var valueToTest = $('#userEmail').val(); 
      if (testEmail.test(valueToTest)) 
      { 
       //pass 
       saveUserInfo($('#userName').val(), $('#userEmail').val(), $('#optedIn').val(), $('#currentUser').val(), $('#catDog').val(), $('#otherBrand').val()); 
       $('.wrapper').slick('slickNext'); 
      } else { 
       // Do whatever if it fails. 
     alert("Please enter a valid email address."); 
       $('.wrapper').find("#userEmail").focus(); 
      } 

     }else{ 
      alert("Please enter your name and email address then click Submit."); 
      $('.wrapper').find("#userName").focus(); 
     } 
    } 

はsurveySurverコードです:ここで良いメジャーの

if (isset($_GET['user'])) 
    { 
      try 
      { 
       $dbh = new PDO('mysql:host=localhost; dbname=bp1234', 'bp1234','bp1234'); 


       $userAnswerArray = explode("~", $_GET['user']); 

       $stmt = $dbh->prepare("INSERT INTO bpQuiz (userName, userEmail, optedIn, currentUser, catDog, otherBrand) VALUES (:userName, :userEmail, :optedIn, :currentUser, :catDog, :otherBrand)"); 

       $stmt->bindParam(':userName', $userName); 
       $stmt->bindParam(':userEmail', $userEmail); 
       $stmt->bindParam(':optedIn', $userOption); 
       $stmt->bindParam(':currentUser', $currentUser); 
       $stmt->bindParam(':catDog', $catDog); 
       $stmt->bindParam(':otherBrand', $otherBrand); 

       // insert value 
       $userName = $userAnswerArray[0]; 
       $userEmail = $userAnswerArray[1]; 
       $userOption = $userAnswerArray[2]; 
       $currentUser = $userAnswerArray[3]; 
       $catDog = $userAnswerArray[4]; 
       $otherBrand = $userAnswerArray[5]; 
       $stmt->execute(); 

      } 

      catch (PDOException $e) 
      { 
        echo 'ERROR: ' . $e->getMessage(); 
      } 

    } 
    else 
    { 
     echo "no querystring..."; 
    } 

フォームフィールドです:

 <input type="text" class="contactFormInputBox" name="userName" id="userName"/> 
     <input type="text" class="contactFormInputBox" name="userEmail" id="userEmail"/> 
     <input type="checkbox" name="optedIn" id="optedIn" value="Yes"> 
     <input type="radio" name="currentUser" value="1" id="currentUser">Yes 
     <input type="radio" name="currentUser" value="0" id="currentUser">No 
     <input type="radio" name="catDog" value="cat" id="catDog">Cat 
     <input type="radio" name="catDog" value="dog" id="catDog">Dog 
     <input type="radio" name="catDog" value="both" id="catDog">Both 
     </div> 
     <div class="surveyOptions" id="Q2-b"> 
     <input type="text" class="contactFormInputBox" name="otherBrand" id="otherBrand"/> 
+3

はラジオからの 'のid ='を削除しますボタン。 IDは一意でなければなりません。ラジオは 'name =' not idでマッチし、idではなく名前で投稿されますが、idは混乱するかもしれません。 –

+0

ありがとう、私はこれを試して、すべての3つの値を未定義に設定します。しかし、その後私はJQueryを使ってラジオボタンの値を取得していました。以下の回答を投稿してください。 – Hennessey

+0

申し訳ありませんが、JSをフォームの解析場所に近づけてください。問題は '$( '#userName')。val()' - これはIDに関係なく常に最初に一致する要素を与えます*選択された*要素ではありません。 '$("#userName:selected ")。val()'に変更した可能性があります。しかし、キーは ':selected 'を使用しています。 –

答えて

0

私は削除"ID ="は自由なn-mの提案に従って、また私はvalを引っ張る次のコードを変更しなければならなかったチェックするラジオボタン/チェックボックスからのUE(すなわち:$( "入力[タイプ=チェックボックス] [名= optedIn]:チェックする").val()):

function saveUser() 
    { 
     $('.wrapper').find("#userEmail").blur(); 
      if (($('#userName').val().length > 0) && ($('#userEmail').val().length > 0)) 
      { 
       var testEmail = /^[A-Z0-9._%+-][email protected]([A-Z0-9-]+\.)+[A-Z]{2,4}$/i 
       var valueToTest = $('#userEmail').val(); 
       if (testEmail.test(valueToTest)) 
       { 
        //pass 
        saveUserInfo($('#userName').val(), $('#userEmail').val(), $("input[type=checkbox][name=optedIn]:checked").val(), $("input[type=radio][name=currentUser]:checked").val(), $("input[type=radio][name=catDog]:checked").val(), $('#otherBrand').val()); 
        $('.wrapper').slick('slickNext'); 
       } else { 
        // Do whatever if it fails. 
      alert("Please enter a valid email address."); 
        $('.wrapper').find("#userEmail").focus(); 
       } 

      }else{ 
       alert("Please enter your name and email address then click Submit."); 
       $('.wrapper').find("#userName").focus(); 
      } 
     } 
関連する問題