如何捕捉post变量时,我张贴我的提交表单PHP ajax


how to catch post variable when i post my submit form php ajax

我在这个论坛上发帖,因为我没有设法收到我的信息,我通过我的提交表单发布到我的showidz.php页面。我不知道如何捕捉我的$_POST["numversion"]生成我的ajax脚本在我的docversion.php

总而言之,我有一个表单页面docversion.php,我在其中输入文档名称,并在同一页面上捕获通过使用ajax脚本输入该文档可能的"链接"版本。这很好。

我的问题是当我点击提交以抛出从docversion.phpshowidz.php的信息时,我无法捕获numversion。

这是我的源代码: docversion.php

<script type='text/javascript'>
  function getXhr(){
    var xhr = null; 
    if(window.XMLHttpRequest) // Firefox 
      xhr = new XMLHttpRequest(); 
    else 
      if(window.ActiveXObject) { // Internet Explorer 
        try {
          xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
      else { // XMLHttpRequest non supporté par le navigateur 
        alert("Browser not compatible with XMLHTTPRequest..."); 
        xhr = false; 
      } 
    return xhr;
  }
/**
* catch on click
*/
function go(){
  var xhr = getXhr();
  // do when we have the answer
  xhr.onreadystatechange = function(){
    // do if the server answer is OK
    if(xhr.readyState == 4 && xhr.status == 200){
      leselect = xhr.responseText;
      // use innerHTML
      document.getElementById('numeroversion').innerHTML = leselect;
    }
  }
  // post to rep_PhpAjax.php to have version
  xhr.open("POST","rep_PhpAjax.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  sel = document.getElementById('course');
  iddocument = sel.value;
  xhr.send("idDoc="+iddocument);
}
</script>
<form name="test1" method="post" action="showidz.php" >
  Nom du document <label>:</label><br>
  <input type="text" name="course" id="course" size="40" onclick='go()'/><br/>
  <label>Version</label>
  <div id='numeroversion' style='display:inline'>
    <select name='numeroversion'>
      <option value='-1'>Choose a version</option>
    </select>
  </div>
  <input type="submit" name="OK" value="OK">
</form>

rep_PhpAjax.php

<?php
  echo "<select name='numeroversion'>";
  if(isset($_POST["idDoc"])){
    $res = mysql_query("SELECT `NumeroVersion` 
                        FROM `version`, document 
                        WHERE document.idversion = version.idversion 
                           and document.NomDocument ='".$_POST["idDoc"]."'");
    while($row = mysql_fetch_assoc($res)){
      echo "<option value='".$row["idversion"]."'>".$row["NumeroVersion"]."</option>";
    }
  }
  echo "</select>";
?>

showidz.php:有问题的页面,我不能有数字版本已发布的docversion.php:

<?php
  $docname = $_POST["course"];
  $idversion = $_POST["numeroversion"];
  echo "$docname</br>";
  echo $idversion;
?>

希望有人能帮我解决这个问题。

发送到showidz.php的表单数据似乎没有问题但问题可能是你在点击时调用ajax。也许你应该修改

onclick="go();"

onkeyup="go();"
在docversion.php中的第54行

,以便每次键入字母

时调用ajax。

我用jQuery重写了您想要做的事情。你可以在这里看到它的运行。

文件夹结构:

site -+- displayDocument.php
      |- ajaxGetVersions.php
      |- ajaxGetVDocument.php
      |- queries.php

displayDocument.php:

<?php
require_once 'queries.php';
$documents = getDocuments();
?>
<form id="myform" method="post" action="" >
    <label for="documents">Choose a document</label>
    <select id="documents" name='documents[]'>
        <option value='0'>Choose a document</option>
        <?php foreach ($documents as $document) : ?>
            <option value='<?php echo $document ?>'><?php echo $document ?></option>
        <?php endforeach; ?>
    </select>
    <br/>
    <label for="versions">Version </label><span id="refreshVersions"></span>
    <select id="versions" name='versions[]'>
    </select>
    <br/>
    <input type="submit" name="OK" value="OK">
</form>
<div id="refreshDocument"></div>
<div id="document"></div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
    $("#documents").change(function() {
        $("#refreshVersions").text("refreshing...");
        $.post("ajaxGetVersions.php", { documentId: $(this).val() },
        function(data) {
            $("#versions").html(data);
            $("#refreshVersions").text("");
        });
    });
    $("#myform").submit(function(e) {
        $("#refreshDocument").text("refreshing...");
        $.post("ajaxGetDocument.php", { documentId: $("#documents").val(), version: $("#versions").val() },
        function(data) {
            $("#document").html(data);
            $("#refreshDocument").text("");
        });
        e.preventDefault();
    });
</script>

ajaxGetVersions:

<?php
require_once 'queries.php';
if (!isset($_POST['documentId'])) {
    die('missing post parameter: documentId');
}
$versions = getVersionsOfDocument($_POST['documentId']);
?>
<?php foreach ($versions as $version): ?>
    <option value='<?php echo $version ?>'><?php echo $version ?></option>
<?php endforeach; ?>

ajaxGetDocument:

if (!isset($_POST['documentId']) || !isset($_POST['version'])) {
    die('missing post parameter: documentId or version');
}
$doc = getDocument($_POST['documentId'], $_POST['version']);
?>
<h1><?php echo $doc["documentId"] ?></h1>
<h2><?php echo $doc["version"] ?></h2>
<h3><?php echo $doc["author"] ?></h3>
<p>
    <?php echo $doc["content"] ?>
</p>

queries.php:

<?php
// little database replace
$documents = array("Book1" => array("beta", "1.0", "1.1", "2.0"), "Book2" => array("1.0", "1.1"), "Book3" => array("beta"));
function getVersionsOfDocument($documentId) {
    // replace with database fetch
    global $documents;
    return $documents[$documentId];
}
function getDocuments() {
    // replace with database fetch
    global $documents;
    return array_keys($documents);
}
// get a document by id and version
function getDocument($documentId, $version) {
    //implement your own
    return array("documentId" => $documentId,
        "version" => $version,
        "author" => "...",
        "content" => "bla bla");
}