javascript - PHP/HTML button picture change and script call -
i have page buttons user clicks on. when button gets clicked need change image , call php script save change.
i somehow can't button both, changes pic won't call php.
code:
<?php $db = new pdo('mysql:host=localhost;dbname=mysettings;charset=utf8mb4', 'testuser', '1234567890'); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $db->setattribute(pdo::attr_emulate_prepares, false); try { $mytable = $_session["sess_myuserid"]; if($mytable == null) {$url='login.php'; echo '<meta http-equiv=refresh content="1; '.$url.'">';} $stmt = $db->prepare("select * ".$mytable); $stmt->execute(); $result = $stmt->fetchall(pdo::fetch_assoc); $conn = null; echo'<script>'; $count = count($result); ($i = 0; $i < $count; $i++) { $theid = $result[$i]['id']; $thefunction = $result[$i]['thefunction']; $thesetting = $result[$i]['thesetting']; if ($thesetting == "0"){ echo 'var newsrc'.$theid.' = "on.jpg";'; } else{ echo 'var newsrc'.$theid.' = "off.jpg";'; } echo 'function changeimage'.$theid.'() {'; echo 'if ( newsrc'.$theid.' == "off.jpg" ) {'; echo "document.getelementbyid('pic".$theid."').src = '/images/boff.png';"; echo "$('#newcode').load('monitor.php?id=".$theid."&table=".$mytable."');"; echo 'newsrc'.$theid.' = "on.jpg";'; echo '}'; echo 'else {'; echo "document.getelementbyid('pic".$theid."').src = '/images/bon.png';"; echo "$('#newcode').load('monitor.php?id=".$theid."&table=".$mytable."');"; echo 'newsrc'.$theid.' = "off.jpg";'; echo '}'; echo '}'; } echo'</script>'; $myleft=0; $mytop=0; $myrow=0; ($i = 0; $i < $count; $i++) { $theid = $result[$i]['id']; $thefunction = $result[$i]['thefunction']; $thesetting = $result[$i]['thesetting']; if ($thesetting == "0"){ $thepic = "images/boff.png"; } else{ $thepic = "images/bon.png"; } echo '<div>'; echo '<a href="#" onclick=changeimage'.$theid.'()><img src="'.$thepic.'" alt="" id="pic'.$theid.'" style="position:absolute;left:'.$myleft.'px;top:'.$mytop.'px;width:63px;height:30px;"> </a>'; echo '</div>'; $mytop=$mytop + 30; $myrow=$myrow + 1; if ($myrow == 12){ $myrow=0; $mytop=0; if ($myleft == 0){ $myleft = 292; } else { $myleft = 615; } } } } catch(pdoexception $e) { $url='login.php'; echo '<meta http-equiv=refresh content="1; '.$url.'">'; } ?>
fixed : changed script function. added hidden div called load php outputs nothing stays invisible.
echo'<script>'; $count = count($result); ($i = 0; $i < $count; $i++) { $theid = $result[$i]['id']; $thefunction = $result[$i]['thefunction']; $thesetting = $result[$i]['thesetting']; if ($thesetting == "0"){ echo 'var newsrc'.$theid.' = "on.jpg";'; } else{ echo 'var newsrc'.$theid.' = "off.jpg";'; } echo 'function changeimage'.$theid.'() {'; echo 'if ( newsrc'.$theid.' == "off.jpg" ) {'; echo "document.getelementbyid('pic".$theid."').src = '/images/boff.png';"; echo 'newsrc'.$theid.' = "on.jpg";'; echo '}'; echo 'else {'; echo "document.getelementbyid('pic".$theid."').src = '/images/bon.png';"; echo 'newsrc'.$theid.' = "off.jpg";'; echo '}'; echo' $("#htmlconvo").load("monitor.php?id='.$theid.'&table='.$mytable.'");'; echo '}'; } echo'</script>';
Comments
Post a Comment