0

I'm trying to catch the correct variable in this jQuery function but whatever the button I click, the alert always shows the name of the first label that I have. Any ideas? Thank you.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
    $(".submit").click(function() {
    var name = $("#name").val();
    alert(name);
    });
    });
</script>
</head>

<body>
<?php
include("connection.php"); 
$link=connect(); 
$result=mysql_query("select * from names",$link);

if (mysql_fetch_array($result) == 0)
{
    echo "No names registered. Add one";
}
else
{
    while($row=mysql_fetch_array($result))
    {
        echo "<html>
        <label>".$row["name"]."</label>
        <input type='button' class='submit' value='Delete'>
        <input type='hidden' id='name' value=".$row["name"].">
        <br>
        </html>";     
    }

    mysql_free_result($result);
    mysql_close($link);
}
?>  
</body>
</html>
2
  • 1
    id's need to be unique Commented Feb 17, 2014 at 21:53
  • @cmorrisey yes, I want to delete items with AJAX but at the moment I only need to catch the correct variable in each case. Commented Feb 17, 2014 at 22:31

4 Answers 4

2

Use DOM navigation to get the value from the adjacent input:

$( document ).ready(function() {
    $(".submit").click(function() {
        var name = $(this).next().val();
        alert(name);
    });
});

Also, there should just be one <html> block in the page. Don't put that in the loop.

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

Comments

0

At first, html ID should be unique.

Try using html5 data attributes and remove your hidden form element:

while($row=mysql_fetch_array($result))
{
    echo "<input type='button' class='submit' value='Delete' data-value=".$row["name"].">";     
}

And JS part:

<script type="text/javascript">
    $( document ).ready(function() {
       $(".submit").click(function() {         
           alert($(this).data('value'));
       });
    });

Comments

0

You are re-using the same id - name - multiple times:

while($row=mysql_fetch_array($result))
{
    echo "<html>
    <label>".$row["name"]."</label>
    <input type='button' class='submit' value='Delete'>
    <input type='hidden' id='name' value=".$row["name"].">
    <br>
    </html>";     
}

You should wrap your fields in individual forms (not html elements) and use the name attribute:

while($row=mysql_fetch_array($result))
{
    echo "<form action='' method='post'>
    <label>".$row["name"]."</label>
    <input type='button' class='submit' value='Delete'>
    <input type='hidden' name='name' value=".$row["name"].">
    <br>
    </form>";     
}

This will make your form also work without javascript.

Then, in your javascript you can do:

$(".submit").click(function(e) {
  e.preventDefault();
  var name = $(this).closest('form').find('input[name="name"]').val();
  ...

Or if you need to post it using ajax afterwards:

var data = $(this).closest('form').serialize();

(or you catch the submit event of the form instead)

Comments

0

In addition to some of the other answers, you don't really need the hidden input. Could just change the button input to:

<input type='button' class='submit' name='".$row["name"]."' value='Delete'>

Then change your JQuery selector to:

var name = $(this).attr('name');

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.