Solved

PHP Radio Issue

Posted on 2015-02-08
10
147 Views
Last Modified: 2015-02-08
 
    <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
Comment
Question by:doctorbill
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 168 total points
ID: 40596817
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
 

Author Comment

by:doctorbill
ID: 40596822
quotes make no difference at all - sorry
0
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 168 total points
ID: 40596837
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 40596862
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40596903
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:doctorbill
ID: 40596917
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 166 total points
ID: 40596935
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 40597004
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
 

Author Comment

by:doctorbill
ID: 40597381
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
 

Author Closing Comment

by:doctorbill
ID: 40597385
Solution already there
Valuable information
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now