2012-12-24 20 views
0

我有這兩個文件:相關事情PHP + AJAX

new_aircraft.php

<!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> 
<link rel="STYLESHEET" type="text/css" href="./style.css"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Insert Aircraft</title> 
</head> 
<body> 
<script> 
//Ajax Script 
function require(){ 
    try{ 
    req = new XMLHttpRequest(); 
    }catch(err1){ 
     try{ 
     req = new ActiveXObject("Microsoft.XMLHTTP"); 
     }catch(err2){ 
      try{ 
      req = new ActiveXObject("Msxml2.XMLHTTP"); 
      }catch(err3){ 
      req = false; 
      } 
     } 
    } 
return req; 
} 


var request = require(); 

function callAjax(){ 
var ramdom = parseInt(Math.random()*999999999); 
valor = document.getElementById("numberclasses").value; 
var url="classes.php?Value="+valor+"&r="+ramdom; 
request.open("GET",url,true); 
request.onreadystatechange = answerAjax; 
request.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
request.send(null); 
} 

function answerAjax(){ 

    if(request.readyState==4){ 
     if(request.status==200){ 
     //alert(request.responseText); 
     document.getElementById("classes").innerHTML=request.responseText; 
     }else{ 
     alert("ha ocurrido un error"+request.statusText); 
     } 
    } 
} 

</script> 
<? 

$boton = $_POST['enviar']; 
$nombre = $_POST['nombre']; 
$precio = $_POST['precio']; 
$pax = $_POST['pax']; 
$ICAO = $_POST['ICAO']; 

if($boton == 'Insertar') 
{ 
    include('./db.inc.php'); 

    $ICAO = strtoupper($ICAO);  
    mysql_query("INSERT INTO flota(ICAO, nombre, precio, capacidad) VALUES('$ICAO', '$nombre', '$precio', '$pax')") or die('<h4 style="color: red;">Ha habido un problema con la insercion.</h4>'); 

    mysql_close(); 

    echo '<h3 style="color: green;">Aeronave adquirida correctamente.<h3/>'; 
} 
else 
{ 
    ?> 
    <form action="insertar-modelo.php" method="post" enctype="application/x-www-form-urlencoded"> 
    <table> 
    <tr><td class=Forms>ICAO: </td><td><input type="text" value="" name="ICAO" /></td></tr> 
    <tr><td class=Forms>Name: </td><td><input type="text" value="Airbus A320" name="nombre" /></td></tr> 
    <tr><td class=Forms>Price: </td><td><input maxlength="9" value="1000000" type="text" name="precio" /> €</td></tr> 

    <tr><td class=Forms>Number Classes: </td></td><td><select name="numberclasses" id="numberclasses" onchange="callAjax()"> 
     <option>Select Number of Classes</option> 
     <?php 
     echo'<option value="1">One</option>'; 
     ?> 
    </select></td></tr> 


<tr><td id="classes" ></td></tr> 

    <tr><td class=Forms>Capacidad: </td><td><input maxlength="3" value="150" type="text" name="pax" /> pasajeros</td></tr> 
    </table><br /> 
    <input type="submit" name="enviar" value="Insertar"/> 
    </form> 
    <? 
} 
?> 
</body> 
</html> 

classes.php

<?php 
$value = $_GET['Value']; 

if($value == 1){ 


    ?> 


<thead> 
    <td class=Forms>First Class: </td><td><input maxlength="3" value="150" type="text" name="pax" /></td> 
</thead> 


<?php 
} 

?> 

當我 「選擇課程數量」 1的結果是http://i48.tinypic.com/erbfnn.png(這是一張圖片,請看它)。

頭等艙部件沒有按正確的順序排列。它應該在數字類下面,但是它是第一個。頭等艙部分並不尊重其他部件的寬度。

我該如何解決?

+0

我喜歡你的冠軍。依賴的東西。我喜歡的東西..非常多..我希望事情讓我喜歡:| – dbf

+0

歡迎來到Stack Overflow! –

回答

1

