0

On my ASP.Net/C# page, I have textboxes to enter in data for a transaction. I am using a payment API to accept credit card transactions. It comes with some sample code in jQuery, which I am editing (see below). The section of my code that says "data" is where I need help. I want to get the text from each asp.net textbox. My current idea below, such as $(TxtAction).val() does not actually pull the textbox value as I want. So I need some help on how I can get the text from the asp.net textboxes. I'd actually prefer to get the values in C# and then somehow send those values to my jQuery code or even just have everything in C# instead of jQuery. Any advice/help is appreciated.

$(document).ready(function () {

    'Send a Credit Card Transaction'
    var settings = {
        "url": "URLHERE",
        "method": "POST",
        "headers": {
            "X-Forte-Auth-Organization-Id": "IDHERE",
            "Authorization": "CODEHERE",
            "Content-Type": "application/json",
        },
        "data": "{\r\n    \"action\":" + $(TxtAction).val() + ",\r\n    \"authorization_amount\":" + $(TxtAuthorizationAmount).val() + ",\r\n   \"subtotal_amount\":" + $(TxtSubtotalAmount).val() + ",\r\n   \"billing_address\":{\r\n     \"first_name\":" + $(TxtFirstName).val() + ",\r\n     \"last_name\":" + $(TxtLastName).val() + ",\r\n  \"card\":{\r\n     \"card_type\":" + $(TxtCardType).val() + ",\r\n     \"name_on_card\":" + $(TxtCardName).val() + ",\r\n     \"account_number\":" + $(TxtCardNumber).val() + ",\r\n     \"expire_month\":" + $(TxtCardExpireMonth).val() + ",\r\n    \"expire_year\":" + $(TxtCardExpireYear).val() + ",\r\n     \"card_verification_value\":" + $(TxtCardVerificationCode).val() + ",\r\n  }\r\n}"
    }

    $.ajax(settings).done(function (response) {
        console.log(response);
    });

})
1
  • 1
    I'd actually prefer to get the values in C# . no no! You really don't want to do that! But I think you are just missing a #. $("#TxtAction").val() Commented Dec 3, 2019 at 20:05

3 Answers 3

1

You're passing the textbox names to the jQuery selector $(...) without quotes, so it's interpreting those as variable names instead of selectors. Instead, you need to use $('#TxtAction').val() and so on.

Where "TxtAction" is the id attribute of each textbox.

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

Comments

1

Two things. As Matt U said, you can fix your immediate problem by changing

$(TxtAction)

To:

$('#TxtAction').val() 

That being said, I think you are making this a lot harder for yourself than you need to. I wouldn't try and format your json in the JQuery. JQuery can do that for you!

"data": JSON.stringify({
    action: $('#TxtAction').val(),
    authorization_amount: $('#TxtSubtotalAmount').val(),
     ... and all the rest of your text box values ...

You can let jQuery do the messy work for you. JSON.stringify({...}) will format your JSON string for you.

(Note: I've not tested this)

Comments

0

try

$('[id$=TxtAction]').val()

instead of

$(TxtAction).val()

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

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.