Eric.18 Eric.18 - 4 months ago 16
PHP Question

Ajax serialize with other parameters,

I am very close on this working. I am just having issues with one variable (q3).

I have been working to get some data into a database using ajax, php and MySQL. It is all working except question 3. All of the data submits but question 3, in the database, is always empty...

Does anyone have any ideas? I am new to using serialize() but logging it in the console shows a string of all checked boxes.

Please note this is only a subset of code. Thank you.

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" id="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" id="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" id="question3_answer" value="Buses are too crowded" /> Buses are too crowded
<input type="checkbox" name="question3_answer[]" class="question3_answer" id="question3_answer" value="Buses are too crowded" /> Buses are unreliable
<input type="checkbox" name="question3_answer[]" class="question3_answer" id="question3_answer" value="Transfers between bus routes are difficult" /> Transfers between bus routes are difficult
<input type="checkbox" name="question3_answer[]" class="question3_answer" id="question3_answer" value="Waiting times are too long" /> Waiting times are too long
<input type="checkbox" name="question3_answer[]" class="question3_answer" id="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" id="question3_answer" 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>


Ajax:

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';
}
}

Answer

Try something like this to post value.

using array push you can add your checkbox value in s3 variable see below code.

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