1

我有一个 ajax 下拉框,如果选中它会在表单中添加另一个字段,这可以工作,但是这个新字段不会随表单一起提交。有什么建议么?

findTest.php 通过 ajax 调用

<?php
$work = ($_GET['work']);
if ($work == "Work1") {
    ?>
    <tr>
        <td>New work1<input name="newWork1" size="20" type="text"/></td>
    </tr> <?
} elseif ($work == "Work2") {
    ?>
    <tr>
        <td>New work<input name="newWork" size="20" type="text"/></td>
    </tr><?
} elseif ($work == "Work3") {
    ?>
    <tr>
        <td>No work today</td>
    </tr><?
}?>

测试.php

<?php
$page = '';
$answer = null; //clean start.
$Comments = $_POST['Comments'];
$new = $_POST['new'];

// Check if button name "Submit" is active, do this
if (isset($_POST['Submit'])) {
    ?>
    <p align="center" id="infomessage">Test message <?= $_POST['Comments'] ?>
        and <?= $_POST['new'] ?> or this should be
        work2 <?= $_POST['newWork'] ?></p>
<? } ?>
<script language="javascript" type="text/javascript">
    // Roshan's Ajax dropdown code with php
    // This notice must stay intact for legal use
    // Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
    // If you have any problem contact me at http://roshanbh.com.np
    function getXMLHTTP() { //function to return the xml http object
        var xmlhttp = false;
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e1) {
                    xmlhttp = false;
                }
            }
        }
        return xmlhttp;
    }

    function getWorkDone(workId) {
        var strURL = "findTest.php?work=" + workId;
        var req = getXMLHTTP();
        if (req) {
            req.onreadystatechange = function () {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {
                        document.getElementById('workdiv').innerHTML = req.responseText;
                    } else {
                        alert("1 There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }
            }
            req.open("GET", strURL, true);
            req.send(null);
        }
    }
</script>

<table id="meters" width="800" border="0" cellspacing=0 cellpadding=2>
    <form name="meters" method="POST" action="test.php">
        <tr>
            <td width="200" align="right">Work Done:</td>
            <td><select name="WorkDone" tabindex="14"
                        onchange="getWorkDone(this.value)">
                    <option value="Work1">Work1</option>
                    <option value="Work2">Work2</option>
                    <option value="Work3">Work3</option>
                    <option value="Other">Other</option>
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><p id="workdiv">New Work<input name="newWork" size="20"
                                               type="text"/></td>
        </tr>
        <tr>
            <td width="200" align="right">Comments:</td>
            <td><textarea name="Comments" tabindex="40" cols="40"
                          rows="4"><?= $_POST[Comments] ?></textarea></td>
        </tr>
        <tr>
            <td colspan="7" align="center"><input type="submit" name="Submit"
                                                  class="button" value="Submit">
            </td>
        </tr>
        </tr>
</table></form>

更新:新代码。

findTest.php 通过 ajax 调用

<?php
if (isset($_GET['work'])) {
$work= $_GET['work']; 
if ($work == "Noworktoday") {
echo "<tr><td>No work today</td></tr>";
}  else {
echo "<tr><td>New ".$work."<br>";
echo "<input name=\"new".$work."\" type=\"text\"";
echo " size=\"20\" value=\"new".$work."\" />";
echo "</td></tr>";
}
}?>

和Test.php

<?php
$page = '';
$answer = null; //clean start.
if (isset($_POST['Comments'])) {
$Comments = $_POST['Comments']; } else {
$Comments = 'n/a'; }

if (isset($_POST['newWork1'])) {
$newWork = $_POST['newWork1']; } else {
$newWork = 'n/a'; }

if (isset($_POST['newWork1'])) {
$newWork = $_POST['newWork1']; } else {
$newWork = 'n/a'; }

if (isset($_POST['Submit'])) {
echo "message = ".$Comments."<br>";
echo "newWork1 = ".$newWork1."<br>";
echo "newWork2 = ".$newWork2."<br>";
echo "newOther = ".$newOther."<br>";
}
?>

<script language="javascript" type="text/javascript">
// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;  
try{
xmlhttp=new XMLHttpRequest();
}
catch(e)    {       
try{            
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}

function getWorkDone(workId) {      
var strURL="findTest.php?work="+workId+"&new1="+"&$new";
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {                        
document.getElementById('workdiv').innerHTML=req.responseText;                      
} else {
alert("1 There was a problem while using XMLHTTP:\n" + req.statusText);
}
}               
}           
req.open("GET", strURL, true);
req.send(null);
}       
}
</script>
<table id="meters"  width="800"  border="0" cellspacing=0 cellpadding=2>
<form name="meters" method="POST" action="test.php">
<tr>
<td width="200" align="right" >Work Done:</td>
<td><select name="WorkDone" tabindex="14" onchange="getWorkDone(this.value)">
<option value="">Select</option>
<option value="Work1">Work1</option>
<option value="Work2">Work2</option>
<option value="Work3">Work3</option>
<option value="Other">Other</option>
</select>
</td>
</tr>
<tr >
<td ></td>
<td ><p id="workdiv">New Work<input name="newWork" size="20" type="text" /></td>
</tr>
<tr>
<td width="200" align="right" >Comments:</td>
<td><textarea name="Comments" tabindex="40" cols="40" rows="4" ><?=$_POST[Comments]?></textarea></td>
</tr>
<tr>
<td colspan="7" align="center"><input type="submit" name="Submit" class="button" value="Submit">
</td>
</tr>
</tr> 
</table>
</form>

