0

I have a jquery mobile survey form.

The first question ask the gender of the participants using {radio buttons}

When a user clicks on the gender radio buttons, and the number of females is equal to 100 then I want the form to be disabled.

I have done the php that connects to mysql database and counts the number of female participants, and it works very well. But how can I use ajax to dynamically connect to the php script and display the results of the php script, immediately when the user clicks on the gender radio buttons.

Please help me.php code that checks database

  • 2
    you provided a very low amount of information for us to help you with this – rsz May 18 '16 at 10:43
  • Please let us know what have tried and also add some code here of what have you you already done – Nehal May 18 '16 at 10:47
  • I have edited my question by uploading a snapshot of the php code that retrieves the data from the database table – Neliswa Astute May 18 '16 at 10:56
  • 1
    Don't include any screenshot, attach some *real* code with your question. And just for the record, your queries are susceptible to SQL injection, if you're using `mysqli`, learn about [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Also see [how you can prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Rajdeep Paul May 18 '16 at 10:59
  • Im currently working offline, im using the stack overflow app to post the question.That is why I used a snapshot.Thank you for pointing out the SQL injection vulnerability – Neliswa Astute May 18 '16 at 11:12

3 Answers3

1

You can add code of change event on radio button and place your ajax code in that so, if your php ajax has some result you can make condition accordingly.

PS: use on change on radio button class and not id as each radio button can have separate id. And if you use same id only first one will be used to check data.

Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
1

Please note that this code is not tested, I've just wrote it just for demonstration:

This JS script mainly checks whether the selected radio button is a female, then sends a check flag through post to a php file.

In your php file you should check if the total number of woman is already 100; then return a message with the type "exceeded" which will be captured in the script, thus you can add required actions such as hiding the form and adding a custom message.

JS:

<script type="text/javascript">
    $('form input[name=female]').on('change', function () {
        if ($('input[name=female]:checked', 'form').val() == "female") {
            var loader = jQuery('#loader');
            loader.fadeIn();
            //add aditional post values values here
            post_data = {
                'check': '1'
            };
            $.post('processing.php', post_data, function (response) {
                if (response.type == "exceeded") {
                    $('#message').html(response.text);
                    $("#message").fadeIn();
                    loader.fadeOut();
                    //Do more actions here like:
                    $('form').slideUp();
                }
            }, 'json');
        }
    });
</script>

processing.php

<?php
if ($_POST) {
    //check if its an ajax request, exit if not
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        $output = json_encode(array(//create JSON data
            'type' => 'error',
            'text' => 'Sorry Request must be Ajax POST'
        ));
        die($output); //exit script outputting json data
    }

    $check = filter_var($_POST["check"], FILTER_SANITIZE_NUMBER_INT);
    if ($check) {
        //check the total nbr of women saved in the database and add it to $total_women
        if ($total_women >= 100) {
            $output = json_encode(array('type' => 'exceeded', 'text' => 'Max nbr reached'));
            die($output);
        }
    }
}
?>

Cheers!

Maroun Melhem
  • 3,100
  • 1
  • 20
  • 22
0

if you echo anything in php server side it retuning the ajax success

so your ajax should catch that show it to user like this

success: function(data) {

       //data is what you echoed in php server side like enough participant like this 

       alert(data); //just alert message or you should append it with html
    }
JYoThI
  • 11,977
  • 1
  • 11
  • 26