0

我似乎找不到将跨度值插入数据库的正确解决方案。或者是否有任何其他选项可以通过从下拉列表中选择来显示数据库中的值?这是我的代码。

这是我的 javascript 获取从下拉列表中选择的值。

<script type="text/javascript"> 
function showUser(userNumber, str) 
{ 

if (str=="") 
{ 
  document.getElementById("value" + userNumber).innerHTML=""; 
  return; 
}   
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
  xmlhttp=new XMLHttpRequest(); 
} 

xmlhttp.onreadystatechange=function() 
{ 
  if (xmlhttp.readyState==4 && xmlhttp.status==200) 
  { 
    //document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText;
    var responseText = xmlhttp.responseText;
    var coron = responseText;
    var meas = "";
    var unit ="";

    if (responseText.indexOf("NOT A VALID") == -1)
    {
      coron = responseText.substring(12, responseText.indexOf(",Value:")); 
      meas = responseText.substring(responseText.indexOf(",Value:")+7,      responseText.indexOf(",Weight:")); 
      unit = responseText.substring(responseText.indexOf(",Weight:")+8);

    }

    document.getElementById("value" + userNumber).innerHTML = coron;
    document.getElementById("meas" + userNumber).innerHTML = meas; 
    document.getElementById("unit" + userNumber).innerHTML = unit;
  } 
} 
xmlhttp.open("GET","coron.php?q="+str,true); 
xmlhttp.send();
}
</script>

这是我的表格。

<form name="mainform" method="post" action="submit.php">
<table align = "center" border = "1" ><tr align = "center"><td class = "laman">Qty.
    <td class = "laman">Units
    <td class = "description">Desciprtion of Articles
    <td class = "laman">Value
    <td class = "laman">Meas/Weight
    <td class = "laman">Freight Charge</tr>
    <tr align = "center"><td class = "laman"><input size = "5" type = "text" name = "qty" onkeyup = "calculateTotal()" />
    <td class = "laman"><span id="unit1"></span>
    <td class = "description"><? 
                                $con = mysql_connect("localhost","root","") or die(mysql_error());
                                mysql_select_db("ler_marineland", $con) or die(mysql_error());

                                $sql="SELECT product_id, description FROM product"; 
                                $result=mysql_query($sql); 

                                $options=""; 


                                while ($row=mysql_fetch_array($result)) { 

                                    $product_id=$row["product_id"]; 
                                    $description=$row["description"]; 
                                    $options.="<OPTION VALUE=\"$product_id\">".$description.'</option>'; 

                                } 
                                ?> 

                                <SELECT id=myselect onchange="showUser(1, this.value);"> 
                                <OPTION VALUE=0>
                                <?=$options?>
                                </SELECT>

    <td class = "laman"><span id="value1" name="value"></span>
    <td class = "laman"><span id="meas1" name="meas"></span>
    <td class = "laman"><span id="fc" name="fc"></span></tr>
            <tr><td>Voyage No. <td><input size = "8" type = "text" name = "voyage_no" />

PHP

$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("ler_marineland", $con) or die(mysql_error());


            $qty= $_POST['qty'];
            $units= $_POST['units'];
            $description= $_POST['description'];
            $value= $_POST['value'];
            $meas= $_POST['meas'];
            $fcharge= $_POST['fc'];
                            $voyage_no= $_POST['voyage_no'];


$sql = 'SELECT * FROM coron WHERE voyage_no = "'.mysql_real_escape_string($voyage_no).'"'; 

    mysql_query("INSERT INTO coron (voyage_no, qty, units, description, value, meas, fc) VALUES ('$voyage_no', '$qty', '$units', '$description', '$value', '$meas', '$fc')") or die(mysql_error());

这就是我想要做的,从填充的下拉菜单中选择一个项目,然后选择后,它将自动显示其相应的值、度量和单位。我找到了适合我的项目的代码,然后想出了那个 javascript 代码。并发现代码仅使用 span 或 div 显示。但据说,我真的想把它显示到一个文本框中。现在,我想通过将它们提交到数据库来存储它们,但我似乎不知道如何。有没有其他方法可以通过从下拉列表中选择来自动获取相应的值、度量和单位?请帮帮我。我对此真的很陌生。提前致谢!

4

0 回答 0