可能重复: Obtain日期与JavaScript的AJAX请求之前
我有一个Ajax请求是这样的:
函数ajaxrequest(STR){
VAR飞机= $(#resultdiv);
aircraft.load(./ other_file.php?国际民航组织=+ STR,函数(){
});
}
如果我把JavaScript中的other_file.php鸵鸟政策作品。我不知道为什么?但是,如果使用JavaScript code直接工作的乐趣!但我需要的功能。我怎样才能改变功能,这样的结果与JavaScript的乐趣?
例如:
如果该other_file.php有这样的:
<脚本>
例如,函数(){
。VAR ID =的document.getElementById('身份证')值;
$(DIV2)文本(ID);
}
< / SCRIPT>
这鸵鸟政策工作,因为它需要获得VAR ID,但如果元素的值2365的JavaScript code,以获得它的document.getElementById('身份证')。值;
鸵鸟政策工作,因为JavaScript鸵鸟政策工作。但是,如果我的功能直接这样做,并没有ajaxrequest一切工作正常。
这是真的含有other_file.php的
<脚本>
$(函数(){
$(#editaircraft)
.button()
。点击(函数editForm(){
});
});
功能editForm(){
VAR国际民航组织=的document.getElementById('ICAO)的价值。
。VAR名称=的document.getElementById('名')值;
。VAR weightempty =的document.getElementById('weightempty')值;
。VAR weightfull =的document.getElementById('weightfull')值;
。VAR cargofull =的document.getElementById('cargofull')值;
VAR cruisespeed =的document.getElementById('cruisespeed')值。
VAR范围=的document.getElementById('范围')值。
。VAR价格=的document.getElementById('价格')值;
VAR firstclassseats =的document.getElementById('firstclassseats')值。
VAR businessclassseats =的document.getElementById('businessclassseats')值。
VAR economyclassseats =的document.getElementById('economyclassseats')值。
ajax.open(POST,edit_aircraft_process.php,真正的);
ajax.onreadystatechange =功能(){
如果(ajax.readyState == 4)
{
refreshTable(函数(){$(#loadingdialog)对话框(亲密);});
refreshTable(函数(){$(#结果)淡入();的document.getElementById(结果)的innerHTML = ajax.responseText;});
的setTimeout(函数(){$(#结果)淡出()},5000);
}
}
ajax.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD);
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)对话框(亲密)。
$(#loadingdialog)对话框(公开)。
}
< / SCRIPT>
< PHP
require_once('../../config.php');
$国际民航组织= $ _REQUEST [国际民航组织];
$查询=SELECT * FROM飞机WHERE国际民航组织='$国际民航组织;
$结果= mysql_query($查询);
如果(!$结果)
{
死亡(mysql_error());
}
?>
<形式的行动=JavaScript的:editForm();方法=邮报是enctype =应用/的X WWW的形式urlen codeD>
< PHP
回声'<表格边框=0>';
而($行= mysql_fetch_array($结果,MYSQL_ASSOC)){
回声'< TR>< TD类=形式> ICAO:LT; / TD>< TD><输入类型=文本ID =国际民航组织NAME =国际民航组织大小=30值=';
回声$行[国际民航组织。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>名称:< / TD>< TD><输入类型=文本ID =名称NAME =名字大小=30值=';
回声$行[名称]。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>空重:LT; / TD>< TD><输入类型=文本ID =weightemptyNAME =weightempty大小=30 值=';
回声$行[WeightEmpty。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>重量满:LT; / TD>< TD><输入类型=文本ID =weightfullNAME =weightfull大小=30 值=';
回声$行[WeightFull。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>货物完整:LT; / TD>< TD><输入类型=文本ID =cargofullNAME =cargofull大小=30 值=';
回声$行[CargoFull。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>巡航速度:LT; / TD>< TD><输入类型=文本ID =cruisespeedNAME =cruisespeed大小=30 值=';
回声$行[CruiseSpeed。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>范围:< / TD>< TD><输入类型=文本ID =范围NAME =范围大小=30值=';
回声$行[范围。 >&所述; / TD>&所述; / TR>中;
回声'< TR>< TD类=形式>价格:其中; / TD>< TD><输入类型=文本ID =价格NAME =价格大小=30值=';
回声$行[价格。 >&所述; / TD>&所述; / TR>中;
如果($行[FirstClassSeats] = NULL和放大器;!&安培;!$行[FirstClassSeats] = 0){
回声'< TR>< TD类=形式>头等舱座椅:LT; / TD>< TD><输入类型=文本ID =firstclassseatsNAME =firstclassseats大小= 30值=';
回声$行[FirstClassSeats。 >< / TD>< / TR>中;}
如果($行[BusinessClassSeats] = NULL和放大器;!&安培;!$行[BusinessClassSeats] = 0){
回声'< TR>< TD类=形式>商务舱座椅:LT; / TD>< TD><输入类型=文本ID =businessclassseatsNAME =businessclassseats大小= 30值=';
回声$行[BusinessClassSeats。 >< / TD>< / TR>中;}
如果($行[EconomyClassSeats] = NULL和放大器;!&安培;!$行[EconomyClassSeats] = 0){
回声'< TR>< TD类=形式>经济舱座椅:LT; / TD>< TD><输入类型=文本ID =economyclassseatsNAME =economyclassseats大小= 30值=';
回声$行[EconomyClassSeats。 >< / TD>< / TR>中;}
回声'< TR>< TD><输入ID =editaircraft类型=提交值=编辑飞机>< / TD>< / TR>';
}
回声< /表>;
?>
< /形式GT;
瓦尔的成于被undefinid!
解决方案 VAR飞机= $(#resultdiv);
功能ajaxrequest(STR){
$获得(other_file.php?ICAO =+ STR,功能(结果){
aircraft.append(结果);
});
}
other_file.php
...
<脚本>
$(#editaircraft)
.find('按钮')/ * ??? * /
。点击(editForm);
功能editForm(){
VAR国际民航组织=的document.getElementById('ICAO)的价值。
。VAR名称=的document.getElementById('名')值;
。VAR weightempty =的document.getElementById('weightempty')值;
。VAR weightfull =的document.getElementById('weightfull')值;
。VAR cargofull =的document.getElementById('cargofull')值;
VAR cruisespeed =的document.getElementById('cruisespeed')值。
VAR范围=的document.getElementById('范围')值。
。VAR价格=的document.getElementById('价格')值;
VAR firstclassseats =的document.getElementById('firstclassseats')值。
VAR businessclassseats =的document.getElementById('businessclassseats')值。
VAR economyclassseats =的document.getElementById('economyclassseats')值。
ajax.open(POST,edit_aircraft_process.php,真正的);
ajax.onreadystatechange =功能(){
如果(ajax.readyState == 4)
{
refreshTable(函数(){$(#loadingdialog)对话框(亲密);});
refreshTable(函数(){$(#结果)淡入();的document.getElementById(结果)的innerHTML = ajax.responseText;});
的setTimeout(函数(){$(#结果)淡出()},5000);
}
}
ajax.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD);
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)对话框(亲密)。
$(#loadingdialog)对话框(公开)。
}
< / SCRIPT>
Possible Duplicate: Obtain dates with JavaScript before AJAX request
I have an Ajax Request like this:
function ajaxrequest(str){
var aircraft = $("#resultdiv");
aircraft.load("./other_file.php?icao="+str, function(){
});
}
If I put Javascript in the other_file.php don´t works. I don´t know why? But If use the javascript code directly works fun! But I need the function. How Can I change the function to the result of this with Javascript works fun?
For example:
If the other_file.php have this:
<script>
function example(){
var id = document.getElementById('id').value;
$("div2").text(id);
}
</script>
This don´t work because it needs obtain the var id, but if the element has the value 2365. The javascript code to obtain it document.getElementById('id').value;
don´t works because javascript don´t work. But If I do this directly and no by the function ajaxrequest all works fine.
This is the really contain of the other_file.php
<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>
<?php
require_once ('../../config.php');
$icao = $_REQUEST["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>
The obtains of vars are undefinid!
解决方案var aircraft = $("#resultdiv");
function ajaxrequest(str){
$.get("other_file.php?icao="+str, function(result){
aircraft.append(result);
});
}
other_file.php
...
<script>
$("#editaircraft")
.find('button') /* ??? */
.click(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>