Sven van den Boogaart
Sven van den Boogaart

Reputation: 12327

Post form in while loop with javascript submit

I have a while loop wich creates forms like:

$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid  ORDER BY                   AfbeeldingPrior DESC");

while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3" action="kamer.php">

<input type="hidden" name="id" value="<?php echo$kamerid;?>">

<input type="hidden" name="afbeeldingplus" value="12345">

</form>
<?php
}

I want to post these forms with a text link. Normally I use

<script>
function f3_submit()
{
document.form3.submit();
}
 </script>

and then I put echo "<a href=\"##\" onClick=\"f3_submit();\" >"; under the form but because I got a lot of forms with the same name this wont work. it doesn't post anything !

How can I post these forms with a text link, so without a submit button.

Upvotes: 0

Views: 4927

Answers (5)

leafnode
leafnode

Reputation: 1380

If I understand correctly, you want to have many different forms, and many links submitting different forms? If that's true, you have to name the forms differently, e.g. using the ID you get from DB, and then use this generated name in submit links.

Like:

<?php
$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid  ORDER BY                   AfbeeldingPrior DESC");

while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3_<?php echo $kamerid;?>" action="kamer.php">
    <input type="hidden" name="id" value="<?php echo $kamerid;?>">
    <input type="hidden" name="afbeeldingplus" value="12345">
</form>
<a href="#" onClick="document.form3_<?php echo $kamerid;?>.submit(); return false;"><?php echo $kamerid;?></a>
<?php

}

Upvotes: 0

mplungjan
mplungjan

Reputation: 177955

Remember to return false!

<script>
function submitForm(kamerid) {
  document.forms["form"+kamerid].submit();
  return false;
}
 </script>

and have

echo '<a href="#" onClick="return submitForm(\''.$kamerid.'\');" >';

Upvotes: 1

VeXii
VeXii

Reputation: 3109

while($row3 = mysql_fetch_array($result3))
{
<form method=POST name="form3" action="kamer.php">

<input type="hidden" name="id" value="<?php echo$kamerid;?>">

<input type="hidden" name="afbeeldingplus" value="12345">
<button type="submit">Text to Submit here</buttton>
</form>
}

and then just style the button to look like text

Upvotes: 0

Jano
Jano

Reputation: 181

Maybe loop through the values and generate two comma (or pipe or whatever suits the task) separated strings and then create just one form and assign "id" and "afbeeldingplus" fields with these values. Your javascript submission will work fine and you have less HTML that having all these fields repeated either in one single form or across multiple forms.

Upvotes: 0

MattDiamant
MattDiamant

Reputation: 8771

I'm not sure that you're creating these forms in the way that you'd like to. But if you're happy with the way they are, then you can add in a submit button, and then just style it to look like a text link with css:

form input[type="submit"]{

    background: none;
    border: none;
    color: blue;
    text-decoration: underline;
    cursor: pointer;
}

Upvotes: 0

Related Questions