我正在尝试使用php代码删除整行。
但在此之前,我的页面有多行以单选按钮结束 在点击删除按钮之前,我们必须选择任何一个单选按钮,然后我们可以点击删除。
但我想要的是,我已经编写了一个获取单选按钮值的java脚本代码,之后我想将该javascript变量值转换为php代码,然后执行delete命令..
这是我试过的
代码 javascript
<script>
function getResults() {
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var a = radios[i].value
alert(a);
break;
}
}
</script>
php代码:
<?php
$_GET['a']
mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL =("DELETE FROM theater WHERE theater_name='"$_POST["a"]"'");
$rs = mysql_query($strSQL);
mysql_close();
?>
}
但这不起作用
我甚至尝试了这个。它也没有用。
<?php
if ($_POST['submit'])
{
$_GET['a'];
mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL =("DELETE FROM theater WHERE address='".$_POST["a"]."'");
$rs = mysql_query($strSQL);
mysql_close();
}
?>
答案 0 :(得分:3)
在js中
<script>
function getResults() {
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var apost = radios[i].value
//send set state request
$.ajax({
type: "POST",// here you can use get/post if use get then in php use $_GET['a'] or use post then in php $_POST['a']
url: "your php file name with path",
data: {a:apost},
beforeSend: function () {
},
success: function (data, textStatus, XmlHttpRequest) {
if(data==1)// see php file code you will know how i use data==1
{
// do your action
}
else{
// in you php there is someyhing error
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
// do you action
}
});
break;
}
}
</script>
PHP中的
<?php
mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL =("DELETE FROM theater WHERE theater_name='".$_POST["a"]."'");
$rs = mysql_query($strSQL);
echo 1; // here i use 1 to get response if this is done successfully then in ajax we get 1 in data
mysql_close();
?>
答案 1 :(得分:3)
您必须将where条件值从js发送到delete query ..
<script>
function getResults() {
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var a = radios[i].value
alert(a);
document.yourformname.action="yourphp.php?a="+a;
document.yourformname.submit();
break;
}
}
</script>
答案 2 :(得分:1)
嘿,你需要附加php变量,如下所示
$strSQL =("DELETE FROM theater WHERE theater_name='".$_POST["a"]."'");
答案 3 :(得分:1)
你必须从html发送值 分配javascript变量未在GET中添加
你必须发送类似
的值<script>
function getResults() {
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var a = radios[i].value;
header.location.href = "yourphp.php/?a=" + a; //changed
break;
}
}
</script>
Php Code
<?php
$t_name = $_GET['a']
mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL =("DELETE FROM theater WHERE theater_name='" . $t_name . "'"); //changed Line
$rs = mysql_query($strSQL);
mysql_close();
?>