0

我在这里和其他地方读过很多关于记住单选按钮的帖子。我还阅读了很多关于初始化一个单选按钮的帖子。我没有读过很多书是把两者结合起来。如果可能,我想在输入语句中执行此操作。

记忆示例(即在重新显示页面时保留突出显示的单选按钮):

<input type="radio" name="sex" value="Male" <?php echo ($sex=='Male')?'checked':'' ?   >size="17">Male
<input type="radio" name="sex" value="Female" <?php echo ($sex=='Female')?'checked':'' ?> size="17">Female 

或者

<input type="radio" name="veg" value="cabbage" <?php if(!isset($veg)){print "     checked=\"checked\"";} if(isset($veg) && $veg == "cabbage"){print " checked=\"checked\"";} ?>> cabbage 
<input type="radio" name="veg" value="onion" <?php if(isset($veg) && $veg == "onion"){print " checked=\"checked\"";} ?>> onion  

或者

<input name="searchtype" type="radio" value="artist"  
<? if ($searchtype == 'artist') echo 'checked'; ?>> 
&nbsp;&nbsp;&nbsp; 
Title: 
<input name="searchtype" type="radio" value="title"  
<? if ($searchtype == 'title') echo 'checked'; ?>> 
&nbsp;&nbsp;&nbsp; 
Year: 
<input name="searchtype" type="radio" value="year"  
<? if ($searchtype == 'year') echo 'checked'; ?>>  

第一个似乎最简单,但我无法让它工作。我不断收到一些错误,大概是语法。这是我的部分代码:

<input type="radio" name="typ" value="exact" <?php if ($_GET["typ"]=="exact") echo "checked=\"checked\""; ?>Exact
<input type="radio" name="typ" value="starts" <?php if ($_GET["typ"]=="starts") echo "checked=\"checked\""; ?>Starts with
<input type="radio" name="typ" value="sounds" <?php if ($_GET["typ"]=="sounds") echo "checked=\"checked\""; ?>Soundex</td></tr>
<input type="submit" value="Find" /></td></tr>

第二个问题是以某种方式将默认值合并到上述内容中。

我的尝试涉及使用上面代码的第三种格式并最初尝试测试 null。像这样的东西。它再次不起作用:

从 Soundex 开始

以下代码有效,但很冗长:

$exact_status = 'unchecked';
$starts_status = 'unchecked';
$sounds_status = 'unchecked';

if (isset($_POST['typ'])) {
   $selected_radio = $_POST['typ'];
   if ($selected_radio == 'exact')
     {$exact_status = 'checked';} 
   else if ($selected_radio == 'starts') 
      {$starts_status = 'checked';} 
   else if ($selected_radio == 'sounds') 
      {$sounds_status = 'checked';}
}
else {
$exact_status = 'checked';} 
echo 
  '<p>Enter Surname and optionally Given Name(s):  
  <form action="'.$_SERVER['PHP_SELF'].'" method="post">
  <table cellpadding="4" cellspacing="0">
  <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr>
  <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr>
  <tr><td valign="top">Search Type:</td><td>
    <input type="radio" name="typ" value="exact"'.$exact_status.'>Exact
    <input type="radio" name="typ" value="starts"'.$starts_status.'>Starts with
    <input type="radio" name="typ" value="sounds"'.$sounds_status.'>Soundex
  </td></tr>
  <tr><td></td><td><input type="submit" value="Find" /></td></tr>
  </table>
  </form>  
  <p>';

根据下面的评论,我尝试了这个。它几乎可以工作。第一个输入框有效。接下来的两个只是输出单词“checked”而不是输入语句???

if (!isset($_POST['typ']))
{$radio1 = '<input type="radio" name="typ" value="exact" checked>Exact';}
else 
{$radio1 = '<input type="radio" name="typ" value="exact"'.($_GET['typ'] =="exact")?"checked":''.'>Exact';}
 $radio2 = '<input type="radio" name="typ" value="starts"'.($_GET['typ'] =="starts")?"checked":''.'>Starts with';
 $radio3 = '<input type="radio" name="typ" value="sounds"'.($_GET['typ'] =="sounds")?"checked":''.'>Soundex';

echo 
  '<p>Enter Surname and optionally Given Name(s):  
  <form action="'.$_SERVER['PHP_SELF'].'" method="post">
  <table cellpadding="4" cellspacing="0">
  <tr><td valign="top">Surname:</td><td><input type="text" size=40 name="surname" value="'.$surname.'" /></td><td>Required</td></tr>
  <tr><td valign="top">Given Name(s):</td><td><input type="text" size=40 name="firstname" value="'.$firstname.'" /></td><td>Optional</td></tr>
  <tr><td valign="top">Search Type:</td><td>',$radio1,$radio2,$radio3, 
  '</td></tr>
  <tr><td></td><td><input type="submit" value="Find" /></td></tr>
  </table>
  </form>  
  <p>';

换句话说,在第一个单选按钮之后它说:Exactcheckedchecked。接下来的两个按钮不见了。

4

1 回答 1

0
<input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="exact")?"checked":'' ?>>Exact
<input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="starts")?"checked":'' ?>>Starts with
<input type="radio" name="typ" value="exact" <?php echo ($_GET['typ'] =="sounds")?"checked":'' ?>>Soundex

<input type="submit" value="Find" /></td></tr>

像上面这样的东西应该可以解决问题。如果并且是 - 我会说 - 仅在上述情况下有用,它被称为简写。

请阅读更多关于它的信息: http: //davidwalsh.name/php-ternary-examples其中有很多关于如何使用它的示例。

Lycka 直到!

于 2013-09-20T09:00:28.610 回答