Reputation: 679
I have a php file(index.php) with two fields when i post that form, in javascript i am doing form data serialize and send it to next php page(results.php) through ajax. When i try to print the data inside success it is not printing. FInd the below code.
<html>
<head>
<title></title>
<script src="../scripts/jquery-1.9.1.js"></script>
</head>
<body>
<form method="post" name="index" id="indexform">
<table border="1">
<tr>
<td>Name:</td>
<td><input type="text" name="fname"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="sendData"></td>
</tr>
</table>
</form>
</body>
<script type="text/javascript">
$( "#indexform" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $(this).serialize() );
var formdata = $(this).serialize();
// alert(formdata);
$.ajax({
type:"POST",
url:"result.php",
dataType:'json',
data:formdata,
success: function(data){
alert(data);
}
});
});
</script>
In the above i cant print the data inside the success callback.
Upvotes: 3
Views: 16379
Reputation: 4620
Try this
<script type="text/javascript">
$( "#indexform" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $(this).serialize() );
var formdata = $(this).serialize();
$.ajax({
type:"POST",
url:"result.php",
data:formdata,
success: function(html){
alert(html);
}
});
});
</script>
In your result.php page
$name=$_REQUEST['fname'];
$email=$_REQUEST['email'];
echo $name." ".$email;
Upvotes: 4
Reputation: 5824
Hello My Friends your html code is in
<script type="text/javascript">
$( "#indexform" ).on( "submit", function( event ) {
event.preventDefault();
console.log( $(this).serialize() );
var formdata = $(this).serialize();
// alert(formdata);
$.ajax({
type:"POST",
url:"result.php",
dataType:'json',
data:formdata,
success: function(data){
alert(data);
}
});
});
</script>
and you result.php file code is in
<?php
include 'db.php';
$name=$_REQUEST['fname'];
$email=$_REQUEST['email'];
echo $name." ".$email;
?>
now the result.php file is return the entered name and email address.
Upvotes: 0
Reputation: 601
Echo the content inside results.php page.
Ex:
echo "two fields $first_field , $second_field successfully inserted. " ;
This content results as an ajax response and stores in the data variable .
Upvotes: 0