2015-09-25 119 views
2

阿賈克斯:將數據傳遞到PHP

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文件,並保持返回false值給我。我能做些什麼來使它工作?

**注意:不知何故,即使我改變兩個結果返回true在Ajax,它仍然會返回false。 和我試圖改變鏈接到另一個文件只有回聲「OK」;但它也不起作用。所以我認爲這不是文件問題。無論如何,它都不會運行ajax。我需要做任何鏈接爲ajax運行? **

+0

您能看到控制檯中的任何錯誤嗎? –

+0

@NiranjanNRaju沒有錯誤 – yan

+1

更改ajax url'url:「test/testForm.php」' –

回答

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; 
     } 
    }); 
} 
+0

它不工作.. – yan

+0

我不認爲,http:// localhost將工作。如果它在一個不同的文件夾嘗試../去一個文件夾,看看是否可行 –

+0

我目前的文件夾是localhost/abc/bsd.php,而我要去的是localhost/test/testForm.php – yan

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

嗯..我正在使用數據庫來驗證,所以我不認爲它的作品。感謝您試圖幫助 – yan

+0

好吧,但您可以刪除此驗證。只使用你需要的部分 –

+0

你可以看到另一個例子 –

0

「我的當前文件夾爲localhost/ABC/bsd.php而我要去一個爲localhost /測試/ testForm.php」。

從上面的評論,你必須在AJAX網址更改爲 url: "/test/testForm.php"

除此之外,你的PHP代碼顯示

$num = mysqli_fetch_assoc($list); 

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

這是不正確的,mysqli_fetch_assoc返回一個字符串的關聯數組。所以,比較$num >0是不合邏輯的。

+0

之前嘗試過。不工作。任何其他可能的方式? – yan

+0

你確定這條路嗎?像正確位置的斜槓? –

+0

是的。我已將路徑複製到網址以進行測試。我相信這是正確的 – yan

0

嘗試,這也都被我的本地主機上運行:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
 
    <script> 
 
\t \t $(function() { 
 
\t \t \t $('form').on('submit', function (e) { 
 
\t \t \t e.preventDefault(); 
 
\t \t \t $.ajax({ 
 
\t \t \t \t \t type: 'post', 
 
\t \t \t \t \t url: 'post.php', 
 
\t \t \t \t \t data: $('form').serialize(), 
 
\t \t \t \t \t success: function (d) { 
 
\t \t \t \t \t \t alert(d); 
 
\t \t \t \t \t } 
 
\t \t \t \t }); 
 
\t \t \t }); 
 
\t \t }); 
 
    </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>

使post.php中和有害的代碼的新文件。此代碼將提醒您的輸入字段值:

<?php print_R($_POST); ?>