提交动态ajax字段

时间:2013-08-20 22:21:11

标签: php ajax

我有一个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><?
}?>

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>

更新:新代码。

通过ajax调用findTest.php

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

1 个答案:

答案 0 :(得分: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']

所以为什么不使用你的选择值来完成所有这些。

<强> 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>
...

<强> 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 ...。在我的例子中,我只描述了两种可能性。你还需要例如

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