2016-07-21 54 views
0

我非常接近這個工作。我只是有一個變量(q3)的問題。Ajax與其他參數序列化,

我一直在努力使用ajax,php和MySQL獲取一些數據到數據庫中。除了問題3之外,它全部正在工作。所有的數據提交,但問題3,在數據庫中,始終是空的...

有沒有人有任何想法?我是使用serialize()的新手,但是在控制檯中記錄它會顯示一個所有選中框的字符串。

請注意,這只是代碼的一個子集。 謝謝。

HTML:

<div id="question3" class="question_block" style="display:block;"> 

    <p class=""><b>3. If you don’t use the bus system, please let us know why (pick your top 3 reasons):</b></p> 

    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Buses do not go where I want to go" /> Buses do not go where I want to go 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Buses do not run when I need it" /> Buses do not run when I need it 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Buses are too crowded" /> Buses are too crowded 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Buses are too crowded" /> Buses are unreliable 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Transfers between bus routes are difficult" /> Transfers between bus routes are difficult 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="Waiting times are too long" /> Waiting times are too long 
    <input type="checkbox" name="question3_answer[]" class="question3_answer" value="I don’t live near a bus route" /> I don’t live near a bus route 
    <input id="question3_other" type="checkbox" name="" class="question3_answer other" value="other" />Other: 
    <input type="text" id="question3_textanswer" name="question3_answer[]" class="question3_answer" placeholder="Your answer here" style="width:40%;display:none;"> 

    <button id="toPlay" onclick="submit_1();">Submit and continue</button> 
</div> 

阿賈克斯:

function submit_1() { 
     var q1 = document.getElementById("question1_answer").value; 
     var q2 = document.getElementById("question2_answer").value; 
     var q3 = $('.question3_answer:checked').serialize(); 
     var q4 = document.getElementById("question4_answer").value; 
     var q5 = document.getElementById("question5_answer").value; 

     var dataString = 'question1_answer=' + q1 + '&question2_answer=' + q2 + '&question3_answer=' + q3 + '&question4_answer=' + q4 + '&question5_answer=' + q5; 

     $.ajax({ 
      type: "POST", 
      url: "php/video_form_process.php", 
      data: dataString, 
      cache: false, 
      success: function(html) { 
       alert(html); 
      } 
     }); 
     return false; 
    } 

PHP

if (isset($_POST['question1_answer'])) { 
// Gets data from URL parameters 
$question1_answer = $_POST['question1_answer']; 
$question2_answer = $_POST['question2_answer']; 
$question3_answer = $_POST['question3_answer']; 
$question4_answer = $_POST['question4_answer']; 
$question5_answer = $_POST['question5_answer']; 

    $query= "INSERT INTO `responses` (`uid`, `time`, `q1`,`q2`,`q3`,`q4`,`q5`, `ipaddr`) VALUES ('', '$today','$question1_answer','$question2_answer','$question3_answer','$question4_answer','$question5_answer')"; 

    if ($query_run = mysqli_query($server, $query)) { 
     echo 'success'; 
    } else { 
     echo 'error'; 
    } 
} 

工作的Ajax:

function submit_1() { 
     var q1 = $(".question1_answer:checked").val(); 
     var q2 = document.getElementById("question2_answer").value; 

     var q3 = []; 
     $(".question3_answer:checked").each(function() { 
      q3.push($(this).val()); 
     }); 

     var q4 = document.getElementById("question4_answer").value; 
     var q5 = document.getElementById("question5_answer").value; 

     var dataString = 'question1_answer=' + q1 + '&question2_answer=' + q2 + '&question3_answer=' + q3 + '&question4_answer=' + q4 + '&question5_answer=' + q5; 

     $.ajax({ 
      type: "POST", 
      url: "php/video_form_process.php", 
      data: dataString, 
      cache: false, 
      success: function(html) { 
       alert(html); 
      } 
     }); 
     return false; 
    } 

工作的PHP:

if (isset($_POST['question1_answer'])) { 
// Gets data from URL parameters 
$question1_answer = mysqli_real_escape_string($server, $_POST['question1_answer']); 
$question2_answer = mysqli_real_escape_string($server, $_POST['question2_answer']); 
$question3_answer = mysqli_real_escape_string($server, $_POST['question3_answer']); 
$question4_answer = mysqli_real_escape_string($server, $_POST['question4_answer']); 
$question5_answer = mysqli_real_escape_string($server, $_POST['question5_answer']); 

$query= "INSERT INTO `responses` 
     (`time`, `q1`,`q2`,`q3`,`q4`,`q5`, `ipaddr`) 
     VALUES ('{$today}', '{$question1_answer}', '{$question2_answer}', '{$question3_answer}', '{$question4_answer}', '{$question5_answer}', '127.0.0.1')"; 

if ($query_run = mysqli_query($server, $query)) { 
    echo 'success'; 
} else { 
    echo 'error'; 
} 

}

+2

我很驚訝,你是怎麼得到了所有輸入值?.......你r分別輸入字段具有相同的ID .... –

+1

ID是單一的... ...' ID =「question3_answer」'< - 具有相同ID的多個元素無效 – epascarello

+0

就打算把它擺在那裏,使用''