1

我打算做的是一个对话框会提示,用户将输入他的用户名,当单击确定时,javascript函数mySearch中的php代码将在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:&nbsp&nbsp</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:&nbsp&nbsp</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:&nbsp&nbsp</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:&nbsp&nbsp</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:&nbsp&nbsp</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>";
    }
?>

4

1 回答 1

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 "[]";
   }
} ?>

就是这样!它应该可以工作,尽管我是即时编写的并且尚未对其进行测试。其背后的想法:

  1. JavaScript 通过 sentval 调用其他 PHP 脚本$.ajax()
  2. PHP 运行
  3. PHP 将一组行(在这种情况下,只有一个,但脚本通过更改fetch()fetch_all()删除Array()in json_encode)到 JavaScript也能正常工作
  4. JavaScript 解析和更新客户端的表单。
于 2012-11-28T09:25:59.840 回答