2013-01-08 19 views
2

我想發送一個哈希字符串到我的PHP文件。但失敗了,任何人都可以幫助我嗎?傳遞字符串使用jQuery的AJAX到PHP

我的JavaScript是這樣的:

var hashString = "#support_form"; 

$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: hashString, 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 

和PHP:

<?php 
$theHash = $_POST['hashString']; 
?> 

我該怎麼辦?由於

+0

HTML崗位工作期望(名稱,值)對。你只提供一個值, –

回答

5

你需要指定數據的名稱/值

data: {hashString:hashString}, 
0

用途:

data: 'hashString=' + encodeURIComponent(hashString), 
1

你要做像這個 -

$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: "hashString=value", 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 

所以,你可以得到值爲 -

$theHash = $_POST['hashString']; 
echo $theHase; //will print value 
+0

請告訴我如何在java中獲得價值 –

0

如果您使用的是對象而不是字符串,那麼您的代碼就可以工作。試試這個:

var hashString = {hashString: "#support_form"}; 

$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: hashString, 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 
0

你的JS應該是這樣的:

var hashString = "#support_form"; 

$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: {hashString: hashString}, 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 
0

數據密鑰必須是對象類型 這會爲你

var hashString = "#support_form"; 

$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: {'hashString':hashString}, 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 
+1

'data'可以是'Object'或'String'。 –

0
$.ajax ({ 
    type: "POST", 
    url:"index.php", 
    data: {"hashString":hashString}, 
    success: function() { 
     console.log("message sent!"); 
    } 
}); 
0
var queryString = "f_name=" + f_name + "&l_name=" + l_name + "&contact_no=" + contact_no + "&email=" + email; 
$.ajax({ 
    type:'POST', 
    url:'insert1.php', 
    data:queryString, 
    dataType:'json', 
    success: function(success){ 
console.log("message sent!"); 
} 
});