Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Ajax响应中执行JavaScript_Php_Javascript_Jquery_Ajax - Fatal编程技术网

Php 在Ajax响应中执行JavaScript

Php 在Ajax响应中执行JavaScript,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我有一个ajax表单流程。但我在单击某个函数时会执行此过程。当我单击它时,会在Jquery对话框中打开一个PHP表单。但是当我处理表单时,POST变量为NULL 代码如下: 具有请求表单功能的主文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:

我有一个ajax表单流程。但我在单击某个函数时会执行此过程。当我单击它时,会在Jquery对话框中打开一个PHP表单。但是当我处理表单时,POST变量为NULL

代码如下:

具有请求表单功能的主文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aircrafts</title>
    <link rel="stylesheet" type="text/css" href="../../../lib/css/style.css">
    <link rel="stylesheet" href="../../../lib/css/flick/jquery.ui.all.css">
    <script src="../../../lib/js/jquery.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.button.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.core.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.widget.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.mouse.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.button.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.draggable.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.position.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.resizable.js"></script>
    <script src="../../../lib/js/ui/jquery.ui.dialog.js"></script>


    <script>
    $(function() {
        $( "#loadingdialog" ).dialog({
            autoOpen: false,
            width: 300,
            height: 65
        });

        $("#loadingdialog").dialog('widget').find(".ui-dialog-titlebar").hide();
        $("#loadingdialog").dialog('widget').find(".ui-resizable-se").hide();

        $( "#editaircraftdialog" ).dialog({
            autoOpen: false,
            width: 425
        });

        $("#editaircraftdialog").dialog('widget').find(".ui-resizable-se").hide();                   
    });

        if (window.XMLHttpRequest)
                {
                ajax=new XMLHttpRequest();
                }
            else
                {
                ajax=new ActiveXObject("Microsoft.XMLHTTP");
                }


     function edit(str) {
            $.ajax({
                url: "./edit_aircraft.php?icao="+str,
                context: document.body,
                success: function(responseText) {
                    $("#editaircraftdialog").html(responseText);
                    $("#editaircraftdialog").find("script").each(function(i) {
                        eval($(this).text());
                    });
                    $("#editaircraftdialog").dialog('open');
                }
            });
        };
    </script>
    </head>
    <body>

    <div id="result"></div></br>

    <div id="loadingdialog"><center><p><img src="../../../lib/images/loading.gif"></center></p></div>

    <div id="editaircraftdialog"></div>


 <?php
    require_once('../../config.php');

$query = "SELECT * FROM aircrafts ORDER BY ICAO ASC LIMIT";

$result = mysql_query($query);
if (!$result)
{
die (mysql_error());
}

echo "<table border='0' cellspacing='0'>";
echo "<tr><th class=tabletitles>ICAO</th><th class=tabletitles>Edit</th></tr>";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    if($i++%2==0){
       $color="#FFFFFF";
    }else{
       $color="#CCCCCC";
    }

    ?>

    <tr bgcolor='<?php echo $color; ?>' onmouseover="this.style.background='#ABFB04';" onmouseout="this.style.background='<?php echo $color; ?>';">
    <?php

echo "<td class=tablelist>";

echo $row["ICAO"] . '</td><td class=tablelist>';


echo "<img src=\"../../../lib/images/edit.png\" onclick=\"edit('".$row["ICAO"]."')\"></td><td class=tablelist>";

}

echo "</table>";
mysql_close();
    ?>

    </body>
    </html>
包含AJAX表单流程和PHP表单的文件:

<?php
require_once ('../../config.php');
$icao = $_GET["icao"];

$query = "SELECT * FROM aircrafts WHERE ICAO = '$icao'";

$result = mysql_query($query);
if (!$result)
{
die (mysql_error());
}

?>
<form action="javascript:editForm();" method="post" enctype="application/x-www-form-urlencoded"> 
<?php
echo '<table border="0">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

echo '<tr><td class="forms">ICAO:</td><td><input type="text" id="icao" name="icao" size="30" value=';
echo $row["ICAO"] . "></td></tr>";

echo '<tr><td class="forms">Name:</td><td><input type="text" id="name" name="name" size="30" value=';
echo $row["Name"] . "></td></tr>";

