-1

Im trying to send Ajax post to PHP.

This code works Ok:

$(document).on('keyup change',function(){
        $.post("suma.php",{name: "oscar"},
            function(data){
                $('#Resultado').html(data);
            }

            );

    });

PHP:

<?php
if( $_REQUEST["name"] )
{
   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
}
?>

But the problem is, How can I send the value of two input class like this and calculate the sum in PHP?

 <tr>
    <td><input value="0" class="sum1"/></td>
    <td><input value="0" class="sum2"/></td>
</tr>

Thank You in advance.

Funereal
  • 653
  • 2
  • 11
  • 32
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – John Dvorak Apr 24 '14 at 19:47
  • @JanDvorak but the problem that I dont understand is how can I send class "sum1" and "sum2" correctly. – Funereal Apr 24 '14 at 19:51
  • find them, then collect them into an object, then send that? If you are stuck with the last part, check the documentation on `$.ajax` – John Dvorak Apr 24 '14 at 19:52

3 Answers3

1

This is an example for you:

<div>
  <a href="#" id="text-id">Send text</a>
  <input id="source1" name="source1" >
  <input id="source2" name="source2" >
</div>

You need to include success handler in your ajax:

$("#text-id").on( 'click', function () {
        $.ajax({
            type: 'post',
            url: 'text.php',
            data: {
                source1: "some text",
                source2: "some text 2"
            },
            success: function( data ) {
                console.log( data );
            }
        });
    });
0

Firstly you will need to give distinguished names/IDs to the two input fields, then in JS you can find the value of those two like $("#input_id").val(), then post them in ajax data attribute like posted by Jeffery. Secondly, you will be able to fetch all the posted data in PHP via $_POST variable, then you can do whatever you want with the data return a response.

imcom
  • 61
  • 1
  • 6
0

You can send it here;

$.post("suma.php", {
       src1 ; $('.sum1').val(),
       src2 : $('.sum2').val()
   }

And your php:

<?php
    if( $_REQUEST["src1"] && $_REQUEST["src2"] ){
    $sum = $_REQUEST["src1"] + $_REQUEST["src2"]; 
    echo $sum;
 }
?>
Jai
  • 74,255
  • 12
  • 74
  • 103