0

I am making a form to check a security code. In fact, I am new to ajax and jquery, so I tried what I can, but my code doesn't work. Can anybody help me?

php file :

<?php 
include('/includes/db-connect.php');

if( isset($_POST["seccode"]) ){
    $result=mysqli_query($con,"SELECT * FROM `certificate_acheived_tbl` WHERE `cert_check_code` = ".$seccode.")";
    if( mysql_num_rows($result) == 1) {    
        echo "<script>alert('s')";
    }
}
?>

js file:

$(function() {
    $(".btn btn-success").click(function() {
        var ID = $(this).attr('id');

        $.ajax({
            type: "POST",
            url: "cert-check-ajax.php",
            data: 'certcode='+ ID,
            success: function() {
                $('#someHiddenDiv').show();
                console.log();
            }
        });
    });
});
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 1
    *"doesn't work"* is simply not a substantive enough problem statement for anyone to do anything but guess. Time to read up on basic troubleshooting and debugging or provide a lot more detail – charlietfl Nov 12 '17 at 20:53
  • where you define `$seccode`? – Sysix Nov 12 '17 at 20:58
  • it is in the form – gfdfgf fgfdfg Nov 12 '17 at 21:10
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Nov 13 '17 at 02:47

2 Answers2

0

your code is bad... (sometimes mine too)

One first mistake : data: 'certcode='+ ID, in jQuery

and isset($_POST["seccode"]) in PHP 'certcode' != 'seccode'

so a better code.. ?

jQuery (I allways use JSON, it's more easy)

$(function () {
    $(".btn btn-success").click(function() {
        var 
        Call_Args = {
            certcode:  $(this).attr('id')
        };
        $.ajax({
            url: 'cert-check-ajax.php',  
            type: 'POST',
            data: Call_Args,
            cache: false,
            dataType: 'json',
            success: function (data) {
                console.log( data.acceptCod );    // or data['acceptCod'] if you want
                $('#someHiddenDiv').show();
                // ...
            } 
        }); //$.ajax
    });  // btn btn-success").click
});

PHP (with utf8 insurance, and good header / JSON encode response )

<?php
mb_internal_encoding("UTF-8");
include('/includes/db-connect.php');

$T_Repons['acceptCod'] = "bad";

if (isSet($_POST['certcode'])) {
    $sql = "SELECT * FROM `certificate_acheived_tbl` ";
    $sql .= "WHERE `cert_check_code` = ".$_POST['certcode'].")";

    $result = mysqli_query($con, $sql);

    $T_Repons['acceptCod'] = (mysql_num_rows($result) == 1) ? "ok" : "bad";
}
header('Content-type: application/json');
echo json_encode($T_Repons);
exit(0);
?>
  • $T_Repons['acceptCod'] = ( mysql_num_rows($result) == 1) ? "ok", "bad"; saying ( ! ) Parse error: syntax error, unexpected ',' – gfdfgf fgfdfg Nov 12 '17 at 23:47
  • Did you notice my new correction and testing it ? no answer or anything; is it something wrong ? –  Nov 15 '17 at 23:59
-1

you can use it

$(function() {
    $(".btn btn-success").click(function() {
        var ID = $(this).attr('id');
        $.ajax({
            type: "POST",
            url: "cert-check-ajax.php",
            data: {'seccode': ID}
        }).done(function(data) {
            $('#someHiddenDiv').show();
            console.log(data);
        });
    });
});
VMT
  • 191
  • 10
  • if you compare my script with your script, you can see – VMT Nov 12 '17 at 21:18
  • First of all I am not the OP. Second there is nothing inherently wrong with the way their ajax is configured. String vs object for data is minor variation...both are correct. Same with `done()` vs `success`. You haven't explained why your solution would help – charlietfl Nov 12 '17 at 21:20