0

I am trying to retrieve data from database using AJAX without any success. These are the codes I am using. I dont see any specific errors in console.

HTML:

<button type="button" name="result_submit" id="result_submit" >Submit</button>
<div class="result" id="result" name="result"> </div>

Jquery:

$(document).ready(function(e) {
    $('#result_submit').click(function() {

        $.ajax({
            url  :"Income.php",
            type :'POST',
            success: function(data){
                $("#result").html(data);
            }
        });
    }); 
});

Income.php content:

<?php

include_once 'dbConnection.php';

$stmt = mysqli_stmt_init($conn);

$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";

    if(!mysqli_stmt_prepare($stmt,$income)) 

        {
            $message =  '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
        } 
        else 
        {
            mysqli_stmt_execute($stmt);
            $result= mysqli_stmt_get_result($stmt);
            $income_sum=mysqli_fetch_assoc($result); 
            $TotIncome= "Total Income is ".$income_sum['incomeNumber'];
        }
?>

dbConnection.php has connections details:

<?php

$dbServername = "localhost";    
$dbUsername = "root";
$dbPassword = "";
$dbName = "wordpress";

$conn= mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

?>

Can someone guide me how to resolve the issue

3
  • 3
    You just need to echo your data from your PHP file. Commented Oct 22, 2018 at 11:30
  • write echo $TotIncome;exit(); and it will work Commented Oct 22, 2018 at 11:31
  • echo is not working Commented Oct 22, 2018 at 11:45

3 Answers 3

3

You need to echo your data from your PHP file :

$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";
$response = '';

if (! mysqli_stmt_prepare($stmt,$income)) {
     $response = '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
} else {
     mysqli_stmt_execute($stmt);
     $result = mysqli_stmt_get_result($stmt);
     $income_sum = mysqli_fetch_assoc($result); 
     $response = "Total Income is ".$income_sum['incomeNumber'];
}

echo $response;
Sign up to request clarification or add additional context in comments.

5 Comments

No luck with it. I dont see any errors also in console
Try to call your Income.php directly in your navigator. It must echo the response.
It looks like button click function $('#result_submit').click(function() not triggering. I tried to put a alert box but not getting any output
do I need to add an onclick function along with button and change jquery ?
Found the issue. I mentioned complete path of Income.php and its working as expected. I saw some thread on how to use url on wordpress site. thanks for you r help
0

First off all add on the beginig of page:

error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set('error_reporting', E_ALL);

and printf("Errormessage: %s\n", mysqli_error($income_sum));

afther query to db. Do you have any respons from the page called by ajax?

Comments

-1

The script that you call using AJAX doesn't render anything.

echo $TotIncome;

2 Comments

its already given in comment so why you should use as answer.if you have different things than write here
SO is a question/answer website. A comment is not an answer, it is useful if you need aditionnal infos to solve the problem. If you know the solution, just post an answer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.