Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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 我可以使用ajax按钮获取下拉值并填写表单吗_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 我可以使用ajax按钮获取下拉值并填写表单吗

Javascript 我可以使用ajax按钮获取下拉值并填写表单吗,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我一直在尝试使用Jquery和一个下拉框来填充输入表单,但没有成功,我正在尝试找到一种方法来解决这个问题,直到我最终能够解决它。我想知道的是,如果可以用sql填充下拉列表,那么一旦选择了该值,就运行sql命令来获取数据行的其余部分,然后$\u进入输入字段 我一直在查看示例,但大多数示例都输出表数据,并且找不到任何可以拉入输入字段的内容,因此我可以将表单处理到数据库中 我只是在寻找一个想法或方向,我应该寻找这样一个任务,任何帮助将不胜感激。TEST.php <?

我一直在尝试使用Jquery和一个下拉框来填充输入表单,但没有成功,我正在尝试找到一种方法来解决这个问题,直到我最终能够解决它。我想知道的是,如果可以用sql填充下拉列表,那么一旦选择了该值,就运行sql命令来获取数据行的其余部分,然后$\u进入输入字段

我一直在查看示例,但大多数示例都输出表数据,并且找不到任何可以拉入输入字段的内容,因此我可以将表单处理到数据库中

我只是在寻找一个想法或方向,我应该寻找这样一个任务,任何帮助将不胜感激。

TEST.php

       <? 


       if(isset($_GET['name'])) {
       /*Some queries if need*/
       echo '{"name":"yiu send name ['.$_GET['name'].'] and may be some other data'.'"}';
       }
        else {

       $bd_host='localhost';
       $bd_user='root';
       $bd_password='';
       $bd_base='db_name';

       /*connect to db*/
       $con=mysql_connect($bd_host, $bd_user, $bd_password); 

       /*control connection*/
       if(!con) {
       echo 'Error.'; 
       exit; 
       }

       /*select databese name*/
       mysql_select_db($bd_base, $con); 

       /*set encode to utf-8*/
       mysql_query('SET NAMES utf8');



       $sql="Select name from users limit 10"
       $res=mysql_query($sql);
       ?>
       <head>
       <style>
       div{background-color:red;width:150px;color:white;}
       ul{display:none;position:absolute;width:150px;margin:0;padding:0;background-color:yellow;color:black;}
       div:hover ul{display:block;}
       .filled{background-color:blue;}
       </style>
       </head>
       <div>button<ul>
       <?
       While($row=mysql_fetch_assoc($res)) { ?>
       <li onclick="ajax('<? echo $row['name']; ?>');return false;"><? echo $row['name']; ?></li>
       <?
       }
       ?>
       </ul></div>
       <input type="text" value="none" id="id1">
       <?
       }
       ?>

       <script>           
       function ajax(name) {
       var url='/test.php?name='+name;

        if (window.XMLHttpRequest) {
                   xmlhttp = new XMLHttpRequest();
               } else if (window.ActiveXObject) {
                   xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
               }
                   xmlhttp.open('GET', url, true);

               xmlhttp.onreadystatechange = function() {
                   if (xmlhttp.readyState == 4) {
                       myfunction(xmlhttp.responseText);
                   }
               }

                   xmlhttp.send(null );


               function myfunction(response) {
       var= arr; 
       arr = JSON.parse(response);
       document.getElementById('id1').value=arr.name;
       document.getElementById('id1').className='filled';
       alert ('The input was filled with '+arr.name);
       }

       }
       </script>    

      /*END of test.php*/       

div{背景色:红色;宽度:150px;颜色:白色;}
ul{显示:无;位置:绝对;宽度:150px;边距:0;填充:0;背景色:黄色;颜色:黑色;}
div:hover ul{display:block;}
.filled{背景色:蓝色;}
按钮
函数ajax(名称){ var url='/test.php?name='+name; if(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }else if(window.ActiveXObject){ xmlhttp=newActiveXObject('Microsoft.xmlhttp'); } open('GET',url,true); xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4){ myfunction(xmlhttp.responseText); } } xmlhttp.send(空); 功能myfunction(响应){ var=arr; arr=JSON.parse(响应); document.getElementById('id1')。value=arr.name; document.getElementById('id1')。className='filled'; 警报('输入用'+arr.name'填充); } } /*测试结束。php*/
好吧,通过使用和编写自己的PHP功能,您可以轻松做到这一点

您需要一个HTML选择框:

<select id="myselectbox">
    <option>Select</option>
</select>
<input type="text" id="myinputbox">

我希望您现在能有一些想法:)

可能的重复您必须提供代码示例,这样我们就可以看到您已经尝试了@Glenn Zealous什么PHP框架用于此目的?或者只使用一个.php文件?这几乎是完美的,非常感谢,我现在可以看到如何执行这样一个函数。但是我确实有一个问题,因为我可以看到函数正在工作,但是sql没有填充到下拉列表中,而是在回显文件的一行。然后,当选择打开时,它会将邮政编码回显到收件箱中。它的核心是工作,但在添加到脚本之前,我仍然需要对其进行更多的调整。@Glenzealus很高兴知道它帮助了您:)而您刚才问我的问题,我无法理解。我想你应该用你现在的代码发布另一个问题。所以,我或其他人将能够解决您的问题并帮助您:)我已经做到了,这有点像是向前迈出了一步,向后退了两步。我可以看出这个代码确实有效,但我只需要用一把椅子和一只包裹在椅子上的章鱼在它脸上打几下就行了(很好,很愉快的编码:)
<script type="text/javascript">
$( document ).ready(function() {

  $.get( "myfunctions/get_records_for_select.php", function( data ) {
    //Look we will change the HTML content for our select-box on success
    $( "#myselectbox" ).html( data );
  });

  $( "#myselectbox" ).change(function() {

    $.post( "myfunctions/get_records_by_id.php", { selectedId: $(this).val() })
    .done(function( data ) {
      $( "#myinputbox" ).val( data );
    });

 });
});
</script>
// your get_records_for_select.php file

$yourSql = "select ...";
$results = run_sql($yourSql);
$html = "<option>Select</option>";
foreach($results as $record){
    $html .= "<option value='".$record['id']."'>".$record['title']."</option>";
}

echo $html;exit;
// your get_records_by_id.php file
$post_data $_POST['selectedId'];
$yourSql = "select ... WHERE id=".$post_data;
$results = run_sql($yourSql);
$row = $results->row();
echo $row['yourvalue'];exit;