0

I went through solutions provided to similar questions in Stack Overflow and other as well. (Seems like its not only me with this problem ). So I finally am posting the question with a hope to find some solution.

I am sending two numbers to PHP function via ajax call and expect to get sum from function. I think I am right not sure though, that half of the ajax is right and half not i.e. I can send values to PHP function (i think) but I can't get sum instead in success message I get "html data of the page" :D

Can you please point my mistake and viable solution.

Ajax:

$(document).ready(function(){
    $('#myForm').submit(function(event){
    
        var num1 = Number($('#num1').val());
        var num2 = Number($('#num2').val());
        var summ = num1+num2;
        //var data = $('#myForm').serialize();
        console.log(" nums are " + summ); //totally works
        var myNums = "num1= " + num1 + " & num2=" + num2; 
        alert(myNums);       //totally works
        event.preventDefault();
        $.ajax({
            url:'arrayTest.php',
            type: 'POST',
            data: {num1 : num1,
                    num2 : num2 },
            success:function(data){
                console.log(" <br>success yr data is " + myNums);
                var results = data;
                alert(data); // i get html of the entire page containing form
                return false;
            },
            error:function(error){
                console.log(" sorry error " + error); // get this when i add header in php 
            }
        });
    });

});

html :

<form id ="myForm" method="post" action="">  

  number1: <input type="number" name="num1" id="num1">
  <br><br>
  number2: <input type="number" name="num2" id="num2">
  <br><br>
   <br><br>
  <input type="submit" id ="submit" name="submit" value="add">  
</form>

php:

if(isset($_POST['submit'])){
            
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $result= $num1 + $num2;
    //echo "num1 is ".$num1;// **i didnt see any echo** 
    
    echo " your result ". $result; // **i didnt see any echo** 
    return ($result);
}

PS: html and ajax is in same page, php function in another; I see message

XHR finished loading: POST "http://localhost/phpTest/arrayTest.php"."

in console (I don't have idea what it means!)

4
  • 2
    If I'm not wrong, your check for $_POST['submit'] is the culprit here. The POST data you send along with your request doesn't have a key called 'submit' in it, so the PHP code never executes like you expect. Commented Oct 12, 2018 at 20:58
  • 2
    I strongly suggest you utilize the browser's Dev Tools. You can examine exactly what gets sent and received from an AJAX query - it takes a lot of the guesswork out. Commented Oct 12, 2018 at 21:05
  • @Connor You're not wrong, post that as an answer. Commented Oct 12, 2018 at 21:17
  • @Connor Thank you for your suggestion. Actually if i am not wrong you meant the i should have used $_POST['add'] or something? If yes, sorry to tell that it had not helped me actually .... Commented Oct 13, 2018 at 5:58

2 Answers 2

1

Your form define num1, num2 and add but you are using submit which is not define.

if(isset($_POST['add'])){

  $num1 = $_POST['num1'];
  $num2 = $_POST['num2'];
  $result= $num1 + $num2;
  //echo "num1 is ".$num1;// **i didnt see any echo** 

   echo " your result ". $result; // **i didnt see any echo** 
   return ($result);
}

TIPS: Always turn on error reporting and development Environment and use print_r() to inspect variable

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you friend. Turns out that i tried your solution beforehand i posted here ..I thought it could be one of reason so I had been trying to change 'POST' to 'add' and vice-versa.
UPDATE: use print_r($_POST) and notice sudmit/add was sent with the form
..i used it in php page and no error found (unless i used it before the function/if(isset...))
sorry for the typo i meant add/submit is'n part of the post data. Perhaps you should consider adding it with JavaScript or replace isset($_POST['add'])** with **isset($_POST)
Princewell Thanks a lott my friend -Your solution is perfect ..I now got print_r($num1+$num2) in php file as alert ...but still this is not flashing in ajax "data/result"
0

If your code works just disable XMLHttpRequests from chrome console.

  • open the console -> click the vertical "..." in the top right -> go to settings, and uncheck "Log XMLHttpRequests" under the "Console" header.

Comments

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.