Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-21-2012, 04:13 PM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
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.
kevinkhan is offline   Reply With Quote
Old 06-21-2012, 05:11 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Old 06-21-2012, 05:40 PM   PM User | #3
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
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();
}); 
kevinkhan is offline   Reply With Quote
Old 06-21-2012, 05:50 PM   PM User | #4
sayannayas
New Coder

 
Join Date: May 2012
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
sayannayas is an unknown quantity at this point
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
sayannayas is offline   Reply With Quote
Old 06-21-2012, 05:51 PM   PM User | #5
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Old 06-21-2012, 05:58 PM   PM User | #6
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
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
}
kevinkhan is offline   Reply With Quote
Old 06-21-2012, 06:25 PM   PM User | #7
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
oops I thought I
was wrong but it
was a mistake ?

Last edited by DaveyErwin; 06-21-2012 at 06:33 PM..
DaveyErwin is offline   Reply With Quote
Old 06-21-2012, 06:40 PM   PM User | #8
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
oops I thought I
was wrong but it
was a mistake ?
so what should be the code?
kevinkhan is offline   Reply With Quote
Old 06-21-2012, 07:06 PM   PM User | #9
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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; ?>"
DaveyErwin is offline   Reply With Quote
Old 06-21-2012, 07:08 PM   PM User | #10
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
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
kevinkhan is offline   Reply With Quote
Old 06-21-2012, 07:57 PM   PM User | #11
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.