Problems with radiobuttons remain upon error

Heja,

I’m currently running a osTicket site for my department at work just to make ticket handling easier.
Though I’ve got one problem with our Radio buttons - the form won’t remember what radio button I chose if I get any errormsg.
So my current code is:

<tr> <td><b>Återkoppla till kund? </td> <td> <input name='aterkom[]' type=radio value="Ja">Ja <input name='aterkom[]' type=radio value="Nej">Nej &nbsp;<font class="error">* &nbsp;<?=$errors['aterkom']?></font> </td> </tr>

What I’ve done to try make it work:

<tr> <td><b>Återkoppla till kund? </td> <td> <? $aterkomja_checked = (isset($_POST['aterkom[]']) && $_POST['aterkom[]'] == 'Ja') ? 'checked' : ''; $aterkomnej_checked = (isset($_POST['aterkom[]']) && $_POST['aterkom[]'] == 'Nej') ? 'checked' : ''; ?> <input name='aterkom[]' type=radio value="Ja" <?=$aterkomja_checked?>>Ja <input name='aterkom[]' type=radio value="Nej" <?=$aterkomnej_checked?>>Nej &nbsp;<font class="error">* &nbsp;<?=$errors['aterkom']?></font> </td> </tr>

But the form still doesn’t remember the radio buttons.
and if I remove the brackets out of input name=‘aterkom[]’ and on isset($_POST[‘aterkom[]’ it works great with remembering the radio buttons!
The only problem is that then the information won’t get to the database.

Do anyone of you have any clue on what I should do? Can I somehow use the closed brackets in my isset?
Or can I come around with removing the closed brackets and still make it populate with the database?

Here’s the whole script:
http://www.openscriptsolution.com/ticket-system/osticket/how-to-add-extra-fields-on-the-open-ticket-form-of-client-side-in-osticket-v1-6-rc5/

Thanks a lot in advance!

Best regards,
PleXy

I don’t think you want to use empty brackets [] for radio buttons? These are typically used for checkboxes or if you have defined array keys. As far as not inserting into the database, you would have to post that code.

the html is wrong too

do

<input name='aterkom[]' type=radio value="Ja" <?=(isset($_POST['aterkom'] == 'Ja') ? 'checked="checked"' : '') ?> />Ja

The HTML is fine it’s just a matter of preference. If “checked” is present it returns true.

<input checked>
<input checked="checked">
<input checked="">

http://www.w3schools.com/tags/att_input_checked.asp

yea but not all the browsers support the newer definitions. i’ve had problems with it on some of the newer browsers.

Wow! So many great answers, this is my first time on this forum and i’m glad I found it.

I tried that but that didn’t work so I did it like this:

<input name='aterkom[]' type=radio value="Ja" <?=(isset($_POST['aterkom']) == 'Ja') ? 'checked="checked"' : ''; ?> />Ja <input name='aterkom[]' type=radio value="Nej" <?=(isset($_POST['aterkom']) == 'Nej') ? 'checked="checked"' : ''; ?> />Nej
With this script the radiobutton gets saved, BUT, when I choose “Ja” changes it to “No” automatically when I update the page?? When I choose “No” it remains on “No”.

By the way: You can see the how it acts here: http://webbhjalpen.com/kund/frontline/open.php
The radiobuttons are in the bottom “Återkoppla till kund?”

Thanks alot for your help, tell me if you need any more information.

just for kicks, try

<input name=‘aterkom_a’ type=radio value=“Ja” <?=(isset($_POST['aterkom_a']) == 'Ja') ? 'checked="checked"' : ''; ?> />Ja
<input name=‘aterkom_b’ type=radio value=“Nej” <?=(isset($_POST['aterkom_b']) == 'Nej') ? 'checked="checked"' : ''; ?>

You can’t check more than 1 radio button anyways, so there’s no real need to have them in an array. Like matt pointed out, that style is used more for checkboxes and multi-select boxes.

If you have to run it like that, then you’ll probably need to use a foreach or a for loop to see which one is selected.

Hey,

Thanks for your reply!

This causes some problems if the agent/customer selects “ja” and then changes his mind to “no” and then both radiobuttons will be selected.

So I’ll probably test out a foreach or a loop. I’m not a very good PHP programmer, do you think you can put me into the right direction?
I’ll look for some guides on Google if not :slight_smile:

Thanks again! I really appreciate the help.

And oh, your answer to that it doensn’t need an array - yep I understand that but unfortunately I don’t have enough skills to remake the script to get it working =(

try something like
[php]for($t=0; $t < count($_POST[‘aterkom’]); $t++) {
if($_POST[‘aterkom’] == ‘Ja’) {
$checked = “checked=‘checked’’”;
} elseif($_POST[‘aterkom’] == ‘Nej’) {
$checked = “checked=‘checked’’”;
}
}[/php]
If that doesn’t work, i’ll write up a short bit of html for testing tonight when i get home.

Thanks for your tip! It did not work though :frowning:
But it gave me an idea like this, but that didn’t work either.

With this code nothing shows up:

[code]<? for($t=0; $t < count($_POST[‘aterkom’]); $t++) {
if($_POST[‘aterkom’] == ‘Ja’) { ?>
Ja
Nej

<? } elseif($_POST['aterkom'] == 'Nej') { ?>

Ja
Nej

<? } else { ?>

Ja
Nej<?
} } ?>[/code]

If I remove the “for($t=0; $t < count($_POST[‘aterkom’]); $t++) {” then it shows the “else” one but even if I choose Yes/No and get an error, it still shows the “else” one.

[code]<?
if($_POST[‘aterkom’] == ‘Ja’) { ?>
Ja
Nej

<? } elseif($_POST['aterkom'] == 'Nej') { ?>

Ja
Nej

<? } else { ?>

Ja
Nej<?
} ?>[/code]

Sorry for spamming, but I thought it might be easier for you if I show you my include file.

In class.ticket.php:

[php]class Ticket{
var $aterkom;[/php]

[php] if(($res=db_query($sql)) && db_num_rows($res)):
$row=db_fetch_array($res);
$this->aterkom =$row[‘aterkom’];[/php]

[php] function getAterkom() {
return $this->aterkom;
}[/php]

[php]$fields[‘aterkom’] = array(‘type’=>‘text’, ‘required’=>1, ‘error’=>‘Återkom till kund krävs’);[/php]

[php] $mycounter=0;
foreach ($var[‘aterkom’] as $app)
{
$strapp .= ($mycounter==0)?$app:", “.$app.”";
$mycounter++;
}
unset($app);[/php]

[php]$extId=Ticket::genExtRandID();
$sql= 'INSERT INTO ‘.TICKET_TABLE.’ SET created=NOW() '.
‘,aterkom=’.db_input($strapp).[/php]

ok, this code i tested, it works
[php]
if($_POST[‘aterkom’][$t] == ‘Ja’) {
$opt = “Ja\n
Nej”;
} else {
$opt = " Ja
Nej";
}[/php]
I don’t know where you had that in your code, but my entire script for testing is [php]<?php
if(isset($_POST[‘submit’])) {
for($t=0; $t < count($_POST[‘aterkom’]); $t++) {
if($_POST[‘aterkom’][$t] == ‘Ja’) {
$opt = “Ja\n
Nej”;
} else {
$opt = " Ja
Nej";
}
}
} else {
$opt = " Ja
Nej";
}
?>

Untitled Document <?=$opt?> [/php] Hope that helps. I think all that needs to be done is adding the [$t] in my previous code, that's all i did.

You are the BEST mate!!!
It works perfectly!

Thank you so much richei, I really appreciate the help!!

;D

Ah, noticed one problem with the last script.

If you don’t press any of the radios and get an error, it doesn’t show any of the radiobuttons (only the error msg <?=$errors['aterkom']?> which is outside the if-code.

Try it out on http://webbhjalpen.com/kund/frontline/open.php - just press submit and you’ll see that the radios in the bottom disappear.

Here’s my full code:
[php]


Återkoppla till kund?

<?php
if(isset($_POST[‘submit_x’])) {
for($t=0; $t < count($_POST[‘aterkom’]); $t++) {
if($_POST[‘aterkom’][$t] == ‘Ja’) {
$opt = “Ja\n
Nej”;
} else {
$opt = " Ja
Nej";
}
}
} else {
$opt = " Ja
Nej";
}
?> <?=$opt?>
&nbsp;<font class="error">* &nbsp;<?=$errors['aterkom']?></font>
</td>
</tr>[/php]

Because submit_x is now set. change the if statement to [php]if($_POST[‘aterkom’][$t] == ‘Ja’) {
$opt = “Ja\n
Nej”;
} elseif($_POST[‘aterkom’][$t] == ‘Nej’) { {
$opt = " Ja
Nej";
} else {
$opt = " Ja
Nej";
}[/php]

Argh, still not working - acting the same.
I also tried with renaming the submit-button to “submit” and changed back the isset to “submit” and still the same problem. :frowning:

So here’s my full script again:
[php]


Återkoppla till kund?

<?php
if(isset($_POST[‘submit_x’])) {
for($t=0; $t < count($_POST[‘aterkom’]); $t++) {
if($_POST[‘aterkom’][$t] == ‘Ja’) {
$opt = “Ja\n
Nej”;
} elseif($_POST[‘aterkom’][$t] == ‘Nej’) {
$opt = " Ja
Nej";
}
}
} else {
$opt = " Ja
Nej";
}
?> <?=$opt?>
&nbsp;<font class="error">* &nbsp;<?=$errors['aterkom']?></font>
</td>
</tr>[/php]

and the submit button:
[php][/php]

I am so confused why you guys are using a for loop and an array for a single radio button?

This should be all you need.

[php]

<?php var_dump($_POST); $aterkom = (isset($_POST['aterkom']) ? $_POST['aterkom'] : "Ja"); // set either posted or default value ?> />Ja />Nej [/php]

Unfortunately that doesn’t seem to work at all. :frowning:
I get strange texts printed out from my include file next to the “Ja” radio…

And I dont think it works w/o the array with the other parts of the scripts (as I’ve posted previously)

Thanks

How so?

That would be the var_dump($_POST). Remove it

These parts of the scripts are wrong and need to be changed.

Sponsor our Newsletter | Privacy Policy | Terms of Service