*更新了完整的代码示例,在阅读评论后,我想我会发布我仍在努力的示例代码。基本上我希望点击html中的链接,将表单提交给called.php并将其值返回到html页面上的div。
<!DOCTYPE html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function dSubmit(){
$.ajax({
type: "POST",
url: "called.php",
data: $("#projectAddition").serialize(),
success: function(data) {
//here id where you want display
$('#id1').val(data.var1); //here is value1
}
}
}
</script>
</head>
<body>
<form id="projectAddition" action="#" method="POST">
<input type="text" name="projectName">
</form>
<a href="#" onclick="dSubmit()">Submit Form</a>
<br>Output<br>
<div id="id1"></div>
</body>
PHP:
$name = $_POST['projectName'];
// Do something with variable
$array = array(
'var1'=> $name,
'var2'=>'value2'
);
echo json_encode($array);
答案 0 :(得分:0)
首先将变量放入数组中
$array = array(
'var1'=>'value1',
'var2'=>'value2'
);
echo json_encode($array);
然后
在ajax中你得到这种方式
$.ajax({
type: "POST",
url: "url",
data: $("#projectAddition").serialize(),
success: function(data) {
//here id where you want display
$('#id1').val(data.var1); //here is value1
$('#id2').val(data.var2); //here is value2
}
}
答案 1 :(得分:0)
有了更多的修补,看来工作的HTML如下:
// url, formName passed in through function
$.ajax({
type: "POST",
url: url,
data: $("#" + formName).serialize(), // serializes the form's elements.
success: function(data)
{
$("#AtoZ").append('<div id="success">' + data + '</div>');}
PHP方面:
echo $return="Project Added, Reference: $id";
只是想知道如果不使用JSON是一个问题?我唯一传递的是一个字符串,例如'失败'