2

Ajax:

function check_user_country_prod(userId , countryCode  , testType )
{ //using alert to check that all data are correct
    $.ajax({
    type: "POST",

    url: "http://localhost/test/testForm.php",

    data: { userId: userId , 
            countryCode : countryCode  ,
             productCode:  testType
        },
    success:function(res) {
        if(res == "OK")
            return true;    
        else
            return false;
    }
});
}

PHP:

<?php
    require_once("Connections/cid.php");

    $userId= $_POST['userId'];
    $productCode= $_POST['productCode'];
    $countryCode= $_POST['countryCode'];

    $sql_check = "SELECT * FROM utc WHERE userId = '$userId' AND productCode = '$productCode' AND countryCode = '$countryCode'";

    $list = mysqli_query( $conn, $sql_check);
    $num  = mysqli_fetch_assoc($list);

    if($num >0)
        echo "OK";

    else
        echo "NOK";

?>

I am very sure that the data i had pass in to the php file are correct. However i cant seem to get my data to the php file and it keep return false value back to me. Anything i can do to make it works?

**Note: Somehow even if i change both result to return true in ajax, it will still return false. and i tried to change the link to another file with only echo "OK"; but it also doesn't work. So i think it is not the file problem. It just never run the ajax no matter what. Do i need to do any link for ajax to run? **

yan
  • 369
  • 1
  • 4
  • 13

4 Answers4

0
function check_user_country_prod(userId , countryCode  , testType )
{ //using alert to check that all data are correct
    $.ajax({
        url: "test/testForm.php"  
        type: "POST",

        url: "http://localhost/test/testForm.php", // This needs to be "test/testForm.php"

        data: { userId: userId , 
            countryCode : countryCode  ,
             productCode:  testType
        },
        success:function(res) {
        if(res == "OK")
            return true;    
        else
            return false;
        }
    });
}
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Puya Sarmidani
  • 1,226
  • 9
  • 26
0

Adjust this code according your input type. I hope this will help you:

$(document).ready(function() {
$("#submit_btn").click(function() { 
    //get input field values
    var user_name       = $('input[name=name]').val(); 
    var user_email      = $('input[name=email]').val();
    var user_phone      = $('input[name=phone]').val();
    var user_message    = $('textarea[name=message]').val();

    //simple validation at client's end
    //we simply change border color to red if empty field using .css()
    var proceed = true;
    if(user_name==""){ 
        $('input[name=name]').css('border-color','red'); 
        proceed = false;
    }
    if(user_email==""){ 
        $('input[name=email]').css('border-color','red'); 
        proceed = false;
    }
    if(user_phone=="") {    
        $('input[name=phone]').css('border-color','red'); 
        proceed = false;
    }
    if(user_message=="") {  
        $('textarea[name=message]').css('border-color','red'); 
        proceed = false;
    }

    //everything looks good! proceed...
    if(proceed) 
    {
        //data to be sent to server
        post_data = {'userName':user_name, 'userEmail':user_email, 'userPhone':user_phone, 'userMessage':user_message};

        //Ajax post data to server
        $.post('contact_me.php', post_data, function(response){  

            //load json data from server and output message     
            if(response.type == 'error')
            {
                output = '<div class="error">'+response.text+'</div>';
            }else{
                output = '<div class="success">'+response.text+'</div>';

                //reset values in all input fields
                $('#contact_form input').val(''); 
                $('#contact_form textarea').val(''); 
            }

            $("#result").hide().html(output).slideDown();
        }, 'json');

    }
});

//reset previously set border colors and hide all message on .keyup()
$("#contact_form input, #contact_form textarea").keyup(function() { 
    $("#contact_form input, #contact_form textarea").css('border-color',''); 
    $("#result").slideUp();
}); });
0

"my current folder is localhost/abc/bsd.php while the one that i am going is localhost/test/testForm.php".

From the above comment, you have to change the ajax url to url: "/test/testForm.php",

Apart from this , your php code shows

$num  = mysqli_fetch_assoc($list);

if($num >0)
    echo "OK";

This is incorrect as mysqli_fetch_assoc returns an associative array of strings. So, the comparison $num >0 is illogical.

0

Try this also both are running on my localhost:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
  $(function () {
   $('form').on('submit', function (e) {
   e.preventDefault();
   $.ajax({
     type: 'post',
     url: 'post.php',
     data: $('form').serialize(),
     success: function (d) {
      alert(d);
     }
    });
   });
  });
    </script>
<form method="post" id="myform">
      <input type="text" name="time" /><br>
      <input type="text" name="date" /><br>
      <input name="submit" type="submit" value="Submit">
    </form>

make new file of post.php and pest this code. This code will alert your input field value:

<?php print_R($_POST); ?>