echo '<tr><td class="forms">Weight Empty:</td><td><input type="text" id="weightempty" name="weightempty" size="30" value=';
echo $row["WeightEmpty"] . "></td></tr>";

echo '<tr><td class="forms">Weight Full:</td><td><input type="text" id="weightfull" name="weightfull" size="30" value=';
echo $row["WeightFull"] . "></td></tr>";

echo '<tr><td class="forms">Cargo Full:</td><td><input type="text" id="cargofull" name="cargofull" size="30" value=';
echo $row["CargoFull"] . "></td></tr>";

echo '<tr><td class="forms">Cruise Speed:</td><td><input type="text" id="cruisespeed" name="cruisespeed" size="30" value=';
echo $row["CruiseSpeed"] . "></td></tr>";

echo '<tr><td class="forms">Range:</td><td><input type="text" id="range" name="range" size="30" value=';
echo $row["Range"] . "></td></tr>";

echo '<tr><td class="forms">Price:</td><td><input type="text" id="price" name="price" size="30" value=';
echo $row["Price"] . "></td></tr>";

if($row["FirstClassSeats"] != NULL && $row["FirstClassSeats"] != 0){
echo '<tr><td class="forms">First Class Seats:</td><td><input type="text" id="firstclassseats" name="firstclassseats" size="30" value=';
echo $row["FirstClassSeats"] . "></td></tr>";}

if($row["BusinessClassSeats"] != NULL && $row["BusinessClassSeats"] != 0){
echo '<tr><td class="forms">Business Class Seats:</td><td><input type="text" id="businessclassseats" name="businessclassseats" size="30" value=';
echo $row["BusinessClassSeats"] . "></td></tr>";}

if($row["EconomyClassSeats"] != NULL && $row["EconomyClassSeats"] != 0){
echo '<tr><td class="forms">Economy Class Seats:</td><td><input type="text" id="economyclassseats" name="economyclassseats" size="30" value=';
echo $row["EconomyClassSeats"] . "></td></tr>";}

echo '<tr><td><input id="editaircraft" type="submit" value="Edit Aircraft"></td></tr>';

}
echo "</table>";
?>

</form>
<script>

    $(function() {
        $("#editaircraft")
            .button()
            .click(function editForm() {
        });
    });

function editForm(){

            var icao = document.getElementById('icao').value;
            var name = document.getElementById('name').value;
            var weightempty = document.getElementById('weightempty').value;
            var weightfull = document.getElementById('weightfull').value;
            var cargofull = document.getElementById('cargofull').value;
            var cruisespeed = document.getElementById('cruisespeed').value;
            var range = document.getElementById('range').value;
            var price = document.getElementById('price').value;
            var firstclassseats = document.getElementById('firstclassseats').value;
            var businessclassseats = document.getElementById('businessclassseats').value;
            var economyclassseats = document.getElementById('economyclassseats').value;
            ajax.open("POST","edit_aircraft_process.php",true);
            ajax.onreadystatechange=function(){
                if(ajax.readyState==4)
                {
                refreshTable(function(){$("#loadingdialog").dialog('close');});

                refreshTable(function(){$("#result").fadeIn(); document.getElementById("result").innerHTML=ajax.responseText;});
                setTimeout(function() { $("#result").fadeOut() }, 5000);
                }
            }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("icao="+icao+"&name="+name+"&weightempty="+weightempty+"&weightfull="+weightfull+"&cargofull="+cargofull+"&cruisespeed="+cruisespeed+"&range="+range+"&price="+price+"&firstclassseats="+firstclassseats+"&businessclassseats="+businessclassseats+"&economyclassseats="+economyclassseats);
        $("#editaircraftdialog").dialog('close');
        $("#loadingdialog").dialog('open');
        }
</script>
当我执行editForm时,它工作并将过程的结果放入fine中。但是,发送到数据库中进行编辑的变量(例如icao)为空,并且不进行编辑


我认为错误是因为在ajax请求后,它无法执行document.getElementById.

好吧,不确定确切的问题是什么,但在快速检查代码时,这里有两件事:

您没有正确关闭输入标记,例如:

 echo '<tr><td class="forms">ICAO:</td><td><input type="text" id="icao" name="icao" size="30" value=';
