0

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.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

5 Answers5

1

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.'\');" >';
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

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;
}
MattDiamant
  • 8,561
  • 4
  • 37
  • 46
0

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

}

leafnode
  • 1,382
  • 7
  • 15
  • There is still no return false - on some browsers that might create a situation where the browser tried to reload while submitting. I believe my solution is more elegant - it can be adapted as shown in the update – mplungjan Feb 27 '13 at 12:17
  • While using '#' should prevent from any reload, I've fixed the code to mitigate any possibilities of page reload. – leafnode Feb 27 '13 at 12:34
  • 1
    That is not entirely correct. The javascript protocol will return any result of the call unless you wrap it in void() - also the link will give errors if javascript is turned off, and show a scary link in the status bar. – mplungjan Feb 27 '13 at 15:45
  • Both statements are true. But about the first one, the code is not required to return any value here. – leafnode Feb 27 '13 at 17:05
  • You misunderstood. If the statement after javascript: returns anything, the page will be replaced by that anything. That might clobber the submission, hence it is never a good idea to use statements with side effects that may break your code. Use onclick and return false. Never use the javascript protocol for anything other than creating bookmarklets – mplungjan Feb 28 '13 at 06:41
  • 1
    Oh, I didn't know that, but still if I didn't return anything, it'd work. Yet, I fixed the code. – leafnode Feb 28 '13 at 06:47
0

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.

Jano
  • 161
  • 1
  • 8
0
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

VeXii
  • 3,079
  • 1
  • 19
  • 25