donglu9743 2014-03-31 09:44
浏览 63

使用json的Jquery AJAX函数

hey all i'm a bit new in the world of ajax and i couldn't find the reason what is the problem with my code. i'm building a form which i would like the page to show using autocomplete the content already exists in the database.

i want to use jquery ajax function to update the value of a string from a php file.

but unfortunately i keep get the error function and not the success one.

thank you in advance.

this is my html code

    <div id="first_step" style='border-right:1px dotted grey; padding:0 10px 0 0'>

        <div class="image123">
        <img src="img/start.jpg" height="140" width="140" align= "top|left">
        </div>

        <h3> Behaviours Type </h3>
        <p align='justify'> Here you can set the different types of behaviours.<br></p>
        <form method="POST" id="type_form" action="behaviour.php">

            <h5> behaviour type: <input type="text" class= "autocomplete" name="type" id="behaviour_type" ><br> </h5>
            <h5> short description: <input type="text" name="type_description" id="type_description"  ><br></h5>
            <input type="submit" value="Add type" id= "add_type" name="add_type">

        </form>
    </div>
</div>

this is my javascript

<script type="text/javascript">
    var tags;


    $('.autocomplete').on('click', function(e){
    e.preventDefault();


        x=$(this).attr('id');
        console.log(x);
    jQuery.ajax({                                      
    type: "POST",
    url: "behavior_autocomplete_queries.php",
    data:"field_name="+x,
    dataType:"json",
    success:function(ajax){     
       tags=ajax;
    },
    error:function(){
               console.log("There was a problom with the Database please    try again later");
            }
      });

    }); 
</script>

the string which i would like to update is called tags and im trying to do so using a mysql query which works and should received from te following php file called behavior_autocomplete_queries.php which located at the same folder as this html file.

and this is the php file - the database_connection. php file checked and it work

    if ($con==false)
    {
        echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('We are sorry, there was a problem with the server, Please try again later') </SCRIPT>");
    } 
    else
    {

switch ($_POST["field_name"])
{
    case "behaviour_type":
    {
    $sql="select name from behaviour_incident_type";
    break;
    }

    case "antecedent":
    {
    $sql="SELECT name FROM behavior_incident_antecedent";
    break;
    }

    case "behaviour":
    {
    $sql="select name from behaviour_incident_behaviour";
    break;
    }
    case "Consequence":
    {
    $sql="select name from behaviour_incident_consequence";
    break;
    }

}
$result=mysqli_query($con,$sql);

// Fetch all
$all_data= mysqli_fetch_all($result,MYSQLI_ASSOC);

for ($i=0;$i<count($all_data);$i++){
$temp=($all_data[$i]);
$curr[$i]=($temp["name"]);

    }

 $comma_separated = implode('", "', $curr);
 $intial='"';
 $end='"';

 $return_string = '"'.$comma_separated.'"';


mysqli_free_result($result);

mysqli_close($con);
}

    echo json_encode($return_string);




     ?> 
  • 写回答

1条回答 默认 最新

  • dongyingtang3803 2014-03-31 10:18
    关注

    Maybe this will help you.

    1. Use the jQuery change:

      $('.autocomplete').change(function() { ... });
    2. Get the data with XMLHttpRequest

      
      if (window.XMLHttpRequest) {
          var xmlhttp = new XMLHttpRequest();
      } else {
          var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      
      xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
              $( "#target" ).append(xmlhttp.responseText);
          }
      }
      
      xmlhttp.open("GET", "behavior_autocomplete_queries.php", true);
      xmlhttp.send();
      
    3. Use the jQuery Autocomplete

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值