/    Sign up×
Community /Pin to ProfileBookmark

Form with JavaScript onclick submit and also textbox

I’m trying to make a form work with both JavaScript onclick submit and also textbox. The PHP is only to show the results.
– Either the <form> line is put above the form inputs, and the textbox works (and the radio button clicks and submits) but the radiobutton’s value for formVar doesn’t post.
– Or the <form> line is put below all the form inputs except above the hidden inputs, and the radiobutton clicks and submits, the radiobutton’s value for formVar posts successfully, but the textbox doesn’t work at all.

How can I get both the radiobutton onclick values to post when clicked, and the textbox to work together? Thanks. Alex

Here’s the code:

<? echo “answertxt: “.$_POST[‘answertxt’].” formVar: “.$_POST[‘formVar’]; ?><br>

<form action=”test.php” method=”post” name=”myform” id=”myform”><!– if this <form> line goes here, then the textbox submits but the radio buttons’ onclick doesn’t submit the formVar value. if this <form> line goes below all the inputs, then the textbox doesn’t submit but the radio buttons onclick do submit the formVar value. –>

<input type=”radio” name=”formVar” value=”1″ onclick=”document.myform.formVar.value=’1′; document.myform.submit();”>
<input type=”text” name=”answertxt” value=”<? echo $_POST[‘answertxt’]; ?>”>
<input type=”radio” name=”formVar” value=”2″ onclick=”document.myform.formVar.value=’2′; document.myform.submit();”>

<input type=”hidden” name=”formVar” value=””>

</form>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@J2NYCAug 27.2009 — Now that I see the whole thing I think I know what the issue is. (This is the person from Craig's list).

It's not the onclick. It's the hidden input field with the same name as the radio button. That field's value (null) is overwriting the value in the radio button with the same name.

That's the reason that the radio button value isn't passed when you have the form structured properly. Try renaming that and see what happens.
Copy linkTweet thisAlerts:
@alexlinskerauthorAug 28.2009 — Thanks so much. Now it works.
×

Success!

Help @alexlinsker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...