2013-02-19 94 views
0

我有一个即时搜索程序,它是从tutorial 收集的。我修改了一些代码行。下面是文档:将值设置为隐藏字段

<!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. 这里有两种形式被使用,这也是造成问题的原因。

请帮我做好这份工作。提前致谢。

+0

请解释一下什么是你所面对的,而不是寻求整体解决方案的问题。 – 2013-02-19 12:27:37

回答

1

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

instant_search:

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); 
    } 
} 
+0

我想设置值为隐藏元素,如果没有错误 – 2013-02-19 09:53:40

+0

我修改了你的代码。并为我工作。谢谢 – 2013-02-19 10:09:12