onclick操作无法按预期使用单选按钮

onclick action not working as intended with radio buttons

本文关键字:单选按钮 操作 onclick      更新时间:2023-09-26

在过去的4个小时里,我一直在努力工作。我查看了SO和其他来源,但找不到任何与该主题有关的内容。这是代码:

<?php   
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE  email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{   
    $id=$oferta['id_oferta'];
    echo "<input type='radio' name='selectie' value='$id' id='$id'>&nbsp;<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
    echo "</br>";
}
echo "</br>";

//echo "<input type='"button'" id='"cauta'" value='"Vizualizeaza'" onclick='"window.location.href='oferta.php?id={$oferta['id_oferta']}''" />";
//echo "&nbsp;<input type='"button'" id='"cauta'"value='"Modifica'" onclick='"window.location.href='modifica.php?id={$oferta['id_oferta']}''" />";
echo "&nbsp;<input type='"button'" id='"sterge'" value='"Sterge'" onclick='"window.location.href='delete.php?id=$id''" />";
echo "</form>";     
echo "</div>";  
}
else
{
}
?>          

while从数据库中拖动所有用户条目,并为每个条目创建一个单选按钮,其值和id(因为我真的不知道需要哪个)等于数据库中条目的id。我对此表示赞同,id显示为应该显示的,因此没有问题。

删除脚本也可以,所以除非你告诉我,否则我不会附加它。在我尝试删除条目之前,一切都很好,没有错误。无论我从条目列表中选择什么,它都会删除最后一个。请注意,我还有另外两个输入,它们将是条目的"查看"answers"修改"按钮。

我真的希望这与JavaScript无关,因为我对JS一无所知。我认为这将对其他有这个问题的人有很大帮助。如果我需要在降级前编辑我的问题,请告诉我。谢谢

编辑后:

这是删除脚本,正如我之前所说,它运行良好。

<?php
if (isset($_GET['id']))
{
    $id = $_GET['id'];
    echo $id;
    require_once('mysql_connect.php');
    $query = "DELETE FROM oferte Where id_oferta = '$id'";
    mysql_query($query) or die(mysql_error());
    //header('Location: oferte.php');
}
 else
{
//header('Location: oferte.php');
}

?>      

会话也开始了,如下所示:

<?php
session_start(); 
?>  

删除最后一个$id的原因是因为此行在while循环之外/之后:

echo "&nbsp;<input type='"button'" id='"sterge'" value='"Sterge'" onclick='"window.location.href='delete.php?id=$id''" />";

您想在循环中移动这一行,这样您就有了一个为每个单选按钮执行删除的按钮。

更新:

要删除链接和

echo "<input type='radio' name='selectie' value='$id' id='$id'>&nbsp;"; 
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>&nbsp;";
echo "<a href='delete.php?id=$id'>delete</a>";

此外,我认为这里根本不需要单选按钮,因为你并没有真正用它做任何事情。你可以简单地回声出你选择的价值,并有以下链接:

echo $oferta['denumire_locatie'] . '&nbsp;'; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>&nbsp;";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";

在这种情况下,问题与JavaScript有关,是的。我建议您只需为每个项目添加一个删除链接。

echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";

您的$id在while()循环之外。最后一个将被删除,因为当循环退出时,$id具有最后一个的值。

包括您的所有代码:

echo "</br>";

//echo "<input type='"button'" id='"cauta'" value='"Vizualizeaza'" onclick='"window.location.href='oferta.php?id={$oferta['id_oferta']}''" />";
//echo "&nbsp;<input type='"button'" id='"cauta'"value='"Modifica'" onclick='"window.location.href='modifica.php?id={$oferta['id_oferta']}''" />";
echo "&nbsp;<input type='"button'" id='"sterge'" value='"Sterge'" onclick='"window.location.href='delete.php?id=$id''" />";

进入你的while循环。

当渲染的html到达浏览器时,它将是这样的:

<input type='radio' name='selectie' value='1' id='1'>&nbsp;<a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'>&nbsp;<a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'>&nbsp;<a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'>&nbsp;<a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />

有了这个标记,如果不在选择单选按钮时使用javascript更新onclick属性,就无法实现您想要的内容。

另一方面,您可以使用按钮的默认行为,即提交表单,而不是使用客户端onclick事件

你只需要设置动作属性:

<form method="post" action="http://myurl.php">

并编写myurl.php页面,该页面将只读取已发布的变量$_POST["selectie"],并使用已发布的id调用delete方法。