PDA

View Full Version : Click button, change form, submit:


Horus_Kol
11-04-2003, 06:54 AM
Can it be done?

look at this code:


<?php
if (isset($_POST['test_btn']))
echo $_POST['test_btn'];
?>

<head>
<script type='text/javascript'>
function sub(btn)
{
test_div.innerHTML = "<input type='hidden' name='test_btn' value='7'>";

forms[0].submit();
}
</script>
</head>

<body>
<form action='test.php' method='post'>
<div id='test_div'></div>

<button type='button' name='btn1' onClick='sub(1)'>Button 1</button>
<button type='button' name='btn2' onClick='sub(2)'>Button 2</button>
</form>
</body>


I'm getting a Javascript error, but not sure why.

any Client Sider's out there with an idea?

Cheers,
HK

Willy Duitt
11-04-2003, 09:29 AM
forms is undefined.

Try this:

<script type='text/javascript'>
function sub(btn)
{
test_div.innerHTML = "<input type='hidden' name='test_btn' value='7'>";

document.forms[0].submit();
}
</script>


.....Willy

Horus_Kol
11-04-2003, 09:56 AM
thanks Willy - worked a treat.

Vincent Puglia
11-05-2003, 12:17 PM
Hi egyptian god,

I hope you are only concerned with IE, else this

test_div.innerHTML = "<input type='hidden' name='test_btn' value='7'>";

is going to give you problems. It should be:

document.getElementById('test_div').innerHTML = "<input type='hidden' name='test_btn' value='7'>";

and that will work only for version 5+ browsers -- since IE4 and NN4 do not recognize document.getElementById

Vinny

Horus_Kol
11-06-2003, 04:40 AM
thanks for the input Vincent.

As it happens, I am only interested in using IE5.5 or higher, because that is what we specify to our customers for our application (this is not a full internet app - only intranet).