2011-05-29 135 views
3

嘿傢伙, 試圖做一件非常簡單的事情:將2個文本變量傳遞給一個php腳本並將它們插入到MySQL數據庫中。由於某些原因,但我無法讓變量通過(所以我只是在我的數據庫中獲得空記錄)。AJAX + jQuery ...變量不傳遞給PHP

function ajaxCall(){ 

     $.ajax({ 
      type: "GET", 
      url: "http://www.*.be/bubblingAjax.php", 
      cache: false, 
            data: "colour="+colour+"&size="+size, 
     dataType: "html", 
      success: onSuccess 
     }); 
     return false; 
    }; 

而且PHP:

<?php 
    try 
    { 
     $connection = mysql_connect("#"); 
     mysql_select_db("#"); 

     $colour = mysql_real_escape_string($_GET['colour']); 
     $size = mysql_real_escape_string($_GET['size']); 

     mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')"); 
     mysql_close($connection); 
     echo "SUCCESS"; 
     echo $colour; 
     echo $size; 
    } 
    catch(Exception $e) 
    { 
     echo $e->getMessage(); 
    } 
?> 

任何人都願意採取快速看一下它,並指出我的 - 可能obvious-錯誤?這讓我瘋狂了一天!

謝謝!

+0

'mysql_select_db(「#)';您忘記了一個報價或者是錯誤的? – pltvs 2011-05-29 10:59:58

+1

啊,只是在審查時拿出來,在部署腳本中是正確的;) – user775060 2011-05-29 11:08:14

+0

使用螢火蟲或類似的設備。 ajax請求的標題,以查看你是否錯誤不在javascript中,例如你可以在$ .ajax()塊之前做一個「alert(size)」 – Raveline 2011-05-29 11:16:39

回答

0

這工作:

<script type="text/javascript"> 
$(document).ready(function() { 
    //you can wrap the code into an event, e.g click() 
    var colour=... 
    var size=... 
    $.post("http://www.website.com/bubblingajax.php", { colour: colour, size: size }, 
    function(data) { 
    alert("Respond: " + data); 
    }); 
}); 

</script> 

和PHP(只改到發佈)

<?php 
    try 
    { 
     $connection = mysql_connect("#"); 
     mysql_select_db("#"); 

     $colour = mysql_real_escape_string($_POST['colour']); 
     $size = mysql_real_escape_string($_POST['size']); 

     mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')"); 
     mysql_close($connection); 
     echo "SUCCESS"; 
     echo $colour; 
     echo $size; 
    } 
    catch(Exception $e) 
    { 
     echo $e->getMessage(); 
    } 
?> 

而且調試,我會建議使用螢火蟲或鉻的內置檢查工具。

+0

請儘快試一試。調試的問題在於,這只是一小部分,只是一個運行在手機上的更大應用程序(因此沒有鉻或螢火蟲)。 – user775060 2011-05-29 13:03:29

+0

謝謝,這個工程! – user775060 2011-05-29 13:24:47

0

「數據」參數作爲POST變量而不是GET變量發送。在PHP 嘗試$ _ POST

+0

將它交換到發佈,仍然只是吐空記錄... – user775060 2011-05-29 11:04:50

+0

數據參數不作爲發送請求獲取請求。jquery其實足夠聰明將數據值轉化爲查詢字符串參數 – Ben 2011-05-29 11:19:24

0

測試這在test.php的文件(名稱事項):

<?PHP 
if(isset($_POST['user_name'])) 
{ 
$post_output= 
'Hello '.strtoupper($_POST['user_name']).' from '.strtoupper($_POST['user_city']).'! 

This is an other random:'.rand(23,46).'. 

The previous random is still alive! 

I guess you can insert these 2 values 
in the database on your own now, don\'t you?!'; 


$get_output= 
' 


___________________________________________ 
Well if you insist you can keep using get on parallel 
This is what $_GET says: 

'.strtoupper($_GET['getMessage']).' 

And finally you can avoid post at all, to do that: 
1.Use get instead of post inside the insertToDB function 
2.Use send(null) instead of send(params) 
3.Don\'t send the headers 

HOWEVER I LIKE POST!'; 
echo $post_output; 
print $get_output; 
exit; 
} 
?> 

<html> 

<head> 

<script language="javascript" type="text/javascript" > 
<!-- 

var request = false; 
try { 
    request = new XMLHttpRequest(); 
} catch (trymicrosoft) {       
    try { 
    request = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (othermicrosoft) { 
    try { 
     request = new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (failed) {     
     request = false;  
    } 
    } 
} 

if (!request) 
    alert("Error initializing XMLHttpRequest!"); 

    function insertToDB() 
    { 
     var url = "test.php?getMessage=Hi%20There!%20Use%20me%20if%20you%20like.."; 
     var params = "user_name=" + (document.getElementById("user_name").value)+ 
     "&user_city="+(document.getElementById("user_city").value); 

     request.open("POST", url, true); 

     request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); 
     request.setRequestHeader("Content-length", params.length); 
     request.setRequestHeader("Connection", "close"); 

     request.onreadystatechange = updatePage; 
     request.send(params); 

    }//////////////////// 

    //You're looking for a status code of 200 which simply means okay. 
    function updatePage() { 
    if (request.readyState == 4) { 
     if (request.status == 200) 
     { 
     //alert(decodeURIComponent(request.responseText)); 

      alert(request.responseText); 
     } 
     else{ 
     //alert("status is " + request.status); 
     } 
    } 
    } 


// --> 
</script> 
</head> 

<html> 
<body> 

This is a random:<?PHP echo rand(1,23);?><br> 


<input type="text" name="name" value="your name here" size=80 id='user_name' ><br> 
<input type="text" name="city" value="your city goes here" size=80 id='user_city' ><br> 
<input type="submit" name="bttn" value="Go" onClick="insertToDB();" > 




</body> 
</html> 
0

手機上運行

好吧,我不知道我上面的腳本將做一部手機,它需要啓用Javascript!