I have an ajax call which works as it should, it calls a PHP Prepared statement method which makes a database call and echo's some stuff. The problem is that it echoes it out somewhere else than it is supposed to, and I therefore have to move the code to another file and only that snippet of database code shall be moved. Well, my first file looks like this: (The code between the comments saying "//Specifically this code..." shall appear on a different page, or at least the echo statement shall.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['item_id'])){
$item_number = $_POST['item_id'];
require('../includes/db_connect.php');
/* Register a prepared statement */
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET rotation = (rotation + 1) % 4 WHERE ref_id = ?')) {
/* Bind parametres */
$stmt->bind_param('i', $item_number);
/* Execute the query */
$stmt->execute();
$stmt->bind_result($rotation);
$stmt->fetch();
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terribly wrong' . $mysqli->error;
}
//Specifically this code has to be moved to another file, but be called here. ->
if ($stmt = $mysqli->prepare('SELECT x, y, z, src, rotation, link, div_id FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?')) {
$stmt->bind_param('i', $item_number);
$stmt->execute();
$stmt->bind_result($x, $y, $z, $src, $rotation, $link, $div_id);
while($stmt->fetch()) {
if ($link != "") {
echo '<a href="' . $link . '"> ';
}
if ($div_id != "") {
echo '<a href="#" onClick="' . $div_id . '"> ';
}
echo '<img src="' . $src . $rotation .'.png" class="item' . $item_number . '" style="position:absolute; left:' . $x . 'px; top:' . $y . 'px; z-index:'. $z . ';">'; if ($x != 0) { echo'</a>'; }
}
} else {
echo 'Something went terrible wrong' . $mysqli->error;
}
$stmt->close();
// <- Specifically this code has to be moved to another file, but be called here.
}
}
?>
the place I want it to appear is here at the comment "//the echo statement..." the rest of this file which is not shown in this example shall not be called again, only the echo statement shall be updated here. It doesn't neccesarily have to be the entire query that should be moved, just the echo statements.
<?php if (login_check($mysqli) == true): ?>
<?php
require('database/refresh_house.php');
//the echo statement shall be echoed here !
?>
<!-- <div id="start"></div> -->
<!-- <div id="stop"></div> -->
<ul>
<li id="posX"></li>
<li id="posY"></li>
</ul>
I hope it makes sense, and I think it is a bit weird. I hope there's something that can refer the echo statement to another div and show it there, by still using the ajax.
EDIT: The ajax:
function rotateObject(e)
{
//e is handler which contains info about the item clicked. From that we can obtain the image id.
//since the id are of the form img_123(some number), we need to extract only the number.
var img_id = e.id.split("_")[1];
var xmlhttp;
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("item-2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","database/update_settings_rotate.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("item_id="+encodeURIComponent(img_id));
}
EDIT2: Sorry, didn't know all this code was neccesary. Thanks for helping me though. Here is the reason why the ajax says "item-2". However item-2 is just an example, it can be item-1 or item-6 or whatever, depending on how many item that exist. Here is the code:
$item_number = 0;
$rowsize = 12;
$itemArray = array();
$finalArray = array();
$results = 0;
for ($i = 0; $i < $rowsize; $i++) {
$stmt = $mysqli->stmt_init();
$stmt->prepare('SELECT z, name FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE house_room1.ref_id = ?');
$stmt->bind_param('i', $i
);
if ($stmt->execute()) {
$stmt->bind_result($z, $name);
while($stmt->fetch()) {
$results = 1;
$itemArray['number'] = $item_number;
$itemArray['name'] = $name;
$itemArray['ref_id'] = $z;
array_push($finalArray, $itemArray);
}
}
else {
echo 'Something went terribly wrong' . $mysqli->error;
}
$stmt->close();
$item_number++;
}
if($results == 1){
aasort($finalArray,"ref_id");
foreach($finalArray as $arr){
echo '<li id="item-' . $arr['number'] . '" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $arr['name'] . '
<img class="rotate" id="img_'.$arr['number'].'" src="images/house/other/settings.jpg" onclick="rotateObject(this)">';
}
}
//create a function for sorting
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}