Sithelo
Sithelo

Reputation: 307

Radio button value in php not showing

I have a form that searches my local database. Its still in its production phase. However when I try to get the value of my radio button there is nothing. Below is the code:

PHP

$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM  series8000 WHERE ";
$vision = $_GET['Vision'];
$new = $_GET['new1'];
$sithelo = sithelo;
foreach ($terms as $each) {
    $i++;
    if ($i == 1)
        $query .= "Description LIKE '%$each%' ";
    else
        $query .= "OR Description LIKE '%$each%' ";
}   
$query .= "AND VisionType = '$vision' ";

if (!empty($new1 )) {
    $query .= "AND Change = '$new1' ";
}

HTML

<form action='index.php' method='get'>
<fieldset>


                <legend>Change</legend> 


                    <label for="New" class="property">New Product</label>
                    <div class="labels">
                        <input type="radio"  name="new1" <?php if (isset($new1) && $new1 == "N") echo "checked=yes"; ?>size="30" value="N"/>
                        <input type="radio"  name="new1" size="30" value="Y"/>
                        <input type="radio"  name="new1" size="30"  value="YN"/>&nbsp;&nbsp;
                    </div>  

                    <div style="clear: both;"></div>

The challenge is the value of the radio button with name new1 comes blank when I echo in my php code not shown here. I have to get the value of the radio button so that i can use it in my query.

Upvotes: 1

Views: 1813

Answers (3)

vicrabb
vicrabb

Reputation: 190

You put the value of new1 in a variable $new but you don't seem to use it when it comes to this line:

if (!empty($new1 )) {
    $query .= "AND Change = '$new1' ";
}

Try perhaps with:

if (!empty($new)) {
    $query .= "AND Change = '$new' ";
}

And also use what Kalai said:

 echo "checked='checked'";

Upvotes: 1

Henrik Ammer
Henrik Ammer

Reputation: 1899

Change

<input type="radio"  name="new1" <?php if (isset($new1) && $new1 == "N") echo "checked=yes"; ?>size="30" value="N"/>

Into

<input type='radio' name='new1' <?php if (isset($new1) && $new1 == "N") echo "checked='checked' "; ?>size='30' value='N'/>

My answer differs since there is a lack of space between the outputted checked='checked' and the size='30' in previous answers.

Upvotes: 2

Kalaiyarasan
Kalaiyarasan

Reputation: 13364

Please try with checked= 'checked' instead of using checked=yes.

Upvotes: 3

Related Questions