1

I want a code for get textbox value when submit button is clicked. It must be Ajax.Here is the code I tried so far.But I culdent get to work....

<form action="" method="post">
<p>Route No :
<input type="text" name="route_no" required="required" />
<input type="submit" value="Search" name="search" />
</form>

Ajax Code

<script type="text/javascript">
$(document).ready(function() {
$("#sub").click(function() {
var textboxvalue = $('name or id of textfield').val();
$.ajax({
    type: "POST",
    url: 'ajaxPage.php',
    data: {txt1: textboxvalue},
    success: function(result) {
        $("div").html(result);
    }
});
});
});​
</script>

PHP code

$txt = null;
if((isset($_POST)) && (isset($_POST['txt1'])))
{
echo $txt = $_POST['txt1'];
}

3 Answers 3

7

HTML:

<label for="route_no">Route No:</label><input type="text" id="route_no" name="route_no" required="required" />
<input type="button" value="Search" id="search" />
<div id="result"></div>

JavaScript:

$(document).ready(function()
{
    $("#search").click(function()
    {
        var textboxvalue = $('input[name="route_no"]').val();

        $.ajax(
        {
            type: "POST",
            url: 'ajaxPage.php',
            data: {txt1: textboxvalue},
            success: function(result)
            {
                $("#result").html(result);
            }
        });
    });
});​

ajaxPage.php:

if(isset($_POST) && isset($_POST['txt1']))
{
    echo $_POST['txt1'];
}
Sign up to request clarification or add additional context in comments.

7 Comments

nope still not working...I dnt know what is the worng with code..it seemsokay to me
What part isn't working? Do you have a console to look at like Firebug or Chrome Developer's Tools? Is the page request going out or is it returning something you should be looking at with an ajax error function.
ah I dnt use Developer's Tools? Im just working on wamp server
What browser are you testing in?
Press F12 and look at the Console tab for any errors. Then look at the Network tab to see the response for your ajaxPage.php request.
|
2

You have problem here

$("#sub").click(function() {

you are using id ="sub" for submit button but you are not assigning id to that button so give id to submit button as id="sub".

1 Comment

update your code here whatever is changed. I will help you out to fix it.
2

To get the value of the textbox you can use:

$('#elementid').val()

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.