echo $row["ICAO"] . "/></td></tr>";// <--- notice the added forward slash to close the input

在XML中,片段标识符是ID类型的,每个元素只能有一个ID类型的属性。因此,在XHTML1.0中,id属性被定义为id类型,以确保XHTML1.0文档是结构良好的XML文档

在这种情况下,您需要将数据循环到相同的输入id中。将代码更改为:

var i = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

echo '<tr><td class="forms">ICAO:</td><td><input type="text" id="icao_' . i . '" name="icao" size="30" value="';
echo $row["ICAO"] . '"></td></tr>"';

echo '<tr><td class="forms">Name:</td><td><input type="text" id="name_' . i . '" name="name" size="30" value="';
echo $row["Name"] . '"></td></tr>"';

echo '<tr><td class="forms">Weight Empty:</td><td><input type="text" id="weightempty_' . i . '" name="weightempty" size="30" value="';
echo $row["WeightEmpty"] . '"></td></tr>"';

echo '<tr><td class="forms">Weight Full:</td><td><input type="text" id="weightfull_' . i . '" name="weightfull" size="30" value="';
echo $row["WeightFull"] . '"></td></tr>"';

echo '<tr><td class="forms">Cargo Full:</td><td><input type="text" id="cargofull_' . i . '" name="cargofull" size="30" value="';
echo $row["CargoFull"] . '"></td></tr>"';

echo '<tr><td class="forms">Cruise Speed:</td><td><input type="text" id="cruisespeed_' . i . '" name="cruisespeed" size="30" value="';
echo $row["CruiseSpeed"] . '"></td></tr>"';

echo '<tr><td class="forms">Range:</td><td><input type="text" id="range_' . i . '" name="range" size="30" value="';
echo $row["Range"] . '"></td></tr>"';

echo '<tr><td class="forms">Price:</td><td><input type="text" id="price_' . i . '" name="price" size="30" value="';
echo $row["Price"] . '"></td></tr>"';

if($row["FirstClassSeats"] != NULL && $row["FirstClassSeats"] != 0){
echo '<tr><td class="forms">First Class Seats:</td><td><input type="text" id="firstclassseats_' . i . '" name="firstclassseats" size="30" value="';
echo $row["FirstClassSeats"] . '"></td></tr>"';}

if($row["BusinessClassSeats"] != NULL && $row["BusinessClassSeats"] != 0){
echo '<tr><td class="forms">Business Class Seats:</td><td><input type="text" id="businessclassseats_' . i . '" name="businessclassseats" size="30" value="';
echo $row["BusinessClassSeats"] . '"></td></tr>"';}

if($row["EconomyClassSeats"] != NULL && $row["EconomyClassSeats"] != 0){
echo '<tr><td class="forms">Economy Class Seats:</td><td><input type="text" id="economyclassseats_' . i . '" name="economyclassseats" size="30" value="';
echo $row["EconomyClassSeats"] . '"></td></tr>"';}

echo '<tr><td><input id="editaircraft_' . i . '" type="submit" value="Edit Aircraft"></td></tr>';
i++;
}
为此:

ajax.send($("form").serialize());

你能在没有错误的情况下输入代码吗?我不知道如何关闭它,并围绕值ok。上面的代码已修复错误。使用第二个,两个都已修复确保对其他输入执行相同的操作!好的,我来修。但这不是问题所在!我不知道为什么找不到document.getElementById!你有现场演示吗?如果没有,请在editForm中创建变量后立即尝试console.logicao,如果显示良好,则继续向下移动console.log,直到找到错误!第一个代码不正确。您正在php中添加Javascript变量。我需要把var转换成PHP?我需要更改vars文档.getElementById?很抱歉我忘记了,只需将var I更改为PHP变量$I即可
ajax.send("icao="+icao+"&name="+name+"&weightempty="+weightempty+"&weightfull="+weightfull+"&cargofull="+cargofull+"&cruisespeed="+cruisespeed+"&range="+range+"&price="+price+"&firstclassseats="+firstclassseats+"&businessclassseats="+businessclassseats+"&economyclassseats="+economyclassseats);
ajax.send($("form").serialize());