Tuesday 15 May 2012

php - display result from dropdown menu only when value from it is selected -



php - display result from dropdown menu only when value from it is selected -

this code demo part of search form. actual search form contains 3 dropdown list, values selected dropdown list acts keyword on basis of search conducted. dropdown list contains fruits such mango, apple, grapes etc. search code working fine. issue first alternative displayed in dropdown list select (that holds no value), below actual list of fruit starts, still value of first fruit getting selected when page getting loaded first time . trying when page loads first time i.e value in drop downwards list select nil should displayed , after when user selects value dropdown , hits submit button result should displayed

<div class="col-md-3 col-sm-5"> <div class="media"> <div class="media-body"> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "db"; // create connection $con = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$con) { die("connection failed: " . mysqli_connect_error()); } $sql = "select fruits fruits"; $result = $con->query($sql); echo "<label for='fruits'>treatment type: </label>"; echo "<select name='fruits' id='fruits' class='form-control'><option value=''>--select--</option>"; while($row = $result->fetch_assoc()) { echo "<option value='" . $row['fruits'] . "'>" . $row['fruits'] . "</option>"; } echo "</select>"; ?> </div> </div> </div>

code search part

<?php $con=mysqli_connect("","","","");// check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $fruits = mysqli_real_escape_string($con, $_post['fruits']); $sql1 = "select * treatment fruits '%$fruits%'"; $result = mysqli_query($con, $sql1); echo "<table class='table table-striped table-bordered responsive'> <thead> <tr> <th>name</th> <th>type</th> <th>fruits</th> </tr> </thead>"; if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "<tbody data-link='row' class='rowlink'>"; echo "<tr>"; echo "<td><a href='#'>" . $row['name'] . "</a></td>"; echo "<td>" . $row['type'] . "</td>"; echo "<td>" . $row['fruits'] . "</td>"; echo "</tr>"; echo "</tbody>"; } } else { echo "0 results"; } echo "</table>"; mysqli_close($con); ?>

would appreciate if can guide me

p.s (edited part)

<div class="col-md-3 col-sm-5"> <div class="media"> <div class="media-body"> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "db"; // create connection $con = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$con) { die("connection failed: " mysqli_connect_error()); } $sql = "select fruits fruits"; $result = $con->query($sql); ?> echo "<label for="fruits">treatment type: </label>"; echo "<select name="fruits" id="fruits" class="form-control"> <option value="" <?php if(!isset($_post['fruits'])) { ?>selected<?php } ?>>--select--</option>"; <?php while($row = $result->fetch_assoc()) { ?> echo "<option value="<?php echo $row['fruits']; ?>" <?php if(isset($_post['fruits']) && $_post['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>"; <?php } ?> </select> </div> </div> </div>

in <option> portions, modify bit. add together selected conditions:

<div class="col-md-3 col-sm-5"> <div class="media"> <div class="media-body"> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "db"; // create connection $con = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$con) { die("connection failed: " . mysqli_connect_error()); } $sql = "select fruits fruits"; $result = $con->query($sql); ?> <label for="fruits">treatment type: </label> <select name="fruits" id="fruits" class="form-control"> <option value="" <?php if(!isset($_post['fruits']) || (isset($_post['fruits']) && empty($_post['fruits']))) { ?>selected<?php } ?>>--select--</option> <?php while($row = $result->fetch_assoc()) { ?> <option value="<?php echo $row['fruits']; ?>" <?php if(isset($_post['fruits']) && $_post['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option> <?php } ?> </select> </div> </div> </div>

results page:

<?php $con = mysqli_connect("","","","");// check connection if(mysqli_connect_errno()) echo "failed connect mysql: " . mysqli_connect_error(); $fruits = mysqli_real_escape_string($con, $_post['fruits']); if(!empty($fruits)) { $sql1 = "select * treatment fruits '%$fruits%'"; $result = mysqli_query($con, $sql1); $count = mysqli_num_rows($result); } else $count = 0; ?> <table class='table table-striped table-bordered responsive'> <thead> <tr> <th>name</th> <th>type</th> <th>fruits</th> </tr> </thead> <?php if ($count > 0) { while($row = mysqli_fetch_assoc($result)) { ?> <tbody data-link='row' class='rowlink'>"; <tr> <td><a href='#'><?php echo $row['name']; ?></a></td> <td><?php echo $row['type']; ?></td> <td><?php echo $row['fruits']; ?></td> </tr> </tbody><?php } } else echo "0 results"; ?> </table> <?php mysqli_close($con); ?>

php html sql drop-down-menu mysqli

No comments:

Post a Comment