0

I have a form like this:

one.php

<form id='myFormId' name='myFormName'>
  <input type='text' name='myTextField'>
  <a href='two.php'>Show Value</a>
</form>

Question:

I want to pass 'myTextField' textbox value to two.php and want to echo it on screen. I can't use submit button and also can't submit the form. Is there any trick ?

Thanks

3 Answers 3

1

You could start by giving the anchor an unique id:

<a id="mylink" href="two.php">Show Value</a>

and then register for the click event handler and send an AJAX request:

$(function() {
    $('#mylink').click(function() {
        $.ajax({
            url: this.href,
            data: { myTextField: $('#myFormId input[name=myTextField]').val() },
            success: function(result) {
                // TODO: use the resulting value
                alert(result);
            }
        });
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

When I use $('a').click(function() {.....}); then it alert but when I use $('#mylink').click(function() {.....}); then it does not alert the result. I have added this id in link also. Can you understand this problem ? Thanks
OK. When I used this jQuery('#mylink').live('click',function() {...}); then it is alerting.
0

one.php

<form id='myFormId' name='myFormName'>
  <input type='text' name='myTextField'>
  <a href='two.php?value=myTextField'>Show Value</a>
</form>

two.php

echo $_GET['value'];

1 Comment

I want to pass textbox value not its name.
0
<form id='myFormId' name='myFormName'>
  <input type='text' name='myTextField'>
  <a href='two.php' id='myLink'>Show Value</a>
</form>


jQuery('#myLink').live('click',function() {
    $.ajax({
        url: this.href,
        type: 'POST',
        data: $('#myFormId').serialize(),
        success: function( data ) {
            // TODO: use the resulting value
            alert(data);
        }
    });
    return false;
});

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.