我已阅读过类似的主题,但无法完成我的代码。插入表格行数据onclick复选框PHP和Ajax和MySQL
简而言之:您勾选了一个复选框,并取消了调用函数的Ajax响应。该功能获取登录用户的ID。一旦我获得用户的ID,我就可以使用PHP脚本。
我已经重写了代码片段;还没有尝试过。可以说这是我希望更接近。
我使用2个脚本,1个用于Ajax,另一个用于PHP。
阿贾克斯:
<html>
<head>
<script type="text/javascript">
function getLoggedInUser()
{
//IF THERE IS NOTHING CHECKED, THAT IS, STR == 0, THEN REMOVE WHATEVER WAS WRITTEN BEFORE
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
// JUST CHECKING BROWSER COMPATIBILITY ISSUES FIRST
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// IF THE SERVER HAS THE RESPONSE READY
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
THEN在具有选中该复选框将其发送到GETLOGGED在FN 用户ID,而这个函数将其转发到名为 INSERT用户数据的PHP脚本,这将JUST DO那。
xmlhttp.open("GET","insertUserData.php?$user="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php $user = getUserId();?>
<form>
<input type="checkbox" name="asset" value="" onclick="getLoggedInUser($user)" /> I am potentially
interested in this product or service<br />
<div id="txtHint"><b>Note:</b></div>
</form>
<br />
</body>
</html>
======这里的PHP脚本insertUserData.php ===========
<?php
$user = $_GET['user'];
// But now, as I indicated, we want the data corresponding to that ID
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$query = "SELECT name, country
FROM profiles
WHERE id = '". $user . "';
$result = @mysql_query ($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = ($row['name']);
$country = ($row['country']);
mysql_query("INSERT INTO Persons (name, country)
VALUES ($name, $country)");
echo 'Your interest has been saved';
mysql_close($con);
?>
预先感谢您
你的问题到底是什么?正如你上面已经看到的那样,你的PHP代码中存在解析错误(你没有正确关闭查询字符串引号),并且你在第一个代码片段中似乎混合了PHP和Javascript。 – Rijk
看来你运行的mysql_query之前的mysql_connect –
我的建议是通过一些基本的PHP/MySQL和Javascript教程工作,并更动的代码示例一点点。你会很快得到它的窍门。 – Rijk