javascript - Unable to get data from database using AJAX & PHP -
update: first part of question has been solved , has been updated below working code. ~
i’m working on javascript application , i’m having difficultly getting ajax call work.
i’m able insert data database using ajax post & php can’t seem pull data database.
i have javascript application uses image, gets image location in root folder this:
img.src = 'picture1.jpg'; instead of doing this, want select random image table in database every time javascript application loads.
i’ve created table single column, , populated addresses/locations of images contained in folder in root directory.
for example:
/images/00001.jpg /images/00002.jpg /images/00003.jpg this php file (located @ /scripts/imagerandomizer.php) i’m using call random image address:
<?php session_start(); $servername = "localhost"; $username = "myusername"; $password = "mypassword"; $dbname = "mydatabase"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $sqlrandomize = mysqli_query($conn,"select images `images` order rand( ) limit 0 , 1"); $row = mysqli_fetch_row($sqlrandomize); header('content-type: application/json'); echo json_encode($row); mysqli_close($conn); ?> and ajax initiates php & listens echo:
function getrandomimage() { $.ajax({ url: 'scripts/imagerandomizer.php', data: "", datatype: 'json', success: function(response){ alert(response); }}); }; i’m trying use alert(data); have randomly chosen image location/address appear in alert box (just see if it’s working). it’s not working me, i’m pretty sure i've made mistake somewhere, , i’m not sure if json right data type use here?
i have returned address replace current img.src = 'image.jpg'; , when javascript application starts, receive random image img.src = section of code.
thanks on this!
update:
the javascript can correctly display random address (using either “alert” or “console.log”) every time it’s loaded. last part of question concerns how have .js file read string, , use location of image fetches.
this how game set up: have file named “game.js”, contains code needed game operate, right now, part of code this: img.src = 'images/image00001.jpg'; right image permanently defined , doesn’t change. i’m trying replace static definition randomized one. i’m trying randomized address appear after img.src = whenever game.js loads.
i need make sure event happens before rest of game.js code initiates, need randomly chosen image file in place before rest of game loads. i’ve tried defining img.src including img.src=(response) in ajax call @ top of game.js file it’s failing load image game. i’m thinking maybe wrong way this?
2nd update
hi @phpglue i’ve been trying work days i’m still missing something.
this function grab randomized image, , i’ve tried place code run game in success function if (img.src) {//code run game here}:
function getrandomimage() { $.ajax({ url: 'scripts/imagerandomizer.php', data: "", datatype: 'json', success: function(response){ $('#imageid').attr('src', data.image); img.src = $('#imageid'); if (img.src) { //code run game here } }}); }; i’m missing here, think i’m not understanding mean correctly. i’d appreciate advice on , thank again taking time @ question!
3rd update
hi, current code is:
function getrandomimg(gamefunc){ $.post('scripts/imagerandomizer.php', {randimg:1}, function(data){ var im = $('#imageid'); im.attr('src', data.image); im.load(function(){ gamefunc(img.src=im); } }, 'json'); } getrandomimg(function(){ javascriptgame(); }); function javascriptgame(){ //in area i’ve placed code make game work } when said /* pass args gamefunc here */ entered img.src=im. (i’m not sure understand correctly think i’m supposed define img.src= game call in line?
when said // game code here - pass function name instead of anonymous function, created new function called javascriptgame, inside placed game’s code, , called function @ line. i’m not sure if that’s meant me do?
unfortunately right there’s still no image loading game, want thank again taking time me , if offer more advice awesome! much.
the problem mysqli_query() should mysqli_query(connection,query,resultmode) connection , query required , resultmode optional.
example: mysqli_query($conn,"select images images order rand( ) limit 0 , 1");
for more details please check out mysqli_query
Comments
Post a Comment