0

I need to pass the form id to javascript, but always shows the same data.

<script>
$(document).ready(function() {
    $('input[name="submit"]').on('click', function(){
        var vauid = $( "input[name='uid']"     ).val(); alert("\n ID:  "+vauid);
    return false;
    }); 
});
</script>                                               
<? include "../includes/config.php";
$sql = "select * from table";
$reg = mysql_query($sql);
while($row = mysql_fetch_array($reg)){ ?>
<form action="" method="POST">
        <input type="text"   name="uid"     value="<?  echo $row['uid']?>" />
        <input type="submit" name="submit" value="Show"/>
</form> 
<? }?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
BMF
  • 1
  • 1
  • 1
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 10 '15 at 14:22
  • not too sure but you can try this `var vauid = $( "input[name=uid]").val();` .. i removed the single quotes around `uid` –  Jul 10 '15 at 14:26
  • That shouldn't matter @adelowo – Jay Blanchard Jul 10 '15 at 14:32
  • @JayBlanchard oops,did not know that –  Jul 10 '15 at 14:33

1 Answers1

0

Part of the problem is that you're inserting numerous forms with elements having the same names - that is a bad idea as names are intended to be unique.

Second, when you click you have to reference the currently clicked info:

$(document).ready(function() {
    $('input[name="submit"]').on('click', function(e){
        e.preventDefault();
        var vauid = $(this).prev("input[name='uid']").val(); 
        console.log("ID:  "+vauid);
    }); 
});

Here is a working demo.

$(this) will be the clicked button from which we look for the previous input's value. I'm also using preventDefault() to stop the button's default action, rather than returning false.

In addition you should quit using alert() for troubleshooting., use console.log() instead.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • names are not unique in a radio group. also an input in one form can have the same name as an input in another form. – dewd Jul 10 '15 at 14:40
  • You're absolutely right @dewd - I didn't say it was illegal, just a bad idea for single input elements. Radio groups have to be named the same in order to make them a group. – Jay Blanchard Jul 10 '15 at 14:42
  • Thanks Jay. But this method does not work, because I have to load a list of users of a database. – BMF Jul 10 '15 at 15:14
  • That is information that we weren't working with before @BMF - why doesn't this work? Loading the forms as you are doing should work just fine. – Jay Blanchard Jul 10 '15 at 15:15
  • Jay, In the demo you have 2 form, but I need to load the list of users in the same form with a while pass the information of the selected user – BMF Jul 10 '15 at 15:49
  • If you can update what your form would *actually* look like I can modify my answer to help you out. I based my demo off of your original form and question @BMF – Jay Blanchard Jul 10 '15 at 15:51
  • 1
    Jay again thanks your code works fine, my mistake is type first the script and then the form – BMF Jul 10 '15 at 16:08