1

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 :)

1
  • What is the output when you echo $job_type? Have you tried using $_REQUEST['job_type'] Commented May 3, 2012 at 22:58

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, thanks for your response. I plugged in your code, but it's not pulling anything from the database for some reason. What does "if(isset($_POST["submit"]))" do?
it it checks that the form has been submitted, more precise whether the "submit" button has been submitted. did you change the query to suit you tables. SELECT * FROM your_table_name WHERE.... also does the column "job_type" in the sql db have values in it such as "partime" and "fulltime" ?
also i made abit of a typo partime should be parttime, ill update solution
yep, i updated the query with the correct table name, but for some reason still not pulling anything...i must have made a mistake somewhere. when i do this i get the data to output from the table... <td class='job_type'> <span class='job_type_style'> <?php echo $row['job_type']; ?> </span> </td> this outputs into the table all of the fulltime and parttime value...so the question is how to style fulltime differently from parttime...
are you wanting to pull all parttim and fulltime together but just style them different? or do you want to pull them out depending on the selected radio and style them different?
|
0

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>

Comments

0

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...

Comments

0

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'];
}

2 Comments

Yes, or use mysql_fetch_assoc() which is the same to mysql_fetch_array(resource $result [, int $result_type = MYSQL_BOTH ]) as says @mschr above
Hey there, thanks a lot for responding. I just had a few questions: what does MYSQL_BOTH do? And how does this apply the CSS styling? I'm a newbie, so I'm having a hard time following. Thanks :)
0

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

3 Comments

yah, and i will send you request with job_type="', name='lamo-admin')#" and all of your data in table will be lost.
q does not go abouts putting in data but getting it out from db... the form.php is for sake of argue, that this is how display.php trusts data to look like
whom ever would name their table X would be asking for you to be lame and send them exploit anyways ;p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.