javascript - Getting form data from both dependent drop down lists to php -
i have form on page includes 2 dependent drop downwards lists. when user selects value 1st list, populates sec list , user selects value 2nd list.
i want submit form info php page insert table in mysql, when submits, info passed except value 2nd list. value 1st list , other input fields passed ok. i've tried know , can't create work. ideas how implement this?
this form index2.php (edit: simplified form element):
<form name="part_add" method="post" action="../includes/insertpart.php" id="part_add"> <label for="parts">choose part</label> <select name="part_cat" id="part_cat"> <?php while($row = mysqli_fetch_array($query_parts)):?> <option value="<?php echo $row['part_id'];?>"> <?php echo $row['part_name'];?> </option> <?php endwhile;?> </select> <br/> <label>p/n</label> <select name="pn_cat" id="pn_cat"></select> <br/> <input type="text" id="manufactured" name="manufactured" value="" placeholder="manufactured" /> <input id="submit_data" type="submit" name="submit_data" value="submit" /> </form>
and javascript:
$(document).ready(function() { $("#part_cat").change(function() { $(this).after('<div id="loader"><img src="img/loading.gif" alt="loading part number" /></div>'); $.get('../includes/loadpn.php?part_cat=' + $(this).val(), function(data) { $("#pn_cat").html(data); $('#loader').slideup(200, function() { $(this).remove(); }); }); }); });
and php load 2nd list:
<?php include('db_connect.php'); // connects db $con=mysqli_connect(db_host,db_user,db_pass,db_name); $part_cat = $_get['part_cat']; $query = mysqli_query($con, "select * pn pn_categoryid = {$part_cat}"); while($row = mysqli_fetch_array($query)) { echo "<option value='$row[part_id]'>$row[pn_name]</option>"; } ?>
i getting $part_cat 1st list insertpart.php, $pn_cat.
edit: insertpart.php (simplified , echos resuls)
<?php //start session session_start(); //include database connection details require_once('../includes/db_details.php'); //db connect $con=mysqli_connect(db_host,db_user,db_pass,db_name); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } // escape variables security // find part name based on id $part_typeid = mysqli_real_escape_string($con, $_post['part_cat']); $part_name_result = mysqli_query($con, "select part_name parts part_id = $part_typeid"); $part_row = mysqli_fetch_array($part_name_result, mysql_num); $part_type = $part_row[0]; echo"part_type='$part_type'"; //find pn value based on id $pn_typeid = mysqli_real_escape_string($con, $_get['pn_cat']); $pn_name_result = mysqli_query($con, "select pn_name pn pn_id = $pn_typeid"); $pn_row = mysqli_fetch_array($pn_name_result, mysql_num); $pn = $pn_row[0]; echo"pn='$pn'"; mysqli_close($con); ?>
it's still work in progress, code ugly, , know i'm mixing post , beingness rectified. if echo $pn_cat on page there no output, $part_type ok.
can seek swapping $_get
in
$pn_typeid = mysqli_real_escape_string($con, $_get['pn_cat']);
with $_post
?
$pn_typeid = mysqli_real_escape_string($con, $_post['pn_cat']);
edit: based on asker's feedback , thought work-around
note: edit based on suggested, though tested original code , received satisfactory results (after removed php , mysql code , replaced them suitable alternatives).
the work-around
here's html hidden field:
<input type="hidden" id="test" name="test" value="" placeholder="test" />
here's simple javascript function:
function sethiddentextfieldvalue(initiator, target){ $(initiator).change(function() { $(target).val($(this).val()); }); }
you can phone call above function within function(data) {
of original code like:
sethiddentextfieldvalue('#pn_cat', '#test'); // note hashes (#)
i recommend hard-code next html html , php files, right before looping of <option>
s begin:
<option value="" disabled selected="selected">select</option>
the above line improve user experience, depending on how want code work. note however, exclusively optional.
javascript php jquery mysql
No comments:
Post a Comment