Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A radio button can navigate to a URL, but the reliable implementation depends on what the control means. If the choice is ordinary navigation, use links. If it is a submitted option that determines server-rendered data, use a form. For an immediate client-side redirect, attach a change handler and keep PHP, HTML, and JavaScript quoting separate.

Why the original PHP code produced a parse error

The error is caused by PHP parsing, not by the radio button itself. A typical example places an HTML attribute and JavaScript inside a PHP string:

<?php
echo '<input type="radio" onclick="location.href='highP.php'">';
?>

The outer PHP string starts with a single quote. The single quote before highP.php closes that string unexpectedly, so PHP encounters the remaining characters as invalid syntax.

Use a different outer delimiter, escape the embedded delimiter, or avoid building the whole element as one quoted string.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use double quotes around the PHP string

<?php
echo '<label><input type="radio" name="alg_type" value="precision" onchange="location.href='highHp.php'"> High precision</label>';
?>

Here the PHP string uses single quotes, so the JavaScript URL quotes must be escaped. Alternatively, use a double-quoted PHP string and escape its double quotes:

<?php
echo "<label><input type="radio" name="alg_type" value="precision" onchange="location.href='highHp.php'"> High precision</label>";
?>

Prefer leaving HTML outside PHP strings

This is usually clearer and removes one layer of quoting:

<label>
  <input type="radio" name="alg_type" value="precision" onchange="location.href='highHp.php'">
  High precision
</label>

<label>
  <input type="radio" name="alg_type" value="recall" onchange="location.href='highHR.php'">
  High recall
</label>

Immediate navigation when each choice has a fixed destination

For the original high-precision/high-recall case, a radio group can redirect as soon as its value changes:

<label>
  <input type="radio" name="alg_type" value="precision" onchange="location.href='highHp.php'">
  High precision
</label>
<label>
  <input type="radio" name="alg_type" value="recall" onchange="location.href='highHR.php'">
  High recall
</label>

location.href assigns the browser’s current URL and keeps the current page in session history, so the Back button can return to it. If returning to the selection page would be undesirable, use location.replace() instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<input type="radio" name="alg_type" value="precision"
       onchange="location.replace('highHp.php')">

Only use a destination that your application controls or validates. Do not concatenate unchecked user input into a redirect URL.

A cleaner JavaScript implementation

Inline handlers work for a small example, but an event listener keeps behavior out of the markup and makes the PHP template easier to read:

<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>
  const destinations = {
    precision: 'highHp.php',
    recall: 'highHR.php'
  };

  document.querySelectorAll('input[name="alg_type"]').forEach((radio) => {
    radio.addEventListener('change', (event) => {
      const url = destinations[event.target.value];
      if (url) window.location.href = url;
    });
  });
</script>

Radio controls expose change and input events. The change event is the natural choice for navigation because it runs when the user’s selection changes.

When a form is the better solution

If the selected value is data that PHP must process, submit a form instead of hard-coding a separate page for every option. The browser sends only the checked control’s value for a radio group, and PHP reads it from $_GET or $_POST.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<form method="get" action="results.php">
  <fieldset>
    <legend>Choose the data set</legend>
    <label>
      <input type="radio" name="alg_type" value="precision" required>
      High precision
    </label>
    <label>
      <input type="radio" name="alg_type" value="recall">
      High recall
    </label>
  </fieldset>
  <button type="submit">Load data</button>
</form>

A selection produces a URL such as results.php?alg_type=precision. On the server, allow-list the values rather than using arbitrary input as a filename or query fragment:

<?php
$allowed = [
    'precision' => 'highHp.php',
    'recall' => 'highHR.php',
];

$type = $_GET['alg_type'] ?? '';
if (!isset($allowed[$type])) {
    http_response_code(400);
    exit('Choose a valid data set.');
}

// Render the data associated with $type, or include the mapped resource.
?>

Use method="post" when the choice changes server state or should not appear in the address bar. A form also remains usable when JavaScript is disabled.

Radio button or link?

Radio buttons represent one mutually exclusive choice from a related group. Anchors represent navigation. If the user is simply choosing between two pages, links communicate that purpose more clearly and work without JavaScript:

<nav aria-label="Choose a data set">
  <a href="highHp.php">High precision</a>
  <a href="highHR.php">High recall</a>
</nav>

Use radios when the choice is part of a form or configuration and the selected value matters to a subsequent submission. Use anchors when each option is a destination in its own right.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Approach Best use JavaScript required History behavior Server receives a selected value
Anchor with href Ordinary navigation No Creates a history entry Only through the URL
Radio with onchange and location.href Immediate redirect from a choice Yes Creates a history entry Only if encoded in the destination URL
Radio with onchange and location.replace Redirect without retaining the selection page Yes Replaces the current entry Only if encoded in the destination URL
Radio inside a form Server-rendered results based on the choice No Depends on the form method and resulting response Yes
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Accessibility and usability details

  • Give every radio in the group the same name; otherwise users can select multiple values.
  • Give each option a distinct value that PHP can validate.
  • Wrap the input in a label or connect a separate label with for and id, so the text is clickable.
  • Use a fieldset and legend when the options form a meaningful question.
  • Do not make navigation depend only on hover or pointer events; keyboard users must be able to focus and change the control.
  • If changing the radio immediately leaves the page, make that consequence clear in the visible label or surrounding text.

Common failure modes

The PHP parser still reports an unexpected token

Inspect the PHP string immediately before the reported line. Count its opening and closing quotes, then check quotes inside onclick, href, and JavaScript. Moving the HTML outside PHP, as shown above, is safer than adding more escapes.

The URL loads, but the wrong option is processed

Check that both radios share one name, that each value matches the server’s allow-list, and that the destination reads the same parameter name. A redirect to a fixed file does not automatically submit the radio’s value.

The browser goes back to an unwanted intermediate page

Use location.replace() only when replacing the current history entry is genuinely the desired behavior. Otherwise keep location.href, which preserves normal Back-button navigation.

Nothing happens without JavaScript

Replace the redirecting radios with links, or submit a form. Those approaches provide a working fallback and are generally more appropriate for navigation and server-side selection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Bottom Line

For fixed page destinations, an anchor is usually the clearest solution. If the UI must be a radio group, use a change handler with carefully separated quoting. When PHP needs the selected value, submit a form and validate an allow-listed parameter on the server.

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.