0

我有一个从教程中收集的即时搜索程序 。我修改了一些代码行。这是文件:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $(".search_button").click(function() {
                // getting the value that user typed
                var searchString    = $("#search_box").val();
                // forming the queryString
                var data            = 'search='+ searchString;

                // if searchString is not empty
                if(searchString) {
                    // ajax call
                    $.ajax({
                        type: "POST",
                        url: "instant_search.php",
                        data: data,
                        beforeSend: function(html) { // this happens before actual call
                            $(".results").html('');
                            //$("#uname").value('');
                            //$("#searchresults").show();
                            $(".word").html(searchString);
                        },
                        success: function(html){ // this happens after we get results
                            $(".results").show();
                            $(".results").append(html);
                            $('#uname').value(html);
                            //document.getElementById('uname').value(html);
                            //$("#uname").value(html);
                        }
                    });
                }
                return false;
            });
        });
    </script>
</head>
<body>
<?php echo '<center>';?>

    <div class="header_box"><?php echo $f->SYSTEM_NAME; ?></div>

<?php
if($acc_type == 'admin'){ ?>
    <h1>Create new admin account</h1>
    <table>
        <tr>
            <td>Username</td>
            <td>:</td>
            <td><input type="text" name="id" size="20" class="text_box"/></td>
            <td><input type="button" value="Check"></td>
        </tr>
    </table>

    <?php
}else if($acc_type == 'student'){ ?>
    <h1>.:: Create student's account ::.</h1>
    <label style="font-size: 18px"><label style="color: red">*</label> Marked fields are must</label><br/><br/>
<!--    <form action="" method="post">-->
    <table border="0">
        <tr class="unimportant_text">
            <td>Test Username</td>
            <td>:</td>
            <td>
                <form method="post" action="instant_search.php">
                    <input type="text" name="search" id="search_box" class="unimportant_text"/>
                    <input type="submit" class="search_button" value="Check" style="background: #808080; color: white; border: none"/><br />
                </form>
            </td>
        </tr>
        <tr>
            <td>Username<label style="color: red">*</label></td>
            <td>:</td>
            <td>
                <label class="results" style="font-size: 20px; color: green; font-weight: bold"></label>
                <input type="hidden" name="uname" id="uname"/>
            </td>
        </tr>
        <tr>
            <td>Full Name<label style="color: red">*</label></td>
            <td>:</td>
            <td><input type="text" name="name" class="text_box"/></td>
        </tr>
        <tr>
            <td>Contact<label style="color: red">*</label></td>
            <td>:</td>
            <td><input type="text" name="name" class="text_box" /></td>
        </tr>
        <tr>
            <td>Contact (Optional)</td>
            <td>:</td>
            <td><input type="text" name="name" class="text_box" /></td>
        </tr>
        <tr>
            <td>Email</td>
            <td>:</td>
            <td><input type="text" name="name" class="text_box" /></td>
        </tr>
        <tr>
            <td>Course<label style="color: red">*</label></td>
            <td>:</td>
            <td>
                <select name="course">
                    <?php
                    $courses = $f->get_courses();
                    foreach($courses as $c){ ?>
                        <option value="<?php echo $c[1];?>"><?php echo $c[1];?></option>
                    <?php
                    }
                    ?>

                </select>
            </td>
        </tr>
        <tr>
            <td>Address</td>
            <td>:</td>
            <td><input type="text" name="name" class="text_box" /></td>
        </tr>
    </table>
        <input type="submit" value="Submit">
<!--    </form>-->
    <?php
}
?>
<?php echo '</center>';?>
</body>
</html>

这是我的 Instant_search.php:

if (isset($_POST['search'])) {
$word = mysql_real_escape_string($_POST['search']);
$res = $f->select_name($word);
if(mysql_num_rows($res) > 0) {
    //echo 'Not available, choose another one';
} else {
    echo $word;
}
}

我想要的很简单。

  1. 我只想检查$word数据库中是否可用。如果没有,则将其设置为隐藏字段(uname)的值。然后将表单提交到另一个 php 文件并创建帐户。

  2. 这里使用了两种形式,这也产生了问题。

请帮我完成这项工作。提前致谢。

4

1 回答 1

1

您需要从 PHP 返回特定代码并在 AJAX 调用的成功回调中对其进行测试。

即时搜索:

if (isset($_POST['search'])) {
    $word = mysql_real_escape_string($_POST['search']);
    $res = $f - > select_name($word);
    if (mysql_num_rows($res) > 0) {
        //The word is not in DB, then specify error in front of it
        echo '[error]'.$word;
    } else {
        echo $word;
    }
}

在您的成功回调中:

success: function (html) { // this happens after we get results
    if(html.search('[error]') >= 0)
    {
       //Error : set your input field with returned text
       $('#uname').val(html.split('[error]')[1]);

       //Call your second form here           
    }
    else
    {
        //No error
        $(".results").show();
        $(".results").append(html);
    }
}
于 2013-02-19T08:59:05.297 回答