这是一个非常简单的代码,我试图做一个ajax submit
来工作。直到这里,ajax都可以正常工作,但是为什么不能打印$ _POST数据?
console.log
<br />
<b>Notice</b>: Undefined index: fieldText in <b>C:\xampp\htdocs\rajax.php</b> on line <b>4</b><br />
sendajax.php
<form method="POST">
<input type="text" name="fieldText" value="">
<button type="submit" id="save">Send</button>
</form>
<script type="text/javascript">
$(document).ready(function(){
//alert("Jquery's Working");
$("#save").click(function(e){
e.preventDefault();
//alert("Click Event is working");
$.ajax({
type:"POST",
url:'rajax.php',
data: {field: $("input[name=fieldText]").val()},
success: function(result){
console.log(result);
//alert($("input[name=fieldText]").val()); #Print Value is working
},
error: function(result){
console.log(result);
}
});
});
});
</script>
recajax.php
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$test = $_POST['fieldText'];
echo $test;
}
?>
答案 0 :(得分:2)
那是因为您的变量名不是fieldText
,而是field
。尝试在您的PHP中进行更改。
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$test = $_POST['field'];
echo $test;
}
?>