根据表单输入从数据库中检索数据并显示在同一页面上

时间:2015-10-30 04:44:01

标签: javascript php html pdo

我有这个表单,有两个单选按钮选择和两个下拉菜单。一旦选择了最后一个下拉菜单,我想根据输入的选择检索数据,然后显示在同一页面上?以下是我的代码...请建议谢谢!

<fieldset id="tradetype">
    <span class="text_9">TRADE:</span>
    <span class="text_11">
    <input type="radio" name="tradetype" required value="Buy"/> Buy
    <input type="radio" name="tradetype" value="Sell"/> Sell </span>
</fieldset>
<fieldset id="Metal">
    <span class="text_9">METAL:</span>
    <span class="text_11">
    <input type="radio" name="metal" required value="Two"/> Two
    <input type="radio" name="metal" value="One"/> one </span>
</fieldset>
<fieldset id="Amount">
    <label><span class="text_9">AMOUNT:</span></label>
    <select name="amount">
        <option value="">Select</option>
        <?php include_once "selectamount.php"?>
    </select>
</fieldset>
<fieldset id="Date">
    <label><span class="text_9">DATE:</span></label>
    <select name="date" id="date" onchange="showData(this.Value)">
        <option value="">Select</option>
        <?php include_once "selectdate.php"?>
    </select>
</fieldset>
<input type="submit" name="formSubmit" value="Submit">
<div id="txtHint"></div>

的Javascript

        function showData(str) {
  if (str=="") {
    document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","retrievepremordisc.php?q="+str,true);
  xmlhttp.send();
}

PHP(使用PDO)

    <?php
include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 

$form=$_POST;
$trade=$form['tradetype'];
$metal=$form['metal'];
$amount=$form['amount'];
$date=$form['date'];

$stmt = $conn->query("SELECT Discount FROM Contracts WHERE Trade=:trade AND Metal=:metal AND Amount=:amount AND ExpiryDate=:date");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
   echo "<option value='" .  $row['Discount']. "'>" . $row['Discount'] . "</option>"; 
}
?>

好的问题是,当我更改日期时没有显示!

0 个答案:

没有答案