UPDATE 编辑加倍并将 $newWork 更正为 $newWork1

<?php
$page = '';
$answer = null; //clean start.
if (isset($_POST['Comments'])) {
$Comments = $_POST['Comments']; } else {
$Comments = 'n/a'; }

if (isset($_POST['newWork1'])) {
$newWork1 = $_POST['newWork1']; } else {
$newWork1 = 'n/a'; }

if (isset($_POST['Submit'])) {
echo "message = ".$Comments."<br>";
echo "newWork1 = ".$newWork1."<br>";
echo "newWork2 = ".$newWork2."<br>";
echo "newOther = ".$newOther."<br>";}
?>

现在显示 n/a 但不显示 newWork1 的值

4

1 回答 1

1

您对原始问题进行了很大的修改,我只能回复以前的内容。


重要的部分也是findTest.php

从这个脚本你替换你的

document.getElementById('workdiv').innerHTML=req.responseText;

那么怎么findTest.php知道$_POST['newwork1']

<td>New work1<input name="newWork1" ... value="<?=$_POST['newwork1']?>" /></td>..

这必须通过 GET / POST 来实现,但在您的 GET 中只有

strURL="findTest.php?work="+workId;

只有 1 个名为 $_GET['work'] 的 GET 变量。

并且您使用此变量(未经测试)

$work=$_GET['work'];

然后你把请求从$_GET$_POST['newwork1']

那么为什么不使用您的选择值将它们组合在一起。

测试.php

...
<select name="WorkDone" tabindex="14" onchange="getWorkDone(this.value)">
<option value="Work1">Work1</option>
<option value="Work2">Work2</option>
<option value="Noworktoday">No work today</option>
<option value="Other">Other</option>
</select>
...

findTest.php

if (isset($_GET['work'])) {
//for example : $work == "Work1"  
$work=$_GET['work']; 
if ($work=="Noworktoday") {
  echo "<tr><td>No work today</td></tr>";
}  else {
  echo "<tr><td>New ".$work."<br>";
  echo "<input name=\"new".$work."\" type=\"text\"";
  echo " size=\"20\" value=\"new".$work."\" />";
  echo "</td></tr>";
}

//<tr><td>New Work1<input name="newWork1" type="text" size="20" value="newWork1" /></td></tr>

更新:

你看不到你的价值观,因为你没有echo
你必须测试所有$_POST

$page = '';
$answer = null; //clean start.
if (isset($_POST['Comments'])) {
 $Comments = $_POST['Comments']; } else {
 $Comments = 'n/a'; 
}

if (isset($_POST['newWork1'])) {
 $newWork1 = $_POST['newWork1']; } else {
 $newWork1 = 'n/a'; 
}
....

对于所有可能的值,依此类推


echo你的变量

if (isset($_POST['Submit'])) {
    ?>
    <p align="center" id="infomessage">Test message = <? echo $Comments; ?>
        and = <? echo $newWork1; ?> or this should be
        work2 = <? echo $newWork; ?></p>
<? } ?>

更新:

我看不到实际代码,你需要一个if (isset ... for all possible variables 。在我的例子中,我只描述了两种可能性。你还需要例如

 if (isset($_POST['newWork2'])) { ...
 if (isset($_POST['newOther'])) { ... 

等等 !!并且只做简单的输出。

if (isset($_POST['Submit'])) {
 echo "message = ".$Comments."<br>";
 echo "newWork1 = ".$newWork1."<br>";
 echo "newWork2 = ".$newWork2."<br>";
 echo "newOther = ".$newOther."<br>";
 }

并将其合乎逻辑地放在一起

这是不合逻辑的Work2 assigned to newWork!!!

于 2013-08-21T03:59:57.350 回答