在同一页面上的另一个选项卡上运行php文件而不提交表单值

时间:2015-11-11 08:09:06

标签: javascript php mysql ajax

我有一个表单,一旦用户输入并单击一个按钮,另一个选项卡就会显示在同一页面上。在此选项卡中,表单输入中有一些值(设法执行此操作)。我还想从mysql数据库中检索一些数据并将其显示在这个新选项卡上。检索到的数据也基于表单输入。我在网上搜了两天多,但找不到解决办法。任何和所有的帮助表示赞赏。

下面是:我的表单代码(在php文件中); javascript(用于新选项卡)和php脚本(用于检索数据)。

表格代码

<div class="tab-content">
  <fieldset class="tab-pane active" id="form_tab">
      <form id="poform">
         <table>
               <tr>  
                    <th colspan="2"><div class="header_3">Pre-Order Form</div></th>
               </tr>
               <tr>  
                    <td><label>Account Number:</label></td>
                    <td><input type="text" name="accountnumber" id="accountnumber" value="" required/></td>
               </tr>
               <tr>  
                    <td><label>Trade:</label></td>
                    <span class="text_11">
                    <td><input type="radio" id="Buy" name="tradetype" class="tradetype" required value="Buy"/> Buy
                    <input type="radio" id="Sell" name="tradetype" class="tradetype" value="Sell"/> Sell </span></td>
               </tr>
               <tr>  
                    <td><label>Metal:</label></td>
                    <span class="text_11">
                    <td><input type="radio" id="Steel" name="metal" class="metal" required value="Steel"/> Steel
                    <input type="radio" id="Iron" name="metal" class="metal" value="Iron"/> Iron </span></td>
               </tr>
               <tr>  
                    <td class="select"><label>Amount:</label></td>
                    <td><select id="amount" name="amount">
                    <option value="">Select</option>
                    <?php include_once "selectamount.php"?></td>
                    </select>
               </tr>
               <tr>  
                    <td class="select"><label>Date:</label></td>
                    <td><select id="date" name="date" id="date">
                    <option value="">Select</option>
                    <?php include_once "selectdate.php"?></td>
                    </select>
               </tr> 
               <tr> 
                    <td colspan="2" align="center">
                    <input value="Check" type="button" id="submit_btn"/>
                    </button>
                    </td>
               </tr>
         </table>
      </form>
  </fieldset>

  <fieldset class="tab-pane" id="conf_tab">
    <table>
        <tr style="text-align:center">
            <th colspan="2"><div class="header_3">Pre-Order Ticket Details</div></th>
        </tr>
        <tr style="text-align:left">
            <td><label>Account Number:</label></td>
            <td><mgin><span id="confirm_accountnumber"></span></mgin></td>
        </tr>
        <tr style="text-align:left">
            <td><label>Trade Pre-Order:</label></td>
            <td><mgin><span id="confirm_tradetype"></span></mgin></td>
        </tr>
        <tr>
            <td><label>Amount:</label></td>
            <td><mgin><span id="confirm_amount"></span> 9999 Pooled Allocated <span id="confirm_metal"></span> Loco Singapore</mgin></td>
        </tr>
        <tr>
            <td><label>On date:</label></td>
            <td><mgin><span id="confirm_date"></span></mgin></td>
        </tr>
            <td><label>Pre-Order Discount:</label></td>
            <td><mgin><span id="confirm_data"></span></mgin></td>
        </tr>
        <tr>
            <td><label>Pre-Order Deposit:</label></td>
            <td><mgin>SGD 5000.00</mgin></td>
        </tr>
        <tr>
            <td><label>Additional follow up order:</label></td>
            <td><mgin>New Sell Order to be given 2 days before purchase order date.</mgin></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><button type="submit" class="btn btn-default" id="confirm_btn">Confirm Order</button></td>
        </tr>       
    </table>
  </fieldset>
</div>

使用Javascript:

$(document).ready(function() {
  $('#submit_btn').click(function() {

    var accountnumber = $('#accountnumber').val();
    var tradetype = $('input.tradetype:checked').val();
    var amount = $('#amount').val();
    var metal = $('input.metal:checked').val();
    var date = $('#date').val();
    var data = "<?php include 'retrievepremordisc.php'; ?>";

    $('#confirm_accountnumber').text(accountnumber);
    $('#confirm_tradetype').text(tradetype);
    $('#confirm_amount').text(amount);
    $('#confirm_metal').text(metal);
    $('#confirm_date').text(date);
    $('#confirm_data').text(data);

    // Hide form tab and show confirmation tab
    $('#form_tab').removeClass('active');
    $('#conf_tab').addClass('active');

  });
});

PHP(检索数据)

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

$form=$_GET;
$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 $row['Discount']; 
} 
?>

Ajax尝试

  $.ajax({
      url: 'retrievepremordisc.php',
      success: function (data) {
        alert(data);
      }

1 个答案:

答案 0 :(得分:2)

首先在脚本中删除这两行:

var data = "<?php include 'retrievepremordisc.php'; ?>";
....
$('#confirm_data').text(data);

然后在脚本中添加:

$.ajax({
  type: "POST", /* METHOD TO USE TO PASS THE DATA */
  url: "retrievepremordisc.php", /* THE FILE WHERE YOU PROCESS THE SELECT QUERY */
  data: {"tradetype": tradetype, "amount": amount, "metal": metal, "date":  date}, /* THE DATA WE WILL SUBMIT TO retrievepremordisc.php */
  success: function(discount){ /* discount IS THE RETURNED DATA FROM retrievepremordisc.php */
    $("#confirm_data").text(discount); /* PUT THE RESULT OR discount TO span HTML ELEMENTS WITH ID OF confirm_data */
  }
});

然后在retrievepremordisc.php

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

/* HERE IS YOUR SELECT QUERY */
echo $discount; /* WHAT YOU ECHO IS WHAT WILL BE RETURNED TO YOUR MAIN FILE */

如果事情失败,请确保查找错误。只需在浏览器上点击F12,然后查看控制台是否有错误。