使用超链接通过中间函数提交表单而不是按钮是不起作用的

使用超链接通过中间函数提交表单而不是按钮是不起作用的

问题描述:

I want to submit a form through an 'edit(param1,param2)' function which in turn is being called in either of the two ways..

echo '<input type="button" value="DELETE" onclick="edit(\''.$key.'\',\''.$b.'\')"/>';

or

echo '<a href="list_cadmin.php" onclick="edit(\''.$key.'\',\''.$b.'\')"><span  class="bluetext">DEACTIVATE</span>';

the function edit() is something like this:

function edit(a,b) {
var answer = confirm("Do You Really want to Deactivate ?")
if (answer){
    alert(a)
document.getElementById('cid').value= a;
document.getElementById('key').value= b;
document.getElementById('fname').method='get';
document.getElementById('fname').action='samepage.php';
document.getElementById('fname').submit();
}
}

where $key and $b are number and string values respectively.

so, according to the above both should go to 'samepage.php?cid=BLAHBLAH&key=1234' on onClick. But only the input=button is working. Hyperlink is reloading without the GET parameters. How do i get the hyperlink to work?

You need to prevent the href from executing by returning false from the onclick:

echo '<a href="list_cadmin.php" onclick="edit(\''.$key.'\',\''.$b.'\'); return false;"><span  class="bluetext">DEACTIVATE</span>';

Try to use

echo '<a href="javascript:void(0);" onclick="edit(\''.$key.'\',\''.$b.'\')"><span  class="bluetext">DEACTIVATE</span></a>';

<?php
echo <<<EOD
<a href="javascript:void(0);" onclick="edit('{$key}','{$b}')">
<span class="bluetext">DEACTIVATE</span></a>
EOD;