[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

PHP Radio Issue

 
    <input type="radio" name="units" id="cms" value="cm" class="validate[condRequired[im_size_port]] text-input"
<?php if ($row_artgaladd[artgal_sizeUnit] == "cm") { ?>
      checked="checked" <?php } ?>  />
    cm
    </label>
    &nbsp;<input type="radio" name="units" id="inches" value="inches" class="validate[condRequired[im_size_land]] text-input"
<?php if ($row_artgaladd[artgal_sizeUnit] == "inches") { ?>
      checked="checked" <?php } ?> />
    inches

can someone please tell me what is wrong with the script above
The radio buttons do not reflect the statue dof the current radio checked, even though the database entries are correct
0
doctorbill
Asked:
doctorbill
  • 4
  • 2
  • 2
  • +1
6 Solutions
 
Marco GasiFreelancerCommented:
You have to add quotes:

$row_artgaladd['artgal_sizeUnit']

But if you didn't get any error message, it means you have disabled error_reporting: in developing environment it's absolutely useful t set error_reporting to E_ALL so you can get all error, warning and even notice messages

error_reporting(E_ALL);

Put this at the top of each script: you'll save a lot of time :-)
0
 
doctorbillAuthor Commented:
quotes make no difference at all - sorry
0
 
Marco GasiFreelancerCommented:
Sorry, but you must use it anyway, otherwise php will think that those values are constant.
Please, set error_erporting to E_ALL and post here the error message you get, so we'll have a starting point.
Also, in your code above I see a closing tag for label but I don't find an opening tag.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Chris StanyonCommented:
doctorbill,

It's often easier to debug these issues if you take a step back - code a clean solution, and output any data your script is likely to use, as well as turning on error reporting, as Marco has already indicated.

Have a look at this, and see if it makes sense:

<?php error_reporting(E_ALL); ?>

<pre>
<?php var_dump($row_artgaladd['artgal_sizeUnit']); ?>
</pre>
		
<label for="cm">cm</label>
<input type="radio" name="units" id="cm" value="cm"
<?php echo ($row_artgaladd['artgal_sizeUnit'] == "cm") ? 'checked="checked"' : ''; ?>
/>
    	
<label for="inches">inches</label>
<input type="radio" name="units" id="inches" value="inches"
<?php echo ($row_artgaladd['artgal_sizeUnit'] == "inches") ? 'checked="checked"' : ''; ?>
/>

Open in new window


Basically, it will turn on error reporting, and dump the value from your database. It then uses what's called a ternary if statement to decide whether to echo out the checked attribute or not. It also makes proper use of the <label> element. Give it a go and report back with any error message.
0
 
Ray PaseurCommented:
Quotes in PHP:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

Checkboxes in PHP (they are a lot like radio buttons, but they allow multiple selections instead of mutually exclusive selections):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html

The principal issue with radio buttons is that only one of each name can be checked.  In this code snippet, copied from the original question, there are conditional statements associated with two radio controls named "units" and these conditionals may not be syntactically correct.  Take the "checked" attribute out and try it again, using the client input instead of pre-populating the HTML with what might be bad code.

&nbsp;
    <input type="radio" name="units" id="cms" value="cm" class="validate[condRequired[im_size_port]] text-input"
<?php if ($row_artgaladd[artgal_sizeUnit] == "cm") { ?>
      checked="checked" <?php } ?>  />
    cm
    </label>
    &nbsp;<input type="radio" name="units" id="inches" value="inches" class="validate[condRequired[im_size_land]] text-input"
<?php if ($row_artgaladd[artgal_sizeUnit] == "inches") { ?>
      checked="checked" <?php } ?> />
    inches

Open in new window

Also, in addition to using error_reporting(E_ALL), you may want to try using the W3 validator to be sure your PHP is generating valid HTML!  I can't begin to tell you how many headaches these error-checking tools have saved me.  Another one that is very, very helpful: DevTools.  It's like having X-Ray vision for your DOM.
0
 
doctorbillAuthor Commented:
Ray
Can you explain this:

Take the "checked" attribute out and try it again, using the client input instead of pre-populating the HTML with what might be bad code.

Do I take out the following:
<?php if ($row_artgaladd[artgal_sizeUnit] == "cm") { ?>
      checked="checked" <?php } ?>  />
0
 
Ray PaseurCommented:
Yes, I think I would try something like that.  The point here is to isolate the possible points of functional failure.  There are many inflection points in the path from PHP to HTML to form submit to PHP to database to display (it's a long path) and anything we can do to simplify and deconstruct it will help make the debugging more certain and faster.

Here's a minimalist example using just a radio button.  When you run this, PHP creates a script and sends it to the browser.  The client makes a selection and sends it back to the server.  The server examines the input from the client and produces a response that tells what was selected.  You might start with a drop-dead simple "hello world" example like this one and build up the parts of the application step-by-step from there.  That's what most of us do when we have to do this sort of thing for a living!
http://iconoun.com/demo/temp_doctorbill.php

<?php // demo/temp_doctorbill.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28612577.html
 */
error_reporting(E_ALL);


// THE ACTION SCRIPT -- IF THERE HAS BEEN A POST-METHOD REQUEST
if (!empty($_POST['thing']))
{
    echo PHP_EOL . "THIS WAS CHOSEN: " . $_POST['thing'];
}


// MAKE AN HTML DOCUMENT WITH A FORM AND A RADIO BUTTON INSIDE
$form = <<<ENDOF
<form method="post">
<input type="hidden" name="thing" value="Nothing!" />
<input type="radio"  name="thing" value="One" />One
<br>
<input type="radio"  name="thing" value="Two" />Two
<br>
<input type="submit"              value="Go!" />
</form>
ENDOF;


// WRITE THE HTML FORM DOCUMENT TO THE BROWSER
echo $form;

Open in new window

0
 
Chris StanyonCommented:
Removing your IF statement will prevent your radio buttons from reflecting your database values, so it won't solve your problem. Take a look at the code I posted and it'll dump out the info from your database - you can then check if that's correct and move on from there. If the value dumped isn't 'cm' or 'inches' then neither of your radio buttons will be selected. If you've turned on error reporting then you'll get some detailed feedback about where things went wrong.
0
 
doctorbillAuthor Commented:
Sorry all
Thanks very much for the valuable information

I have to confess I made an error. I was editing 2 php pages at once , both with very similar names. The one I was editing was not the one I was checking in my browser, hence any changes were not being reflected in the browser page
The script I posted is actually working fine

Once again - sorry foe being such a twit
0
 
doctorbillAuthor Commented:
Solution already there
Valuable information
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now