0

When my page opens I call a PHP file and output a form in the form of an echo.

A simplified example below:

    echo "<table id='results'>";
    echo "<form method = 'post'><input id='".$row['batchname']."'><button class='btn1'>Query this record</button></form>";
</table>

There will be many versions of the above form as table rows are pulled from the database.

I am usin AJAX to handle the output:

$(document).ready(function(){
    $(".btn1").click(function(e){
        e.preventDefault();
        var bname = ("#<?php echo $row['batchname'];?>").val();
        $post(
            "somephp.php",
            {name : bname},
            function(response, status){
                $("#results").replaceWith(response);
            }
        );
    });
});

When I input an non PHP ID into the jQuery the AJAX work but I always post the first returned row for every form produced as the ids output in the PHP are the same. Can I echo PHP variable into jQuery like this? Is there a better way of getting dynamic ID's into jQuery.

1 Answer 1

1

Rather than hacking about like this, do it properly.

$(".btn1").click(function(e) {
    e.preventDefault();
    var form = $(this).parents("form");
    var value = form.find("input")[0].value; // or something else here
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou that makes much more sense and works. I really need to get my head around where I can use use "this". Thanks again much better than my hacky code!!

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.