javascript - Set interval with AJAX -
the purpose display div when click on button , display text inside div comes database. thing data in databse changes, text inside div also. need setinterval... ajax
i'm new in javascript , don't know way go...
html:
<div onclick="showdiv();"> click </div> <div style="display: none;" id="div"> info database: <span style="display: hidden;" id="data1"> data 1 </span> <span style="display: hidden;" id="data2"> data 2 </span> </div> javascript:
function showdiv() { document.queryselector("#div").style.display = "block"; setinterval(function () {getdata()}, 1000); } function getdata() { $.post( 'process.php', { }, function(data){ if(data == '1'){ document.queryselector("#data1").style.display = "inline"; } else if(data == '2'){ document.queryselector("#data2").style.display = "inline"; } }, 'text' ); return false; } //don't know how take data database without sending post or get.
php:
<?php select x database if(x == 1) {echo '1';} else if(x == 2) {echo '2';} ?>
you're not giving lot of info give basic example of getting data mysql database jquery, ajax , php.
first need include jquery head of document
<script src="http://code.jquery.com/jquery-latest.js"></script> and use ajax
function showdiv(){ document.getelementbyid("div").style.display = ""; setinterval(function (){ getdata('something'); }, 1000); } jquery.noconflict(); jquery(document).ready(function($){ getdata = function(variable){ var postvar = variable; var postvar2 = "exemple"; $.ajax({ type: "post", url: "php/file.php", data: 'variable=' + postvar + "&" + 'variable2=' + postvar2, success: function(data){ data = $.trim(data); var datasplit = data.split("++==09s27d8fd350--b7d32n0-97bn235==++"); if(datasplit[0] == "1"){ document.getelementbyid("data1").innerhtml = datasplit[1]; } if(datasplit[0] == "2"){ document.getelementbyid("data2").innerhtml = datasplit[1]; } } }); } }); finally, need create external php file (in example "file.php" in folder "php") data database mysqli
<?php // prevent error, check if post variable set , // if it's not full of spaces if(isset($_post['variable']) && preg_replace("/\s+/", "", $_post['variable']) != ""){ $con = mysqli_connect("hostname", "username", "password", "database_name"); $query = mysqli_query($con, "select * `table_name` `column_name` = '".$_post['variable']."'"); $results = array(); $row = 0; while($info = mysqli_fetch_array($query, mysqli_assoc)){ $results[$row] = array(); $results[$row]['column_name1'] = $info['column_name1']; $results[$row]['column_name2'] = $info['column_name2']; $row++; } foreach($results $result => $data){ echo "1" . "++==09s27d8fd350--b7d32n0-97bn235==++" . '<div>'.$data['column_name1'].'</div>'. '<div>'.$data['column_name2'].'</div>'; } } ?> hope helps!
Comments
Post a Comment