0

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.

2

1 Answer 1

1

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;
        });
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Looks good man! What about session data, say if I send the request, in the php, can I refer to session data of the user who submitted it?
Not sure if i understood you correctly, but in the script which handles your ajax request (yourscript.php in the example) you can call session_start() which as allows you to access the correspondig session data.

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.