I am trying to use select tag to send data in mysql table using PHP. I can insert using text type by the following code
<td>
<label class="control-label">Image Type </label>
</td>
<td>
<input class="form-control" type="text" name="user" placeholder="Image Catagory value="<?php echo $user; ?>" />
</td>
I have tried the following code using select tag but it is not showing in html
<td>
<label class="control-label">Image Type </label>
</td>
<td>
<input class="form-control" type="<select>
<option>dog</option>
<option>cat</option>
<option>car</option>
</select>" name="user" placeholder="Image Catagory- ex. Pathology or Diagnostic or Dialysis" value="<?php echo $user; ?>" />
</td>
what I am doing wrong here? how to declare select tag here?
You cannot mix up input and select. here is code for select. in php you will get select option valu by select name.
<td><label class="control-label">Image Type </label></td>
<td><select class="form-control" name="selectName">
<option value="dog">dog</option>
<option value="cat">cat</option>
<option value="car">car</option>
</select>
</td>
<td><label class="control-label">Image Type </label></td>
<td>
<select name="user">
<option value="dog" <?php echo $user=="dog" ? "selected":""; ?> >Dog</option>
<option value="cat" <?php echo $user=="cat" ? "selected":""; ?> >Cat</option>
<option value="car" <?php echo $user=="car" ? "selected":""; ?>>Car</option>
</select>
</td>
<html>
<head>
<title>insert data using select</title>
</head>
<body>
<form action="demo.php" method="post">
Name :<select name="data">
<option value="samsang">samsang</option>
<option value="nokia">nokia</option>
<option value="Lg">Lg</option>
<option value="Apple">Apple</option>
</select><br>
<input type="submit" name="submit" value="Insert">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$data=$_POST['data'];
$c=mysql_connect("localhost","root","");
mysql_select_db("test");
$ins=mysql_query("insert into option (name) values ('$data')",$c);
if($ins)
{
echo "<br>".$data."inserted";
}
else
{
echo mysql_error();
}
}
?>