Reputation: 1542
It's a very simple code, I was trying to do an ajax submit
to work. Until here, ajax is working correct, but Why cannot print out $_POST data?
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;
}
?>
Upvotes: 0
Views: 163
Reputation: 8670
That's because your variable name isn't fieldText
, it's field
. try changing it in your PHP.
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$test = $_POST['field'];
echo $test;
}
?>
Upvotes: 2