1

I have the following code:

  <form action="" method="get" onsubmit="doRequest($('word').value); $('word').value=''; return false;">
    <input type="text" name="word" id="word" value="" />
    <input type="submit" name="submit" value="Send" />
  </form>

doRequest() function:

function doRequest(request)
{
$.ajax(url, {
type: 'get',
data: { 'msg' : request }
});
}

The problem is, if I change the word value manually like value="111", I can see the value is being posted to PHP. However, when I want it to post whatever I write into textarea, it posts nothing, so the problem lies in the onSubmit area.

Can anybody help me about this?

2 Answers 2

1

You are missing the # in your jQuery selectors.

onsubmit="doRequest($('#word').value); $('#word').value=''; return false;"

I would also remove the inline JavaScript and replace it with a function in the submit handler instead. Also using jQuery .val() instead of .value.

$(document).ready(function() {
    $('form').submit(function() {
         doRequest($('#word').val());
         $('#word').val('');
         return false;
    });
});
Sign up to request clarification or add additional context in comments.

6 Comments

I can't enter the link on submit handler, guess the server is down. I can move this to a function, but how can I execute the function then? Under document ready function?
@Craig, I initially assumed the OP had a working function with bad selectors. I updated it => it's .val(), not .value
@AnılÜnal, yes, it would simply be inside your document.ready function.
@Sparky672 fair enough...done the same answering a question before to, cheers.
It works perfectly, thank you. Do "$('form').submit(function()" is necessary under document ready function?
|
1

The correct way to access the value via jQuery is val() - not by setting a property.

$(...).val("set the value");
var get_the_value = $(...).val();

Also, your current selectors are looking for <word> elements which you hopefully don't have. Use #word as the selectors to search by ID.

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.