Skip to content Skip to sidebar Skip to footer

How To Display Radio Button Value Using 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

Solution 1:

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

<formaction="page_you_want_to_display.php"method="POST"><labelfor="type">Job Type:</label><labelfor="fulltime"><inputclass="radio_style"id="fulltime"name="job_type"type="radio"value="fulltime">
        Fulltime
    </label><labelfor="parttime"><inputclass="radio_style"id="parttime"name="job_type"type="radio"value="parttime">
        Part Time
    </label><inputname="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

Solution 2:

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:

<divclass='job_type_div'><?phpif($row['job_type']=='fulltime') {?><spanclass='job_type_style'><?phpecho$row['job_type']; ?></span><?php } elseif($row['job_type']=='parttime') {?><spanclass='job_type_style2'><?phpecho$row['job_type']; ?></span><?php } ?></div>

Solution 3:

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

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

//etc...

Solution 4:

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) ordie(mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_BOTH))
{
   echo$row['job_type'];
}

Solution 5:

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>";

Post a Comment for "How To Display Radio Button Value Using Php"