【问题标题】:JQuery .load() or .ajax() or .post() for form data to php functionJQuery .load() 或 .ajax() 或 .post() 用于将表单数据发送到 php 函数
【发布时间】:2013-01-31 22:49:41
【问题描述】:

尝试使用 JQuery 的功能之一来实现 AJAX。我一直在尝试 .load()、.ajax() 或 .post() 以各种方式,通过使用 Stack Overflow 上的一些示例但没有成功。

我有一个表单,它查询 oracle DB,并返回另一个表单和一个结果表。我让它以传统的方式使用单独的 PHP 文件(重新加载一个全新的页面)。现在我只想加载内容而不刷新页面。

启动表单 PHP (checkin_start.php):输入条形码并提交……

<div class="content" id="bodyContent">
  <form id="usualValidate" class="mainForm" method="post" action="">
    <label>Barcode:<span>*</span></label>
    <input type="text" class="required" name="startBarcode" id="startBarcode"/>
    <input type="submit" value="submit" class="blueBtn" id="startBtn" />
  </form>
</div>

处理 PHP (checkin_process.php):一个新的表单和来自 php 函数的查询结果被加载到 id="bodyContent"...

<form id="checkinProcess" class="mainForm" method="post" action="">
  <label>Barcode:<span>*</span></label>
  <input type="text" class="required" name="processBarocdes" id="processBarcodes"/>
  <input type="submit" value="submit" class="blueBtn" id="submitBtn" />
</form>
<!-- Shipment List Table -->
<?php
  // Accept a parameter from #usualValidate form and run query.                 
  $barcode = $_POST['startbarcode'];    
  search_shipped_barcode($barcode);                         
?>

Functions PHP (functions.php):返回结果表……

<?php 
function search_shipped_barcode($barcode){
<--! DB connection & query -->  
  echo "<table>";
    <--! echo table results -->
  echo "</table>";
}
?>

我想无论我选择哪一个,我都可能遇到同样的问题,所以这是我的 .post() 尝试。我不太明白如何将表单数据传递给我的 php 函数并将数据返回...

$(document).ready(function() {
  $("#usualValidate").submit(function(sevt) {
    sevt.preventDefault();
    var startBC = $("#startBarcode").val();
    $.post("checkin_process.php",
          {"startBarcode" : "startBC"},
          function(data) {$("#bodyContent").html(data)},
          "html");
  });
});

感谢您的任何见解......

【问题讨论】:

  • 在您的示例中,删除“startBC”周围的引号:'startBarcode': startBC。您当前正在传递字符串“startBC”而不是变量的值。

标签: php jquery ajax .post


【解决方案1】:

当您使用 $.post 时,除非它们是文字,否则不应引用这些值。

试试这个:

$(document).ready(function() {
  $("#usualValidate").submit(function(sevt) {
    sevt.preventDefault();
    var startBC = $("#startBarcode").val();
    $.post("checkin_process.php",
          {startBarcode : startBC},
          function(data) {
            $("#bodyContent").html(data);
           // log the returned data to console for debug 
           console.log(data);
        });
  });
});

【讨论】:

  • 嗯, 不应该 - 我不认为这是键的问题?
  • 如果它们是变量,则不应引用这些值。在示例中,startBC 的值是一个变量。
  • @user2023235。谢谢,这有助于传递变量,我现在可以在 firebug 中看到它。现在它指出了我的下一个问题,checkin_process.php 只是内容,我没有引用 function.php。将 require once functions.php 添加到 checkin_process.php 会导致 oci_parse 错误。任何人都知道如何在没有标题的情况下引用它?
  • 我想我知道问题所在。我将重新发布为新问题。
  • 我已经发布了一个与之相关的quesiton,您可以查看一下吗?
【解决方案2】:

在您的 javascript 文件中,您可以使用post 方法,也可以使用ajax 发送数据,如下例所示:

$.ajax({
    type : 'post',
    url : 'currentphpfile.php',
    //Here is where the data is put
    data : {
        "ajax" : "1",
        "otherData" : "abc"
    },
    success : function(data, textStatus, jqXHR) {
        //Do something with the data here like insert it into the document
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        //Do something to fix the error
    },
    dataType : 'text' //Or automatically parse a json response with 'json'
});

为此,您需要在您的 php 文件中添加一些可以像这样处理请求的内容:

if (!empty($_POST['ajax']) && $_POST['ajax']==1){
    //Do stuff with the other post data
    //Here's how to return the data to your script:
    //If you chose a text response above:
    echo $importantData;
    exit;
    //If you chose json
    echo json_encode(array("title" => $title, "text" => $text /*, etc... */));
}

我还没有测试过这段代码的错误,但你可能明白了。

【讨论】:

  • 谢谢。阅读 json 看看这是否是一个更好的选择。
猜你喜欢
  • 1970-01-01
  • 2017-04-23
  • 2015-06-14
  • 1970-01-01
  • 2018-04-10
  • 2014-04-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多