...

View Full Version : how to get value of a hidden field in js



kevinkhan
06-21-2012, 04:13 PM
my form look like this



<?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


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.

DaveyErwin
06-21-2012, 05:11 PM
my form look like this



<?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


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
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;
}
?>

kevinkhan
06-21-2012, 05:40 PM
ok i have changed the code to this


<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


$(function() {

$(".button").click(function() {

var id = $("#eid").val();

alert (id);

var dataString = 'id='+ id;

$.ajax({
type: "POST",
url: "process.php",
data: dataString,
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


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

sayannayas
06-21-2012, 05:50 PM
you can have id to your input feilds like


<input type="submit" id="<?php echo $id; ?>_btn" class="button" name="action" value="Viewed"/>

and your jquery will look like


jQuery('input.1_btn').bind('click', function() {
dosomthing

});

jQuery('input.2_btn').bind('click', function() {
dosomthing

});


Look this example (http://fundapass.blogspot.in/2012/05/hello-syntaxhighlighter-function.html#more)

DaveyErwin
06-21-2012, 05:51 PM
ok i have changed the code to this


<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.



how do i get the value of the submit button i clicked


$(function() {

$(".button").click(function() {

var id = $("#eid").val();

alert (id);

var dataString = 'id='+ id;

$.ajax({
type: "POST",
url: "process.php",
data: dataString,
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


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)"/>

kevinkhan
06-21-2012, 05:58 PM
you can have id to your input feilds like


<input type="submit" id="<?php echo $id; ?>_btn" class="button" name="action" value="Viewed"/>

and your jquery will look like


jQuery('input.1_btn').bind('click', function() {
dosomthing

});

jQuery('input.2_btn').bind('click', function() {
dosomthing

});


Look this example (http://fundapass.blogspot.in/2012/05/hello-syntaxhighlighter-function.html#more)

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
$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
}

DaveyErwin
06-21-2012, 06:25 PM
oops I thought I
was wrong but it
was a mistake ?

kevinkhan
06-21-2012, 06:40 PM
oops I thought I
was wrong but it
was a mistake ?

so what should be the code?

DaveyErwin
06-21-2012, 07:06 PM
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; ?>"

kevinkhan
06-21-2012, 07:08 PM
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

DaveyErwin
06-21-2012, 07:57 PM
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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum