简体   繁体   中英

isset() does not work properly by using include in PHP

I am a very beginner in programming PHP. I have the following HTML code and two PHP code "include". I am not sure why ISSET() not firing at all. If I remove the isset(){} it brings up the data perfectly fine... but when I try with isset() it shows no data. Can anyone help?

 <form action="functions/pos-getPrice.php"class="form-horizontal" method="post"> <div class="form-group"> <label class="col-md-4 control-label">Item</label> <div class="col-md-6 selectContainer"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i> </span> <select name="item" id="item" class="form-control selectpicker" > <option>Please select your item</option> <?php include "functions/pos-getItems.php" ?> </select> </div> </div> <div class="form-group"> <label class="col-md-4 control-label">Price</label> <div class="col-md-6 selectContainer"> <div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i> </span> <select name="price" class="form-control selectpicker" > <option value="">Please select your price</option> <?php include "functions/pos-getPrice.php" ?> </select> </div> </div> </form> 

 <?php if(isset($_POST['item']) && $_POST['item'] == 'jacket') { include "db-Info.php"; $res = mysqli_query($con, "select id, price, pointRequired from tblPrice"); while($row = mysqli_fetch_array($res)) { $price = $row['price']; $pointRequired = $row['pointRequired']; echo "<option value=\\"".$row['id']."\\">"; echo "Price: $price" ?> &nbsp;<?php echo "Point: $pointRequired"; echo "</option>"; } } ?> 

 <?php include "db-Info.php"; $res = mysqli_query($con, "select distinct item from tblPrice"); while($row = mysqli_fetch_array($res)){ $array = $row['item']; echo "<option value=\\"".$row['item']."\\">"; echo $array; echo "</option>"; } ?> 

From what I can tell, I'm assuming your item list is loading correctly, and your price list is not? Are you expecting the price list to change when a user selects in item?

In this instance, there is no $_POST data when you load your page containing the form. Selecting an item in your dropdown does not send any kind of POST request causing the prices to change. Thus, at the time of page load, your price script is checking to see if $_POST["item"] is set and if that item is "jacket," which it is not at the time of page load. So no prices load, and that's that; the script doesn't keep checking what the item is after that.

If you're expecting prices to dynamically load based on the selected item, you'll either need to have an AJAX call that fetches the price options every time an item is selected, or you can preload all the price data upfront when the page loads, then use javascript to populate the price options on change of the item selector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM