Semger
Semger

Reputation: 263

Submit data to PHP, and get the result

I would like to submit some data. Weather this be using a form, or onClick execute some AJAX, I'm not sure.

For example, I have this input

<input id='inpamount' type="text" name="amount" value="2.00" onkeyup="pad();validatemin();product()">

Now say if I wanted to send this data to a PHP file, using post ( i could just add a form). But then, without reloading the page (I can do this), how could I fetch a response (using AJAX).

Essentially I would like a user to be able to press a button, then to submit the inputs to a php file, and then get the output and assign it to a variable (I know how to do this, I just want to be able to get the data.

The php execute takes a few centiseconds because it comunicates with SQL. (If this matters).

I have considered using invisible forms but it didn't seem to work

If anyone could point me in the right direction, that would be great.

Upvotes: 0

Views: 44

Answers (1)

eol
eol

Reputation: 24565

Something like this?

$(document).ready(function(e) {

       $('#inpamount').click(function() {

            var data = 'myValue=' + $(this).val();

            $.ajax({
                url: "yourscript.php",
                type: "POST",
                data: data,
                cache: false,
                success: function(scriptOutput) {
                   //handle the result
                    }
                }
            });
            return false;
        });
    });

Upvotes: 1

Related Questions