当我发布我的提交表单php ajax时如何捕获post变量

时间:2011-10-05 08:46:25

标签: php ajax variables post

我在此论坛上发帖,因为我无法收到我通过提交表单发布到showidz.php页面的信息。我不知道如何抓住我$_POST["numversion"]中生成我的ajax脚本的docversion.php

总结一下,我有一个表单页面docversion.php,我在其中输入文档名称,并在同一页面上找到使用ajax脚本输入的本文档可能的“链接”版本。这很好用。

我的问题是,当我点击提交以将信息从docversion.php投放到showidz.php时,我无法识别数字。

这是我的源代码: 的 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发布的numeroversion:

<?php
  $docname = $_POST["course"];
  $idversion = $_POST["numeroversion"];

  echo "$docname</br>";
  echo $idversion;
?>

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

2 个答案:

答案 0 :(得分:0)

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

onclick="go();"

onkeyup="go();"

在第54行的docversion.php中,以便每次键入字母时调用ajax

答案 1 :(得分:0)

我使用jQuery重写了你想要做的事情。您可以在行动here中看到它。

文件夹结构:

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");
}