Php 提交动态ajax字段

Php 提交动态ajax字段,php,ajax,Php,Ajax,我有一个ajax下拉框,如果选中该框,将向表单中添加另一个字段,这会起作用,但这个新字段不会随表单一起提交。有什么建议吗 通过ajax调用findTest.php <?php $work = ($_GET['work']); if ($work == "Work1") { ?> <tr> <td>New work1<input name="newWork1" size="20" type="text"/></t

我有一个ajax下拉框,如果选中该框,将向表单中添加另一个字段,这会起作用,但这个新字段不会随表单一起提交。有什么建议吗

通过ajax调用findTest.php

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

新作品1
新作品
今天没有工作
Test.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>

测试消息
或者这应该是
工作2

//Roshan使用php编写的Ajax下拉代码 //此通知必须保持完整,以便合法使用 //版权归Roshan Bhattarai所有-nepaliboy007@yahoo.com //如果您有任何问题,请联系我http://roshanbh.com.np 函数getXMLHTTP(){//返回xml http对象的函数 var xmlhttp=false; 试一试{ xmlhttp=新的XMLHttpRequest(); } 捕获(e){ 试一试{ xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } 捕获(e){ 试一试{ xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”); } 渔获物(e1){ xmlhttp=false; } } } 返回xmlhttp; } 函数getWorkDone(workId){ var strURL=“findTest.php?work=“+workId; var req=getXMLHTTP(); 如果(请求){ req.onreadystatechange=函数(){ 如果(req.readyState==4){ //只有在“OK”的情况下 如果(请求状态==200){ document.getElementById('workdiv')。innerHTML=req.responseText; }否则{ 警报(“1使用XMLHTTP:\n“+req.statusText时出现问题); } } } 请求打开(“获取”,strURL,true); 请求发送(空); } } 已完成的工作: 工作1 工作2 工作3 其他 新工作 评论:
更新:新代码

通过ajax调用findTest.php

<?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
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>

//Roshan使用php编写的Ajax下拉代码
//此通知必须保持完整,以便合法使用
//版权归Roshan Bhattarai所有-nepaliboy007@yahoo.com
//如果您有任何问题,请联系我http://roshanbh.com.np
函数getXMLHTTP(){//返回xml http对象的函数
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}
返回xmlhttp;
}
函数getWorkDone(workId){
var strURL=“findTest.php?work=“+workId+”&new1=“+”&$new”;
var req=getXMLHTTP();
如果(请求){
req.onreadystatechange=函数(){
如果(req.readyState==4){
//只有在“OK”的情况下
如果(请求状态==200){
document.getElementById('workdiv')。innerHTML=req.responseText;
}否则{
警报(“1使用XMLHTTP:\n“+req.statusText时出现问题);
}
}               
}           
请求打开(“获取”,strURL,true);
请求发送(空);
}       
}
已完成的工作:
挑选
工作1
工作2
工作3
其他
新工作
评论:
更新编辑加倍并将$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的值。您已经对原始问题进行了很大修改,我只能回答之前的问题


重要的部分也是
findTest.php

从这个脚本中,您可以替换

document.getElementById('workdiv').innerHTML=req.responseText;
那么
findTest.php
如何知道
$\u POST['newwork1']

<td>New work1<input name="newWork1" ... value="<?=$_POST['newwork1']?>" /></td>..
只有1个名为$\u GET['work']的GET变量

您使用这个变量(无需测试)

然后您将请求从
$\u GET
混合到
$\u POST['newwork1']

<td>New work1<input name="newWork1" ... value="<?=$_POST['newwork1']?>" /></td>..
那么,为什么不使用select值将其全部组合在一起呢

Test.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>
...
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
您的变量

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($_POST['Submit'])) {
 echo "message = ".$Comments."<br>";
 echo "newWork1 = ".$newWork1."<br>";
 echo "newWork2 = ".$newWork2."<br>";
 echo "newOther = ".$newOther."<br>";
 }
if(isset($\u POST['Submit'])){
echo“message=”.$Comments.“
”; echo“newWork1=”.$newWork1.“
”; echo“newWork2=”.$newWork2.“
”; echo“newOther=”.$newOther.
”; }
把它放在一起是合乎逻辑的

这是分配给新工作的不合逻辑的
Work2

你对原来的问题做了很大的修改,我只能回答以前的问题


重要的部分也是
findTest.php

从这个脚本中,您可以替换

document.getElementById('workdiv').innerHTML=req.responseText;
那么
findTest.php
如何知道
$\u POST['newwork1']

<td>New work1<input name="newWork1" ... value="<?=$_POST['newwork1']?>" /></td>..
只有1个名为$\u GET['work']的GET变量

您使用这个变量(无需测试)

然后您将请求从
$\u GET
混合到
$\u POST['newwork1']

<td>New work1<input name="newWork1" ... value="<?=$_POST['newwork1']?>" /></td>..
那么,为什么不使用select值将其全部组合在一起呢

Test.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>
...
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
您的变量

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($_POST['Submit'])) {
 echo "message = ".$Comments."<br>";
 echo "newWork1 = ".$newWork1."<br>";
 echo "newWork2 = ".$newWork2."<br>";
 echo "newOther = ".$newOther."<br>";
 }
if(isset($\u POST['Submit'])){
echo“message=”.$Comments.“
”; echo“newWork1=”.$newWork1.“
”; echo“newWork2=”.$newWork2.“
”; echo“newOther=”.$newOther.
”; }
把它放在一起是合乎逻辑的


这是分配给新工作的不合逻辑的
Work2

你发布了
findWorkDone.php?work=“+workId;”
,并想用
value=“
”获取它:哪里是
$\u post['new1']
集?整个代码大约有800行,我只发布了我认为相关的内容。对不起,id=“workdiv”被->