<table>的孩子只能是<thead>,<tbody><tfoot>;你也可以有<tr>,它會自動把它們包裝在<tbody>

因此改變:

<div id="classes"></div>

到:

<tr><td id="classes"></td></tr>

你也有一個<form></form>元素,你需要正確地匹配它們築巢。您無法在表格末端及其內部啓動表格,因此請刪除表格中額外的</form>

+0

'因此將

更改爲:'...? – dbf

+0

已修復 - 我的代碼標記讓我感到困惑。 – Barmar

+0

如果我搞砸了,我總是會責怪我:P – dbf

0

我編輯的文件:

new_aircraft.php

<!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> 
<link rel="STYLESHEET" type="text/css" href="./style.css"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Insert Aircraft</title> 
</head> 
<body> 
<script> 
//Ajax Script 
function require(){ 
    try{ 
    req = new XMLHttpRequest(); 
    }catch(err1){ 
     try{ 
     req = new ActiveXObject("Microsoft.XMLHTTP"); 
     }catch(err2){ 
      try{ 
      req = new ActiveXObject("Msxml2.XMLHTTP"); 
      }catch(err3){ 
      req = false; 
      } 
     } 
    } 
return req; 
} 


var request = require(); 

function callAjax(){ 
var ramdom = parseInt(Math.random()*999999999); 
valor = document.getElementById("numberclasses").value; 
var url="classes.php?Value="+valor+"&r="+ramdom; 
request.open("GET",url,true); 
request.onreadystatechange = answerAjax; 
request.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
request.send(null); 
} 

function answerAjax(){ 

    if(request.readyState==4){ 
     if(request.status==200){ 
     //alert(request.responseText); 
     document.getElementById("classes").innerHTML=request.responseText; 
     }else{ 
     alert("ha ocurrido un error"+request.statusText); 
     } 
    } 
} 

</script> 
<? 

$boton = $_POST['enviar']; 
$nombre = $_POST['nombre']; 
$precio = $_POST['precio']; 
$pax = $_POST['pax']; 
$ICAO = $_POST['ICAO']; 

if($boton == 'Insertar') 
{ 
    include('./db.inc.php'); 

    $ICAO = strtoupper($ICAO);  
    mysql_query("INSERT INTO flota(ICAO, nombre, precio, capacidad) VALUES('$ICAO', '$nombre', '$precio', '$pax')") or die('<h4 style="color: red;">Ha habido un problema con la insercion.</h4>'); 

    mysql_close(); 

    echo '<h3 style="color: green;">Aeronave adquirida correctamente.<h3/>'; 
} 
else 
{ 
    ?> 
    <form action="insertar-modelo.php" method="post" enctype="application/x-www-form-urlencoded"> 
    <table> 
    <tr><td class=Forms>ICAO: </td><td><input type="text" value="" name="ICAO" /></td></tr> 
    <tr><td class=Forms>Name: </td><td><input type="text" value="Airbus A320" name="nombre" /></td></tr> 
    <tr><td class=Forms>Price: </td><td><input maxlength="9" value="1000000" type="text" name="precio" /> €</td></tr> 

    <tr><td class=Forms>Number Classes: </td></td><td><select name="numberclasses" id="numberclasses" onchange="callAjax()"> 
     <option>Select Number of Classes</option> 
     <?php 
     echo'<option value="1">One</option>'; 
     ?> 
    </select></td></tr> 


<tr><td id="classes" ></td></tr> 

    <tr><td class=Forms>Capacidad: </td><td><input maxlength="3" value="150" type="text" name="pax" /> pasajeros</td></tr> 
    </table><br /> 
    <input type="submit" name="enviar" value="Insertar"/> 
    </form> 
    <? 
} 
?> 
</body> 
</html> 

classes.php

<?php 
$value = $_GET['Value']; 

if($value == 1){ 


    ?> 


<thead> 
    <td class=Forms>First Class: </td><td><input maxlength="3" value="150" type="text" name="pax" /></td> 
</thead> 


<?php 
} 

?> 

,其結果是http://i50.tinypic.com/2yjzinr.png