简体   繁体   中英

Simple php to Codeigniter

i am developing my final year project on codeigniter and facing problem i want to click a drop-down value and then it will return table from db but alas i can't do

this is plain php code that i want to convert into codeigniter please help me thanks in advance.

<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

You need to separate the code in Controller, View and Model:

  • Model for DDBB operations.

  • Controller: control where the data goes and how it can be displayed, and other logic.

  • View, here is where the form is rendered.

Codeigniter has a great documentation to learn how to do it.

Controllers

Views

Models has the same url, but for models, i've not reputation enough to post 3 links, sorry

You can do like this

$(document).on("change","#select",function(){
    $.ajax({
        url:""// path to your controller function. here keep all the code which is present in your getuser.php in to a function in controller
        data:{"key":value,"key":value}
        dataType:"HTML",
        success:function(data){
              $("#txtHint").html(data)// this you can do 2 things, either manipulate all your data in controller 
              //and directly change the html of the div, or u can get the required data 
        }

    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM