2013-01-25 35 views
1

我正在使用此函数来显示在我的表单中选择的值。结果在我的页面上正确显示,但没有任何内容发送到我的getlist.php页面。jquery如何将showValues()数据发布到另一页

有人可以帮助我吗?

jQuery代码:

function showValues() { 
    var str = $("form").serialize(); 
    $("#results").text(str); 
} 

$(":checkbox, :radio").click(showValues); 
$("select").change(showValues); 
showValues(); 

$.post("getlist.php", $("form").serialize()); 

HTML:

<p><tt id="results"></tt></p> 

这里是我的形式

<form id="Moteur" onchange="testMoteur()"> 
<table cellspacing="0" cellpadding="5" bgcolor="#Be9e55"> 
<tr height="30px"> 
<td align="right">Marques : </td> 
<td><? include ("marques.php"); ?></td> 
</tr> 
</table> 
</form> 

和marques.php代码

<select name="marques" id="marques" multiple="multiple"> 
<? 
//<option value="x">Toutes les marques :</option> 
?> 
<? 
mysql_connect("","",""); 
mysql_select_db(""); 
$query = mysql_query("SELECT * FROM marque WHERE flag_montre=1 ORDER BY nom ASC"); 
while ($myrow = mysql_fetch_row($query)) { 
$id_marque=$myrow[0]; 
//test si marque est vide 
$res3=mysql_query("SELECT * FROM montre WHERE id_marque=$id_marque AND id_etat=3"); 
$nbreLignes = mysql_num_rows($res3); 
if ($nbreLignes==0){ 
} 
else { 
echo "<option value='".$myrow[0]."'>".$myrow[1]." (".$nbreLignes.")</option>"; 
} 
} 
        ?> 
</select> 

我刚刚更新了我的脚本,所有的工作都很棒,但ie8中没有任何事情发生(0错误)。这个脚本的目标是发送阵列瓦尔我getlist.php

function SendMoteur() { 

var xmlhttp = ""; 
var url = ""; 

// For modern browsers 
if(window.XMLHttpRequest) { 
    xmlhttp = new XMLHttpRequest(); 

// for IE 5/6 
} else { 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 

// Rebuild the array of selected option boxes 
marques = document.getElementById("marques"); 
for(var i=0; i < marques.length; i++) { 
    if(marques[i].selected) { 
     // Note the [] after the name 
     url += "&marques[]=" + marques[i].value; 
    } 
} 

fourchettes = document.getElementById("fourchettes"); 
for(var i=0; i < fourchettes.length; i++) { 
    if(fourchettes[i].selected) { 
     // Note the [] after the name 
     url += "&fourchettes[]=" + fourchettes[i].value; 
    } 
} 

mouvements = document.getElementById("mouvements"); 
for(var i=0; i < mouvements.length; i++) { 
    if(mouvements[i].selected) { 
     // Note the [] after the name 
     url += "&mouvements[]=" + mouvements[i].value; 
    } 
} 

boitiers = document.getElementById("boitiers"); 
for(var i=0; i < boitiers.length; i++) { 
    if(boitiers[i].selected) { 
     // Note the [] after the name 
     url += "&boitiers[]=" + boitiers[i].value; 
    } 
} 

bracelets = document.getElementById("bracelets"); 
for(var i=0; i < bracelets.length; i++) { 
    if(bracelets[i].selected) { 
     // Note the [] after the name 
     url += "&bracelets[]=" + bracelets[i].value; 
    } 
} 


xmlhttp.open("GET","getlist.php?" + url, false); 
xmlhttp.send(); 

if (xmlhttp.status == 200) { 
    document.getElementById("txtHint2").innerHTML = xmlhttp.responseText; 
} else { 
    return false; 
} 

return false; 
} 
</script> 
+0

告诉我们您的HTML(表格)代码太.. – bipen

回答

0

这是正确的jQuery的形式,试试这个:

$.post("getlist.php", $("#form").serialize()); 

,或者如果这是一个窗体类试试这个:

$.post("getlist.php", $(".form").serialize()); 

另外,检查getlist.php文件是否与您当前的文件位于同一个文件夹中。

+1

他说,'showValues()'函数工作正常,所以它只是'$(“form”)' - 没有类或ID。 – Archer

+0

所有的文件都在同一个文件夹 – user2010500

0

你为什么不使用这个equavalent之一:

给予ID到您的窗体:form_id

function showValues() { 
var str = $("#form_id").serialize(); 
$("#results").text(str); 

$.ajax({ 
    type: "POST", 
    url: "getlist.php", 
    data: {data: str}, 
}).done(function(){ 
    alert('yiha, worked!'); 
}); 
} 
+0

感谢您的回答,但没有发送到getlist.php – user2010500

+0

我使用print_r($ _ POST);在我的getlist.php中,也许这不是验证已发送serialezed var的好方法! – user2010500

相关问题