2

I have a html form with <input type='radio'>

<form action="results.php" method="post" enctype="multipart/form-data" onsubmit='return false'><br>
            <p>Have you ever turned a client down?</p>
            <div id="q_1">
                <input type="radio" name="q1" id="q_1_yes" value="yes">
                <label for="q_1_yes">Yes</label>
                <input type="radio" name="q1" id="q_1_no" value="no">
                <label for="q_1_no">No</label>
            </div><br>
            <p>Are you comfortable with failure?</p>
            <div id="q_1">
                <input type="radio" name="q2" id="q_2_yes" value="yes">
                <label for="q_2_yes">Yes</label>
                <input type="radio" name="q2" id="q_2_no" value="no">
                <label for="q_2_no">No</label>
            </div><br>
            <input type="submit" onclick='return handleClick();' name="sub_eit" id="sub_eit" value="Submit">
</form>

I have a javascript function to check if the amount of radio buttons with value "yes" is greater than the amount with value "no" as below

function handleClick()
{
var amountYes = 0;
for(var i = 1; i <= 4; i++) {
    var radios = document.getElementsByName('q'+i);
    for(var j = 0; j < radios.length; j++) {
        var radio = radios[j];
        if(radio.value == "yes" && radio.checked) {
            amountYes++;
        }
    }
}
//code to perform php insert function if yes is less than or equal to 2 
if (amountYes <= 2) {
   $.ajax({
       type: "POST",
       url: "results.php",
       dataType: "json",
       success: function (response) {
       }
   });
} else {
    alert("Correct Responses: " + amountYes);
}
}

results.php

if (isset($_POST['q_1']) && isset($_POST['q_2']))
{
$q_1 = $_POST['q_1'];
$q_2 = $_POST['q_2'];

$yes = "yes";
$no = "no";

$id = $_SESSION['id'];
$u_name = $_SESSION['uname'];

    $recommend = "Executive";

    $osql =<<< EOF
INSERT INTO results (id, recomend, u_name, status) VALUES ('$id', '$recommend', '$u_name', '1');
EOF;
    $ret = $db->exec($osql);
}

But the ajax code doesn't seem to work. Please what is the issue. Thanks in advance for your help. Highly appreciate

diagold
  • 493
  • 1
  • 7
  • 28
  • *How* does it not work? What are you expecting to happen? What is actually happening? What have *you* done to troubleshoot this? – John Conde Apr 28 '17 at 12:41
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – John Conde Apr 28 '17 at 12:41
  • @JohnConde i want it to insert to database what is in the php code but it is not inserting and i am this is a quick trial for a quiz app i am trying to create and would handle security later – Oke Tega Apr 28 '17 at 12:44
  • Please take a look here. http://stackoverflow.com/questions/20769364/insert-data-through-ajax-into-mysql-database – Vandolph Reyes Apr 28 '17 at 12:48
  • Thanks @DanMiller but it didn't work – diagold Apr 28 '17 at 12:50

1 Answers1

2

You have include post data in your ajax request.

 $.ajax({
  type: 'POST',
  url: 'results.php',
  data: $('form').serialize(),
  dataType: 'json',
  success: function( response){
     console.log( 'the feedback from your result.php: ' + response);
  }
});

and in your result.php you have to rename $_POST['q_1'] to _POST['q1'] and so on. q1 and q2 is the name of your input radio.

Andrew Reyes
  • 152
  • 8
  • Thanks for pointing out the q_1 issue for me forgot to change that. and your answer works perfectly. – diagold Apr 28 '17 at 13:02