我正在嘗試發佈輸入名稱=「sub2」的值,以在我的輸入=「sub3」中處理我的查詢。我使用Ajax,但輸入名稱=「sub3」仍爲空。 sub2的值來自下拉選項文本,如果選項文本值爲Supplies它只獲得3個第一個字母的電源,所以現在sub2的值是「SUP」。這個值我需要張貼在我的sub3文本框中運行我的查詢。請幫助?使用Ajax發佈該值
這是小提琴例如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;
}
?>
你得到什麼結果,如果你把一些示例值(S)爲$子在你的PHP的碼。你的SQL似乎不正確 - >'$ sub -____' – bestprogrammerintheworld
@bestprogrammerintheworld我的查詢工作正常,空的____從數據庫中獲取值,如果已經有002它自動在文本框中003。 – user3097736