将数据从ajax传递给php

时间:2015-09-25 09:05:03

标签: php jquery ajax

的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";

?>

我非常确定我传入php文件的数据是正确的。但是我似乎无法将我的数据发送到php文件,它会将错误的值返回给我。我能做些什么来使它有效?

**注意:不知怎的,即使我将两个结果都更改为在ajax中返回true,它仍然会返回false。 我试图改变链接到另一个文件只有echo&#34; OK&#34 ;;但它也不起作用。所以我认为这不是文件问题。它无论如何都不会运行ajax。我是否需要为ajax运行任何链接? **

4 个答案:

答案 0 :(得分: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;
        }
    });
}

答案 1 :(得分:0)

根据您的输入类型调整此代码。我希望这会对你有所帮助:

$(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();
}); });

答案 2 :(得分:0)

“我当前的文件夹是localhost / abc / bsd.php,而我要去的那个是localhost / test / testForm.php”。

从上面的评论中,您必须将ajax网址更改为 url: "/test/testForm.php"

除此之外,您的PHP代码显示

$num  = mysqli_fetch_assoc($list);

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

这是不正确的,因为mysqli_fetch_assoc返回一个关联的字符串数组。因此,比较$num >0是不合逻辑的。

答案 3 :(得分:0)

尝试这两个也在我的localhost上运行:

&#13;
&#13;
<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>
&#13;
<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>
&#13;
&#13;
&#13;

制作post.php的新文件并查看此代码。此代码将提醒您输入字段值:

<?php print_R($_POST); ?>