1

jQuery code

$.ajax({
    url: "far-area_m.php",
    type: "POST",
    dataType: 'json',
    data: values ,
    success: function (response) {
        alert("sdfds");
        // you will get response from your php page (what you echo or print)                        
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus);
        console.log(textStatus, errorThrown);
    }               
});

far-area_m.php

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname='lateral';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$len = $_REQUEST["len"];
$bre = $_REQUEST["bre"];
$wid = $_REQUEST["wid"];

$sql = mysqli_query(
    $conn,
    "INSERT INTO `far_input` (`length`, `breadth`, `width`)
    VALUES
    ('".$len."', '".$bre."', '".$wid."')
    ");
$data = array(
    'len'     => $len,
    'bre'  => $bre,
    'wid'   => $wid
);

echo json_encode($data);
?>

always Ajax response is printing error.But if I comment the insert statement in far-area_m.php I am getting response. Please tell me what is wrong with the code. on sucess of jquery I am not getting any response its throwing error.

Zim84
  • 3,404
  • 2
  • 35
  • 40
Namratha Sri
  • 17
  • 1
  • 1
  • 8

1 Answers1

0

You posted an incomplete ajax code. Hope you will find what you are doing wrong there, may be error is happening because of not usingreturn false;at the end of code.It stops form submission manually.

your code should be like this:

$('#form').submit(function(){

var values = $('#form').serialize();

$.ajax({
url: "far-area_m.php",
type: "POST",
dataType: 'json',
data: values ,
success: function (response) {
    alert("sdfds");
   // you will get response from your php page (what you echo or print)                 
},
error: function(jqXHR, textStatus, errorThrown) {
    alert(textStatus);
   console.log(textStatus, errorThrown);
}


});
return false;
});
Crunch Much
  • 1,537
  • 1
  • 11
  • 14