I have a big dilemma, I'm generating data from MySQL into a form so, the problem is that while the first row generated by SQL is 0 the select option in the form is interpreting the first value as 1. Would not be any problem if I wouldn't wanted to throw out some data from within this Query. But here is the code:
<select id="exp_address" name="exp_address" onChange="document.getElementById('exp_city').value = this.options[this.selectedIndex].getAttribute('data-exp_city'); document.getElementById('exp_dist').value = this.options[this.selectedIndex].getAttribute('data-exp_distr'); document.getElementById('exp_address_val').value = this.options[this.selectedIndex].getAttribute('data-exp_city_val')">
<?php
for($i = 0; $i < $expselnum; $i++){
$exps_add_id = mysql_result($expsel,$i,'address_id');
$exps_address = mysql_result($expsel,$i, "address");
$exps_city = mysql_result($expsel,$i, "city");
$exps_distr_val = mysql_result($expsel,$i, "district");
echo "<option value=$exps_add_id data-num=$i data-exp_city = '$exps_city' data-exp_distr = '$exps_distr_val' data_exp_city_val = '$exps_city'>$exps_address</option>";
}
?>
</select>
and here is the HTML code generated:
<select onChange="Here is some JS to change the value of District and City inputs">
<option value=1 data-num=0 data-exp_city = 'BUCURESTI' data-exp_distr = 'Bucuresti' data_exp_city_val = 'BUCURESTI' [selected by default]>Another Address in RO</option><option value=2 data-num=1 data-exp_city = 'OTOPENI-IF' data-exp_distr = 'Ilfov' data_exp_city_val = 'OTOPENI-IF'>Some address in RO</option></select>
<input name="exp_city" type="text" id="exp_city" style="text-align:left;" value="OTOPENI-IF" size="30" readonly="readonly">
If you compare the value and data-num att in the option tag, you'll s开发者_开发知识库ee that they are different by an unit, WHY. The first value of option is selected on page landing, but the city input shows the next value from the query. How can I work this around? Thank you guys in advance.
Although it's not stated very subtle, I agree with Shrapnel's comment. However, this might fix your 'problem' (I don't really understand the question, though):
for($i = 0; $i < $expselnum; $i++){
change to:
for($i = 1; $i < $expselnum; $i++){
精彩评论