使用jquery ajax将字符串传递给php

时间:2013-01-08 18:29:52

标签: php jquery ajax json

我想将哈希字符串发送到我的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'];
?>

我该怎么办?感谢

8 个答案:

答案 0 :(得分:6)

您需要指定数据的名称/值

data: {hashString:hashString},

答案 1 :(得分:2)

你必须这样做 -

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

所以你可以得到值 -

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

答案 2 :(得分:0)

使用:

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

答案 3 :(得分:0)

如果使用对象而不是字符串,则代码将起作用。试试这个:

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

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

答案 4 :(得分:0)

你的JS应该是这样的:

var hashString = "#support_form";

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

答案 5 :(得分:0)

数据键必须是对象类型 这对你有用

var hashString = "#support_form";

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

答案 6 :(得分:0)

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

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