Jump to content

[Help!] Getting Data From Database In Javascript?

Recommended Posts

Hello, I am installing a javascript imageslider on my website. But I want to be able to store the "image-link" in a database, so every user can have his own images. This comes from the slidercode (jas.js):

var JaS = {// Customization parametersimagePath : "../pictures/",images : [["1.jpg", "Bat bridge in Austin"],["2.jpg", "Blossoming tree", "Tree"],],

A lot of other vars are beneath here. ---------- Right now it works with this:

<button type="button" onclick="showUser(this.value)" value="<?php echo $_SESSION['myusername']; ?>">Change content</button>		<br />		<div id="myDiv"><h2>Let AJAX change this text</h2></div>

Javascript code I got mostly from w3schools.com:(str = <?php echo $_SESSION['myusername']; ?> --> username of the current user)

function showUser(str){if (str=="")	{	 document.getElementById("myDiv").innerHTML="no username";	 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("myDiv").innerHTML=xmlhttp.responseText;   }   }xmlhttp.open("GET","../modules/get.php?q="+str,true);xmlhttp.send();}

This is my php file (get.php):(it has a database connection)

<?php$q=$_GET["q"];$sql="SELECT * FROM pictures WHERE username='".$q."'";$result = mysql_query($sql);while($row = mysql_fetch_array($result))  {  echo "['" . $row['path'] . "', '" . $row['title'] . "'], </br>";  }?>

The output is:

['1.jpg', 'Bat bridge in Austin'],['2.jpg', 'Blossoming tree'],

---------- So I want to combine the 2 codes so the image slider will select the right images automatically by connection to the database and getting the "image-links" which correspond with the username. I want this to happen without having to push a button (--> automatically :P). I'm not an experienced user/writer of javascript at all, so I hope someone on this forum can help me out. ~ xemx

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...