开发者

Radio button on form field always submitting as ON/TRUE

I have a fairly basic form set up with an input field for a single radio button.

There's a php script that processes the form and inserts the values into a mysql database. For some reason, every time I fill the form out, regardless of whether I have the radio selected or not, the result is always ON/TRUE.

here is my input field:

<input type="radio" name="news" id="news" class="radio" value="yes" />

Here is part of the PHP code that is supposed to process that:

if(isset($_POST['news']))
{
$news = "Yes";
}
else
{
$news = "No";
}

Can someone tell me what hte problem is from that code or would I need to post more?

here is the full form code:

<form method="POST" id="ipad" name="ipad" action="list.php">
<input type="text" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" value="First Name" id="fname" name="fname" class="inputtext">


<input type="text" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" value="Last Name" id="lname" name="lname" class="inputtext">

<input type="text" onfocus="if (this.value 开发者_如何学JAVA== 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" value="Email" id="email" name="email" class="inputtext">

<input type="text" onfocus="if (this.value == 'Code') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Code';}" value="Code" id="code" name="code" class="inputtext">


<input type="radio" value="" class="radio" id="news" name="news">
<div class="radiotext">sign up?</div>

  </form>

Notice that the radio button input has no javascript being applied to it. I've tried filling in the value="" with YES or NO or leaving it blank.


You need to test for the radio button's value. The variable will always be present, so isset() is bound to always return true.

if(!empty($_POST['news']) and ($_POST['news'] == 'yes'))


if (isset($_POST['news']) && $_POST['news'] == 'yes'){
    $news = "Yes";
}
else
{
   $news = "No";
}


about that jquery code, javascript doesn't need to be inside html to work. If I were you, I would search through my code for .attr("checked","checked") or .prop("checked");


You need to workaround it like so...

You need to add a hidden input to your form and when ever you click on one of the radio buttons, you call a function which will change the value of the hidden input by its id.. and here it is step by step :

  1. Add a hidden input to your form and give it a name and an id :

    <input type="hidden" id="IsFormer" name="IsFormerValue" value="">

  2. Add on click attribute for both radio buttons :

    <input type="radio" id="Former" name="IsFormer" onclick="ToggleIsFormer('Yes')">

    <input type="radio" id="NotFormer" name="IsFormer" onclick="ToggleIsFormer('No')">

  3. Add the function in Javascript :

    function ToggleIsFormer (Value) { document.getElementById('IsFormer').value = Value; }

  4. Finally, in PHP you check the value with the name of the hidden input and not the name of the radio buttons.

Here is the above steps on your code (try doing it your self first ;) ) :


<form method="POST" id="ipad" name="ipad" action="list.php">
    <input type="text" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First Name';}" value="First Name" id="fname" name="fname" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Last Name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last Name';}" value="Last Name" id="lname" name="lname" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Email') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Email';}" value="Email" id="email" name="email" class="inputtext">
    <input type="text" onfocus="if (this.value == 'Code') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Code';}" value="Code" id="code" name="code" class="inputtext">

    <input type="radio" class="radio" id="news" name="news" onclick="ToggleWithNews()">
    <div class="radiotext">Sign up?</div>

    <input type="hidden" id="WithNews" name="WithNewsValue" value="No">
</form>

<script>
    function ToggleWithNews () {
        document.getElementById('WithNews').value = 'Yes';
    }
</script>

Done!!!


My note : If you have only on option and the viewer can toggle on or off that option, you should use 2 radio buttons on for Yes and one for No, because one radio button will be unable to un-check once it has been checked.

Or

If you want to only add on input for that option, you should be using a checkbox input, since this can be checked and unchecked at any time.


Hope my answer helped you :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