2013-03-22 135 views
-1

我有此代码的工作:请求while循环

<form method="get" name="MobileDetails"> 
<input name="brand" id="brand" value="<?php echo $brand;?>" type="hidden"> 
<input name="brid" id="brid" value="<?php echo $brandid;?>" type="hidden"> 
<button type="button" name="submitButton" value="get Details" onclick="getDetails()">  
</form> 

Java脚本

<script type="text/javascript"> 
function getDetails(){ 
var brand = document.getElementById('brand').value; 
var brandid = document.getElementById('brid').value; 
document.MobileDetails.action = 'details.php?brand='+brand+'&id='+brandid; 
document.MobileDetails.submit(); 
} 
</script> 

但它不while循环工作英寸有什么问题?我的代码如下。 当我点击按钮,它不会做任何事情。但是代码在顶部给出的while循环中工作良好。

<?php 
require_once('connection.php'); 
$SQL= "SELECT*FROM mobile ORDER BY price ASC LIMIT 10"; 
$result= mysql_query($SQL); 
while ($db_field = mysql_fetch_assoc($result)){ 
$brand=$db_field['brand']; 
$id=$db_field['id']; 
$model=$db_field['model']; 
echo "<form method='get' name='MobileDetails'>"; 
echo " <input name='brand' id='brand' value='". $brand ."' type='hidden'>"; 
echo" <input name='brid' id='brid' value='". $id ."' type='hidden'>"; 
echo" <input name='mod' id='mod' value='". $model ."' type='hidden'>"; 
echo" <button type='button' name='submitButton' value='get Details' onclick='getDetails()'/>  
    </form> "; 
    echo "CLICK HERE"; 
    } 

?> 

回答

1

您正在使用几次相同的ID。 Ids必须是独一无二的。

1

你正在处理多个ID。一个ID的作用是作为元素的唯一标识符。我建议只使用

<form action="details.php" type="get"> 

这将做你正在尝试实现而不使用函数。

0

带元素ID的东西是它们需要对页面是唯一的;但是,正如您所看到的,不需要显示HTML。当调用JS函数getDetails()时,它通过ID抓取元素,但是当页面中有多个ID时,这将失败。

那么你能做什么?那么,在你的循环中,你为每个“品牌”创建一个新表单。您可以将表单的引用传递给grabdetails,然后通过名称,获取该表单中的值。


比使用Javascript功能来生成基于给定信息的链接放置在一个隐藏字段相反,你应该只产生在PHP层的作用。

echo "<form method='get' name='MobileDetails' action='details.php?brand=$brand&id=$brandid'>"; 

但既然你有隐藏字段,只使用action='details.php'形式将用户带到

details.php?brand={brand}&brid={id}&mod={model} 

你应该看看POST或使你的按钮变成一个普通的链接,而不是一种形式。

+0

NOt Working :(bro – 2013-03-22 17:00:47

+0

@RahulMitra你是否改变了你的代码来使用我的建议或者只是改变两个地方?你需要删除两条'var'行的JS函数 – UnholyRanger 2013-03-22 17:02:33

+0

2013-03-22 17:08:07