Free tools Windows power users keep installed
One-click scans. No signup required.
If selecting a radio button should open a different page, JavaScript can set location.href in response to the radio button’s change event. But if the choice is meant to be submitted so PHP can display the matching data, use a form instead. A PHP parse error in this pattern usually comes from conflicting quotes in the PHP string, HTML attribute, and JavaScript.
Why the PHP code causes a parse error
PHP parses the string that contains your HTML before the browser ever sees it. If the string starts and ends with single quotes, then a single quote inside the HTML attribute or JavaScript can accidentally end the PHP string early. The result is a PHP syntax error, not a problem with the browser’s radio-button behavior.
The original SitePoint question used an onclick attribute containing location.href='highP.php'. The key is to keep the PHP string delimiter distinct from the quotes used inside the HTML and JavaScript. The 2012 SitePoint discussion shows the original quoting problem and the follow-up case: choosing between high-precision and high-recall data pages.
Choose the right navigation pattern
For ordinary navigation, use links
If the options are simply links to different pages, use anchors rather than styling navigation as a radio-button choice. An anchor’s href creates a hyperlink to a URL, as described in MDN’s anchor element documentation. This works without JavaScript and communicates the purpose of the control to users and assistive technology.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
For a choice that should be submitted to PHP, use a form
Radio buttons are appropriate when a user is selecting one value from a related group. Only the checked radio button’s value is submitted. Use a form when PHP should receive that value and render the corresponding data set; that keeps the decision on the server rather than hard-coding a separate destination in each control. MDN documents the radio control’s change and input events in its radio input reference.
For an immediate client-side redirect, use a change handler
If each radio option intentionally maps to a fixed URL and selection should navigate immediately, JavaScript can handle the radio group’s change event. Setting location.href navigates to the specified URL. Prefer a JavaScript event listener or a form submission over inline event code when building production pages.
Rank #2
Example: submit the selected data set to PHP
This approach sends the selected value to one PHP endpoint, which can decide what to display:
<form action="results.php" method="get">
<label>
<input type="radio" name="alg_type" value="precision">
High precision
</label>
<label>
<input type="radio" name="alg_type" value="recall">
High recall
</label>
<button type="submit">Show results</button>
</form>
In results.php, read and validate the submitted choice before selecting the data to render. The name groups the radios, and each value identifies the choice submitted to the server. This also works without JavaScript.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Example: redirect immediately when a radio changes
When the destinations are fixed pages, attach a listener after the radios are available in the document:
<label><input type="radio" name="alg_type" value="precision"> High precision</label>
<label><input type="radio" name="alg_type" value="recall"> High recall</label>
<script>
document.querySelectorAll('input[name="alg_type"]').forEach((radio) => {
radio.addEventListener('change', (event) => {
const destinations = {
precision: 'highHp.php',
recall: 'highHR.php'
};
const url = destinations[event.currentTarget.value];
if (url) window.location.href = url;
});
});
</script>
The mapping is explicit, and the destination is chosen from the radio’s value rather than assembled from arbitrary input. Since navigation happens as soon as a choice changes, a user may be taken away before selecting another option; use a submit button if the user should confirm the choice first.
Rank #4
If you keep the inline PHP version
For a quick example, make the outer PHP string use double quotes so the single quotes around the JavaScript URL do not terminate it:
<?php
echo "<label><input type="radio" name="alg_type" value="precision" onchange="location.href='highHp.php'"> High precision</label>";
?>
This illustrates the quoting repair, but it still places JavaScript inline in the HTML and uses the radio as navigation. For maintainable pages, keep markup and JavaScript separate, or use the form pattern when PHP needs the chosen value.
Recommended Free Tools
When to use location.replace()
Use location.href when users should be able to return to the current page with the browser’s Back button. location.replace() navigates to a destination while removing the current page from session history, so Back will not return to that page. Choose it only when that history behavior is intentional; it is not a fix for PHP quoting errors.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

