Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将值设置为隐藏字段_Javascript_Html_Ajax - Fatal编程技术网

Javascript 将值设置为隐藏字段

Javascript 将值设置为隐藏字段,javascript,html,ajax,Javascript,Html,Ajax,我有一个即时搜索程序,它是从一个 . 我修改了一些代码行。文件如下: <!DOCTYPE html> <html> <head> <title></title> <link href="styles.css" rel="stylesheet" type="text/css" /> <script type="text/java

我有一个即时搜索程序,它是从一个 . 我修改了一些代码行。文件如下:

<!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>
我想要的很简单

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

  • 这里使用了两种形式,这也造成了问题


  • 请帮我做这项工作。提前感谢。

    您需要从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);
        }
    }
    

    如果没有错误,我想设置隐藏元素的值。我修改了你的代码。而且为我工作。谢谢你请解释你面临的问题,而不是要求整个解决方案。
    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);
        }
    }