php - keep selected dynamic dropdown list after submit -
below code populate dynamic list.
<?php if(isset($_post['supplier'])) { include 'db.php'; $stmt = $mysql->prepare("select distinct supplierbrand plastic headingno='".$_post['supplier']."' order supplierbrand"); $stmt->execute(); $stmt->bind_result($supplierbrand); ?> <option value="all" width="100px">--all--</option> <?php while ($row = $stmt->fetch()) : ?> <option value="<?php echo $supplierbrand; if($_post['county'] == $supplierbrand) {echo "selected";} ?>" width="100px"><?php echo $supplierbrand; ?></option> <?php endwhile; ?> <?php } ?> i added code if($_post['county'] == $supplierbrand) {echo "selected";} try , retain selected value onsubmit. code in context not work. after debugging noticed county value undefined. error chrome debugging:
notice</b>: undefined index: county how value county on page , further more above php script value $_post['supplier'] passed use of jquery.
below html code:
<div id="county_drop_down"> <select id="county" name="county" > <option value="" width="100px">supplier...</option> </select> </div> <span id="loading_county_drop_down"> <img src="css/loader.gif" width="16" height="16" align="absmiddle"> loading...</span> <div id="no_county_drop_down">no lens type/design has been selected.</div> for javascript, please see link
loads of issues in code here's few of them fix first.
value="<?php echo $supplierbrand; if($_post['county'] == $supplierbrand) {echo "selected";} ?>"
will result in value="(int) selected" invalid html should value="<?php echo $supplierbrand;?>" <?php echo ($_post['county'] == $supplierbrand) ? "selected=\"selected\"" : "" ; ?> remember valid html selected="selected" , checked="checked" etc.
you're missing select open in code above assume created else where?
your country value being posted in page need st ore locally , pass through page when reposts self. use hidden fields if $_post['country'] incoming page on page use <input type="hidden" name="country" id="country" value="<?php echo $_post['country'];?>" /> make sure received country previous page , repopulates field whatever inbound previous on repost.
then if statement above work fine $_post['country'] exist.
finally pointed out isset() should lower case isset()
are using custom db class ? looks mysqli or pdo prepare lists looks wrong should not $mysqli->prepare()? since you're using prepare should doing param binding rather injecting $_post['supplier'] prepare @ moment way of doing makes prepare kinda pointless , makes vulnerable sql injection etc.
Comments
Post a Comment