douxie7738 2014-02-12 01:37 采纳率: 0%
浏览 28
已采纳

使用Ajax发布值

I'm trying to post the value of input name="sub2" to process my query in my input="sub3". I used Ajax but the input name="sub3" still empty. The value of sub2 is came from dropdown option text, if option text value is Supplies It get only the 3 first letters of supplies, so now the value of sub2 is "SUP". That value what I need to post to function my query in my sub3 textbox. Help please?

This is fiddle example http://jsfiddle.net/xqGLS/3/

Code.php

<?php
    $resultcode = $mysqli->query("SELECT category, id, maincode FROM category GROUP BY id ORDER BY maincode");
    $code = '';
     while($row = $resultcode->fetch_assoc())
        {
        $code .= '<option value = "'.$row['maincode'].'">'.$row['category'].'</option>';
        }   
?>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
<br/>

<label>Category</label>
<select name="maincode" style="text-transform:uppercase;" onchange = "GetChangedValue(this);">
<option value=""></option>
<?php echo $code; ?>
</select>

</br>

<label>Sub1</label>
<input type="" name="sub1" id="sub1" value="" readonly style="width:45px;text-transform:uppercase;">

<script>
$('[name="maincode"]').change(function() {
   $('[name="sub1"]').val($(this).val());
   var input = $('[name="sub2"],[name="sub3"]'),
    input1 = $('[name="sub2"]'),
    input2 = $('[name="sub3"]'),
    input3 = $('[name="equal"]');
    input.change(function () {
    input3.val(input1.val() + input2.val());
});
});
</script>

<script>
function GetChangedValue(e) {
var value = e.options[e.selectedIndex].text;
var elem = document.getElementById("sub2"); elem.value = value.substring(0,3);
}
</script>

<label>Sub2</label>
<input name="sub2" id="sub2" value="" style="width:35px;text-transform:uppercase;" readonly>

<label>Sub3</label>
<input id="sub3" name="sub3" style="width:35px;text-transform:uppercase;" value=''>

<script type="text/javascript">
$('#sub2').change(function(){
$.ajax({
url : 'check.php',
data :{mainlist_id : $(this).val()},
dataType:'html',
type:'POST',
success:function(data){
$('#sub3').html(data);
}
});
});
</script>

<input id="equal" name="equal" value="" style="width:60px;text-transform:uppercase;" type="hidden">
<input type="submit" name="">
</form>

Check.php

<?php
    $mysqli = new mysqli("localhost", "root", "", "2015");
    $sub = $mysqli->real_escape_string($_POST["mainlist_id"]);

$result2 = $mysqli->query("SELECT * FROM code
WHERE sub1code LIKE '$sub-___' ORDER BY sub1code");

while($row = $result2->fetch_assoc())
  {
  $value = $row['sub1code'];
  }
  $first = substr($value, 0, 4);
  echo $first;
  $last = substr($value, -3);
  $i="0";
  while($i<=$last)
  {
  $i++;
  }
  $value2=strlen($i);
    echo $first;
    if($value2==1)
    {
    echo "00".$i;
    }
    elseif($value2==2)
    {
    echo "0".$i;
    }
    else
    {
    echo $i;
    }
    ?>
  • 写回答

2条回答 默认 最新

  • dongxie1907 2014-02-12 08:50
    关注

    I think you want something like this. Just a little change on your code but it should work.

    Code.php

    <script type="text/javascript">
    function GetChangedValue(str) {
        if (str==""){
            document.getElementById("sub3").innerHTML="";
            return;
        }
        if (window.XMLHttpRequest)   {
            // code for IE7+, Firefox, Chrome, Opera, Safari   
            xmlhttp=new XMLHttpRequest();  
        } else {
            // code for IE6, IE5   
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        } 
        xmlhttp.onreadystatechange=function(){   
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("sub3").value = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","check.php?q="+str,true);
        xmlhttp.send();
    }
    </script>
    
    <select name="category" id="category" onchange="GetChangedValue(this.value);">
    <option value=""></option>
    <?php
    $mysqli = new mysqli("localhost", "root", "", "2015");
    $result2 = $mysqli->query("SELECT * FROM category ORDER BY maincode");
    while($row = $result2->fetch_assoc())
    {
    $ok = $row['maincode'];
    echo "<option value=".$row['maincode'].">".$row['category']."</option>";
    }                               
    ?>
    </select>
    
    
    <input type="" name="sub3" id="sub3" value="" readonly style="width:85px;text-transform:uppercase;">
    

    Check.php

    <?php
    if (isset($_GET["q"])) {
    $q=$_GET["q"];
    $mysqli = new mysqli("localhost", "root", "", "2015");
    
    $sub = substr($q, 0, 4);
    
    $result2 = $mysqli->query("SELECT * FROM code
    WHERE sub1code LIKE '$sub-___' ORDER BY sub1code");
    
    while($row = $result2->fetch_assoc())
      {
      $value = $row['sub1code'];
      }
      //$first = substr($value, 0, 3);
      echo $sub;
      $last = substr($value, -3);
      $i="0";
      while($i<=$last)
      {
      $i++;
      }
    $value2=strlen($i);
    if($value2==1)
    {
    echo "-"."00".$i;
    }
    elseif($value2==2)
    {
    echo "-"."0".$i;
    }
    else
    {
    echo "-".$i;
    }
    }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向