dsa45664
2012-05-03 22:54
浏览 153
已采纳

如何使用PHP显示单选按钮值

So I have a form that users fill out with some radio buttons. The values from the radio buttons get passed to MySQL. I now want to pull those values from the database, display them in a table on a different page, and apply different styles to them with span tags.

Here's the code from the form:

<input class="radio_style" type="radio" name="job_type" value="fulltime"/>Full-time<br/>
<input class="radio_style" type="radio" name="job_type" value="parttime"/>Part-time<br />

Here's the code for the page where I want to display it:

<div class='job_type_div'>
    <?php if($job_type=='fulltime') {?>
        <span class='job_type_style'>
            <?php echo $row['job_type']; ?>
        </span>
   <?php if($job_type=='parttime') {?>
        <span class='job_type_style2'>
            <?php echo $row['job_type']; ?>
        </span>
            <?php } ?>
    <?php } ?>
</div>

So ideally, the "fulltime" value will have one style and the "parttime" value will have another style. But when I try running this code, nothing happens. I'm definitely connecting to the database correctly. And the row name is properly labelled "job_type". Any ideas on where I might be going wrong? Any help would be greatly appreciated :)

图片转代码服务由CSDN问答提供 功能建议

所以我有一个用户用一些单选按钮填写的表单。 单选按钮的值传递给MySQL。 我现在想从数据库中提取这些值,将它们显示在不同页面上的表中,并使用span标记对它们应用不同的样式。

以下是表单中的代码:< / p>

 &lt; input class =“radio_style”type =“radio”name =“job_type”value =“fulltime”/&gt;全职&lt; br /&gt; 
&lt;  input class =“radio_style”type =“radio”name =“job_type”value =“parttime”/&gt;兼职&lt; br /&gt; 
   
 
 

这里是 我想要显示它的页面的代码:

 &lt; div class ='job_type_div'&gt; 
&lt;?php if($ job_type =='fulltime  '){?&gt; 
&lt; span class ='job_type_style'&gt; 
&lt;?php echo $ row ['job_type'];  ?&gt; 
&lt; / span&gt; 
&lt;?php if($ job_type =='parttime'){?&gt; 
&lt; span class ='job_type_style2'&gt; 
&lt;?php echo $ 行[ '工作类型'];  ?&gt; 
&lt; / span&gt; 
&lt;?php}?&gt; 
&lt;?php}?&gt; 
&lt; / div&gt; 
   
 
  

理想情况下,“全时”值将具有一种样式,而“兼职”值将具有另一种样式。 但是当我尝试运行此代码时,没有任何反应。 我肯定正确地连接到数据库。 行名称正确标记为“job_type”。 关于我可能出错的任何想法? 任何帮助将不胜感激:)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongshie8450 2012-05-04 00:58
    已采纳

    may be problem in your php. Is there some logic?

    $job_type=null;
    if($job_type=='fulltime'){
       ...
       if($job_type=='parttime'){
          ...
       }
    }
    

    did you set $job_type variable? May be you need something like this:

    <div class='job_type_div'>
        <?php if($row['job_type']=='fulltime') {?>
            <span class='job_type_style'>
                <?php echo $row['job_type']; ?>
            </span>
       <?php } elseif($row['job_type']=='parttime') {?>
            <span class='job_type_style2'>
                <?php echo $row['job_type']; ?>
            </span>
        <?php } ?>
    </div>
    
    点赞 评论
  • dongxi0605 2012-05-03 22:59

    I don't believe that the conditions will work the way you implemented it, try doing it like this:

    <?php
    echo "<div class='job_type_div'>";
    if($job_type=='fulltime') {
            echo "<span class='job_type_style'>"
    

    //etc...

    点赞 评论
  • doumei9589 2012-05-03 23:04

    form.php

    if you initially set one 'selected' in your form, dont need to check if its set, simple set db like so:

    ...
    mysql_query("UPDATE X SET (job_type='{$_GET['job_type']}') WHERE Y");
    ...
    

    display.php

    As you will probably be making a stylesheet, reference the selectors with the job_type labels, which you put in your database

    while($row = mysql_fetch_assoc($resultsource))
       echo "<div class='job_type_div'> <span class='{$row['job_type']}_style'>{$row['job_type']}</span>";
    
    点赞 评论
  • dongpou1935 2012-05-03 23:04

    While you fetching your array from Data Base you need to use MYSQL_BOTH, to fetch columns by Name.

       mysql_fetch_array($res, MYSQL_BOTH)
    

    So you should have something like this:

    $job_type_query = "SELECT * FROM `job`; ";
    
    $res = mysql_query($job_type_query) or die(mysql_error());
    while ($row = mysql_fetch_array($res, MYSQL_BOTH))
    {
       echo $row['job_type'];
    }
    
    点赞 评论
  • douhan8009 2012-05-03 23:30

    First of all, your form should be something like so:

    <form action="page_you_want_to_display.php" method="POST">
    
        <label for="type">Job Type:</label>
    
        <label for="fulltime">
            <input class="radio_style" id="fulltime" name="job_type" type="radio" value="fulltime">
            Fulltime
        </label>
    
        <label for="parttime">
            <input class="radio_style" id="parttime" name="job_type" type="radio" value="parttime">
            Part Time
        </label>
    
        <input name="submitted" type="submit" value="Submit">
    
    </form>
    

    The page you want to display on should look something like this:

    if(isset($_POST["submitted"])){
    
        $job_type = $_POST['job_type'];
    
        echo '<div class="job_type_div">';
    
            if($job_type=='fulltime'){
    
                $res = mysql_query("SELECT * FROM jobs WHERE job_type='fulltime'");
    
                while ($row = mysql_fetch_assoc($res)) {
    
                    echo '<div class="fulltime">';
    
                        echo $row['job_title'].' - '.$row['job_type'];
    
                    echo '</div>';
    
                    echo '<br>';
    
                }
    
            } elseif ($job_type=='parttime'){
    
                $res = mysql_query("SELECT * FROM jobs WHERE  job_type='parttime'");
    
                while ($row = mysql_fetch_assoc($res)) {
    
                    echo '<div class="parttime">';
    
                        echo $row['job_title'].' - '.$row['job_type'];
    
                    echo '</div>';
    
                    echo '<br>';
    
                }               
    
            }
    
    echo '</div>';
    
    }
    

    and CSS:

    .fulltime {
        margin:0px;
        padding:5px;
        width:300px;
        background:#9C0;
        color:#fff;
    }   
    .parttime {
        margin:0px;
        padding:5px;
        width:300px;
        background:#069;
        color:#fff;
    }
    

    Tested:

    enter image description here

    Hope this helps

    点赞 评论

相关推荐 更多相似问题