user3022368
user3022368

Reputation: 1

sum two numbers using php and ajax

Here is my html code:

   <form name="sumForm" action="">
<input type="text" id="num1" name="num1" />

<input type="text" id="num2" name="num2" />

<input type="submit" id="sum"  value="sum"/>

</form>

here is my jquery code:

       $("#sum").click(function() {
                        var num1 = $("#num1").val();
                        var num2 = $("#num2").val();


                        var dataString = "num1="+ num1 + "&num2=" + num2; 

                        $.ajax({
                        type: "POST",
                        url: "apotelesma.php",
                        data: dataString,
                        success: function() {
                           alert(dataString)

                                return false;
                        }
                });
                return false;
        });

and here is apotelesma.php script:

<?php

$num1 = isset($_POST['num1']);
$num2 = isset($_POST['num2']);

$apotelesma = 0;

$apotelesma = $num1 + $num2;

echo $apotelesma;

?>

I can't figure out how to get the result. I don't know if is correct to include php script to display result.

Upvotes: 0

Views: 13965

Answers (6)

twinlakes
twinlakes

Reputation: 10228

'isset' returns a boolean representing whether the variable is set.

you need:

<?php

if (!isset($_POST['num1']) || !isset($_POST['num2']))
    exit;
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];

$apotelesma = 0;

$apotelesma = $num1 + $num2;

echo $apotelesma;

?>

then your success callback should include

$('#result').html(dataString);

When you make an ajax call, your page does not refresh like with a normal form, therefore you would not want to use an include.

Upvotes: 0

Expedito
Expedito

Reputation: 7795

You aren't sending the variables. Change AJAX to include the variables num1 and num2 in the data area:

$("#sum").click(function() {
    var num1 = $("#num1").val();
    var num2 = $("#num2").val();
    $.ajax({
        type: "POST",
        url: "apotelesma.php",
        data: {
            num1: num1,
            num2: num2
        },
        success: function(data) {
            alert(data);
            return false;
        }
    });
    return false;
});

And change your PHP to something like this:

$num1 = isset($_POST['num1']) ?  $_POST['num1'] : 0;
$num2 = isset($_POST['num2']) ?  $_POST['num2'] : 0;
echo $num1 + $num2;

Upvotes: 0

Mash
Mash

Reputation: 1339

Others pointed an error in your php file, but there is an error in your javascript [too]:

$.ajax({
type: "POST",
url: "apotelesma.php",
data: dataString,
success: function() {
   alert(dataString)

        return false;
}

should be:

$.ajax({
type: "POST",
url: "apotelesma.php",
data: dataString,
success: function(data) {
   alert(data)

        return false;
}

The data in success: function(data){} is what you are echoing in your php file.

Upvotes: 0

Bjoern
Bjoern

Reputation: 16304

isset() returns either true or false, and its is really tough to do any additions with true or false (sarcasm).

You need to change this to something like:

$num1 = isset($_POST['num1']) ?  $_POST['num1'] : 0;
$num2 = isset($_POST['num2']) ?  $_POST['num2'] : 0;

return $num1 + $num2;

My question: Is there any reason you do an addition over ajax? Why not in javascript without the ajax request?

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116110

isset checks if a value is set and returns a boolean, so $num1 and $num2 will be either true or false. So you are not adding up the actual values.

Upvotes: 2

The problem is that you are setting $num1 and $num2 to boolean..

Try this

$num1 = isset($_POST['num1']) ?  $_POST['num1'] : 0;
$num2 = isset($_POST['num2']) ?  $_POST['num2'] : 0;

Upvotes: 1

Related Questions