我打算做的是一个对话框会提示并且用户将输入他的用户名,点击确定后,javascript函数中的php代码mySearch将在mysql中获取他的详细信息并将其显示在文本框中。救命。 T_T不起作用。
<script>
function mySearch()
{ var name = prompt("Search","Enter username");
if (name!=null && name!="")
{
var sentval = document.getElementById("sentval");
sentval.value = name;
<?php
$myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
$selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
$sql = "SELECT * FROM userTBL WHERE uName ='".$_POST['sentval']."'";
$exec = mysql_query($sql, $myLink);
$row = mysql_fetch_array($exec);
$uname = $row['uName'];
$pwd = $row['pwd'];
$code = $row['sAns'];
$link = $row['path'];
?>
}
}
</script>
</head>
<body>
<form method="post" action="" name="myform" id="myform">
<input type="hidden" id="sentval"/>
<center>
<table>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Username:  </font></td>
<td>
<input type = "text" id = "uname" name = "uname" size="35" value="<?php echo $uname ?>"/>
<button type="button" style="height: 25px; width: 60px" onclick="mySearch()">Search</button>
</td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Password:  </font></td>
<td><input type = "password" id = "pwd" name = "pwd" size="35" value="<?php echo $pwd ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Re-type Password:  </font></td>
<td><input type = "password" id = "repwd" name = "repwd" size="35" value="<?php echo $pwd ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Code:  </font></td>
<td><input type = "text" id = "code" name = "code" size="35" value="<?php echo $code ?>"/></td>
</tr>
<tr>
<td><font face="Tahoma" size = "2" color="#0B3861">Link:  </font></td>
<td><input type = "text" id = "link" name = "link" size="35" value="<?php echo $link ?>"/></td>
</tr>
<tr>
<td colspan=2 align="right">
<font face="Tahoma" size = "2" color="#0B3861">(e.g. http://www.google.com or index.php)</font>
</td>
</tr>
<tr>
<td colspan=2 align="right">
<input type="submit" name="submit" value="Save" style="height: 25px; width: 60px"/>
<button type="button" style="height: 25px; width: 60px" onclick="location.href='adminMain.php'">Cancel</button>
</td>
</tr>
</table>
</center>
</form>
</body>
<?php
if (!empty($_POST['uname']) && !empty($_POST['pwd']) && !empty($_POST['repwd']) && !empty($_POST['code']) && !empty($_POST['link']))
{
if ($_POST['pwd'] == $_POST['repwd'])
{
$myLink = mysql_connect('localhost','root', '1234') or die(mysql_error());
$selectDB = mysql_select_db('proj2db', $myLink) or die(mysql_error());
$sql = "UPDATE `proj2db`.`usertbl` SET pwd='".$_POST['pwd']."', sAns='".$_POST['code']."', path='".$_POST['link']."' WHERE uName='".$_POST['uname']."';";
$exec = mysql_query($sql, $myLink);
}
else
echo "<font face='Tahoma' size = '2' color='red'><center>Error: Password mismatched <br> Press cancel to return to home page </center></font>";
}
?>
答案 0 :(得分:2)
您似乎对PHP和JavaScript的存在感到困惑。 Javascript严格 客户端(禁止node.JS),而PHP 严格服务器端。你不能以你的方式将它们内联在一起 - 或者至少不是你期望它如何工作。现在,无论用户访问该页面的时刻,PHP代码都将运行。
这是一个适合你的解决方案(它依赖于jQuery,所以你需要它的副本)。我也让你转到PDO,让你的生活更轻松,并告诉你如何从mysql_query
过渡。
保持<body>
代码几乎相同。唯一的区别是mySearch()
函数,如下所示:
<script type='text/javascript'>
function mySearch() {
var uname = prompt("Search","Enter username");
if (uname !== undefined && uname.length > 0) {
$.ajax({
url: "ajax.form.php",
type: "POST",
dataType: "json",
data: { sentval: uname },
success: function(d) {
if (d.length > 0) {
$("#uname").val(d[0].uName);
$("#pwd").val(d[0].pwd);
$("#code").val(d[0].sAns);
$("#link").val(d[0].path);
}
else {
alert("Not found");
}
}
});
}
}
</script>
为此,您还需要另一个PHP脚本。我把它命名为ajax.form.php
。内容:
<?php
try {
$DB = new PDO("mysql:host=localhost;dbname=proj2db","root","1234");
} catch (Exception $e) {
die("Could not connect: ".$e->getMessage());
}
$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!empty($_POST['sentval'])) {
$q = $DB->prepare("SELECT * FROM userTBL WHERE uName = :username");
$q->bindValue(":username",$_POST['sentval']);
$q->execute();
if (($r = $q->fetch()) !== false) {
echo json_encode(array($r));
}
else {
echo "[]";
}
} ?>
就是这样!它应该工作,虽然我已经写了这个,并没有测试它。背后的想法:
$.ajax()
fetch()
更改为fetch_all()
并将json_encode中的Array()
删除)到JavaScript,脚本也可以正常工作