Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    how to get value of a hidden field in js

    my form look like this

    PHP Code:
    <?php
    while($row mysql_fetch_array($result)) {
                
    $id $row['id'];

    ?>
                
            <tr>    
                <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="eid" value="<?php
                                            
    echo $id?>"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>
               
            }
    echo "<tr>";
            

    ?>


    </table>
    so for every row there is a different id;

    i copied this piece of code from the internet

    PHP Code:
    var id = $("#eid").val(); 
    but this only get the id of the first button.
    I want it to ge the id of the button that is pressed.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by kevinkhan View Post
    my form look like this

    PHP Code:
    <?php
    while($row mysql_fetch_array($result)) {
                
    $id $row['id'];

    ?>
                
            <tr>    
                <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="eid" value="<?php
                                            
    echo $id?>"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>
               
            }
    echo "<tr>";
            

    ?>


    </table>
    so for every row there is a different id;

    i copied this piece of code from the internet

    PHP Code:
    var id = $("#eid").val(); 
    but this only get the id of the first button.
    I want it to ge the id of the button that is pressed.
    the buttons are not assigned an id
    in the code presented also
    all the hidden fields have the
    same id ("eid") also the code
    presented is not valid php

    your code should look more
    like this ...

    PHP Code:
    <?php
    while($row mysql_fetch_array($result)) {
                
    $id $row['id']; 
    echo <<<HTML
                
            <tr>    
                <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="$id" value="$row"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>   


    HTML;
    }
    ?>
    Last edited by DaveyErwin; 06-21-2012 at 05:25 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    ok i have changed the code to this

    PHP Code:
     <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="<?php echo $id?>" value="<?php echo $id?>"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>
    how do i get the value of the submit button i clicked

    PHP Code:
    $(function() {

          $(
    ".button").click(function() {
            
            var 
    id = $("#eid").val();                          
            
            
    alert (id);
            
            var 
    dataString 'id='id;
            
                $.
    ajax({
                  
    type"POST",
                  
    url"process.php",
                  
    datadataString,
                  
    success: function() {
                      
    alert (dataString);
                  }
                });
                                      
        return 
    false;
        });
    });
    runOnLoad(function(){
      $(
    "input#name").select().focus();
    }); 

    I know this line need to be change var id = $("#eid").val();
    but im not sure what to change it two.

    also what does this code mean

    PHP Code:
    runOnLoad(function(){
      $(
    "input#name").select().focus();
    }); 

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post
    you can have id to your input feilds like

    Code:
      <input type="submit"  id="<?php echo $id; ?>_btn" class="button" name="action" value="Viewed"/>
    and your jquery will look like
    Code:
      jQuery('input.1_btn').bind('click', function() {  
            dosomthing
          
                   });  
    
      jQuery('input.2_btn').bind('click', function() {  
            dosomthing
          
                   });

    Look this example

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by kevinkhan View Post

    ok i have changed the code to this

    PHP Code:
     <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="<?php echo $id?>" value="<?php echo $id?>"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>
    from what I see here the value
    <?php echo $id; ?>
    never changes so all
    hidden fields have the
    same id.

    Quote Originally Posted by kevinkhan View Post
    how do i get the value of the submit button i clicked

    PHP Code:
    $(function() {

          $(
    ".button").click(function() {
            
            var 
    id = $("#eid").val();                          
            
            
    alert (id);
            
            var 
    dataString 'id='id;
            
                $.
    ajax({
                  
    type"POST",
                  
    url"process.php",
                  
    datadataString,
                  
    success: function() {
                      
    alert (dataString);
                  }
                });
                                      
        return 
    false;
        });
    });
    runOnLoad(function(){
      $(
    "input#name").select().focus();
    }); 

    I know this line need to be change var id = $("#eid").val();
    but im not sure what to change it two.

    also what does this code mean

    PHP Code:
    runOnLoad(function(){
      $(
    "input#name").select().focus();
    }); 
    "how do i get the value of the submit button i clicked"...

    <input type="submit" class="button" name="action" value="Viewed"
    onclick="alert(form.elements[1].value)"/>
    Last edited by DaveyErwin; 06-21-2012 at 07:18 PM.

  • #6
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sayannayas View Post
    you can have id to your input feilds like

    Code:
      <input type="submit"  id="<?php echo $id; ?>_btn" class="button" name="action" value="Viewed"/>
    and your jquery will look like
    Code:
      jQuery('input.1_btn').bind('click', function() {  
            dosomthing
          
                   });  
    
      jQuery('input.2_btn').bind('click', function() {  
            dosomthing
          
                   });

    Look this example
    I dont think this is what i wanted as the id could be any number.

    so basically from a code like this i want to get the id of the submit button i click on

    var id = $(" id of button that was clicked ").val();

    here is the form i have which is in a loop

    PHP Code:
    <?php
    $sql 
    "SELECT * FROM events WHERE `show` =  'yes' AND date > now() ORDER BY date ASC";
            
    $result mysql_query($sql); 
                    
            while(
    $row mysql_fetch_array($result)) {
                
    $id $row['id'];


    ?>
                <td>
                  <form name="contact" method="post" action="">
                                <div>
                                    <input type="hidden" id="<?php echo $id?>" value="<?php echo $id?>"/>
                                    <input type="submit" class="button" name="action" value="Viewed"/>
                                </div>
                            </form>
                </td>
                
    <?php
    }

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    oops I thought I
    was wrong but it
    was a mistake ?
    Last edited by DaveyErwin; 06-21-2012 at 06:33 PM.

  • #8
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    oops I thought I
    was wrong but it
    was a mistake ?
    so what should be the code?

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by kevinkhan View Post
    so what should be the code?
    Oh well I like what you posted
    except we don't know if
    $id is a string starting
    with a number so this would be better ...
    id="btn_<?php echo $id; ?>"

  • #10
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DaveyErwin View Post
    Oh well I like what you posted
    except we don't know if
    $id is a string starting
    with a number so this would be better ...
    id="btn_<?php echo $id; ?>"
    id is an int. how do i grab this in js

    Thanks for your help

  • #11
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by kevinkhan View Post
    id is an int. how do i grab this in js

    Thanks for your help
    <form>
    <input>
    <input type="submit" class="button" name="action" value="Viewed"
    onclick='var a="<?php echo $id; ?>";alert(a)' />
    </form>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •