PHP Combobox last selected same after changing page - javascript

Can all help me with my php. i can keep my combobox not changing after reload. but after im changing a page in my paging. it not work can you help me?
THIS IS MY PHP.
PHP
Category :
>All
>Computer
>Peripheral
>Gadget
";
else
echo "";
echo " Category : ".$baris["Category"]."";
echo " ";
echo " ";
echo " ";
echo " ";
echo " Nama : ".$baris["Nama"]. "";
echo " Harga : Rp. ".$newprice. "";
echo " Keterangan : ".$baris['Keterangan']."";
echo " ";
echo "";
echo "";
$no=$no+1;
}
?>
$i ";
}
?>
</div>
<div class="footer">
(C)
</div>
</div>
</body>
</html>

I think the problem lies on the line (roughly 101;):
for ($i=1;$i<=$jum_page;$i++){
echo "<a href = index.php?page=$i>$i</a> ";
}
The middle line should read:
echo "<a href=index.php?page=$i&choice=$selected_choice>$i</a> ";
This will ensure that the combo box choice is preserved across pages. Is this problem you were experiencing?

Related

Output database row in JavaScript loop

I want to make a slideshow of data from database. I use php to print out the rows, then I placed that in a html code, then I want to use javascript to loop the code using the setInterval() loop.
My question is: how can I output the rows and use javascript to display that? because what I tried to do didn't work. I'm not experienced with javascript, and I tried to look up and try out different methods but it doesn't work for some reason.
This is what I tried to do last, but it does not print anything on page.
...
$sel_query = "SELECT * FROM `classifieds` WHERE `title` !=''";
$results = mysqli_set_charset($conn,"utf8");
$results = mysqli_query($conn,$sel_query);
while ($row = mysqli_fetch_array($results)) {
?>
...
<body>
<div id="div"> </div>
<script type="text/javascript">
setInterval(displayCode, 1000);
var codeBlock = '<div id="slider-frame" class="slider-frame">' +
'<div class="slide-images">' +
'<div class="img-container">' +
'<h1>' + '<?php echo $row['title']; ?>' + '</h1>' +
'</div>' +
'<p>' + '<?php echo $row['description']; ?>' + '</p>' +
'</div> </div>';
function displayCode() {
document.getElementById("div").innerHTML += codeBlock;
}
</script>
<?php } ?>
</body>
</html>
My problem was that the in my javascript code, did not work. It did not echo the row I wanted. I figured that this was happening because of the + in the var codeBlock. After deleting them and making the var one string it worked.
<script type="text/javascript">
setInterval(displayCode, 1000);
function displayCode() {
document.getElementById("sliderFrame").innerHTML += '<div id="sliderFrame" class="slider-frame"> <div class="slide-images"> <div class="img-container"> <h1> <?php echo $row['title']; ?> </h1> </div> <p> <?php echo $row['description']; ?> </p> </div> </div> ';
}
</script>

Dynamically created script not working

I am trying to dynamically create a script but it is not working.
I tried:
echo '<script type="text/javascript">alert("hello!");</script>';
echo '<script>alert("hello!");</script>';
echo '<script type="text/javascript">$( document ).ready(function() {alert("hello!");});</script>';
please help
Upon request here is the full code where the script is dynamically created at:
In fact , there is a php page that has a javascript file that calls an ajax function on press of a button that sends the type and calls this php page , this one will dynamically create the bootstrap carousel with the right pictures (give the right category)
<?php
//the login database info goes here too
$_type= $_POST['Type'];
$_category= $_POST['Category'];
if($_type == "adult")
{
$title = "FOR THE ADULTS";
}
else
$title = "FOR THE KIDS";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT image,imagealt FROM gallery where type='".$_type."' AND category='".$_category."' ";
$result = $conn->query($sql);
$result1 = $conn->query($sql);
?>
<tr>
<td class="GalleryImageInfo">
<div id="parent_back" >
<h1>GALLERY</h1>
<p class="ForAdults"> <?php echo $title ?> </p>
<p class="ButtonGalleryPreview" style="text-align:center"> <?php echo $_category ?> </p>
<?php
$countit = 0;
if ($result->num_rows > 0) {
// output data of each row
?>
<div id="slider_caption" > <div>
<?php
while($row = $result->fetch_assoc()) {
$image_description = $row["imgdesc"];
echo "
<div class=\"carousel-caption caption-".$countit."\">
<h3>Description</h3>
<p>".$image_description."</p></div>";
$countit++;
}
echo "</div></div>";
echo "<p class=\"GalleryImageBack\" style=\"font-family:BandaBold; color:#6c555e; font-size:15px;\"><i class=\"fa fa-arrow-circle-left\" style=\"margin-right:10px; font-size:15px;\" ></i>BACK</p>
</div>
</td>
<td style=\"width:100%\">
<div class=\"container\" style=\"width:100% ; padding:0px !important;\">
<div id=\"myCarousel2\" class=\"carousel slide\" data-ride=\"carousel\">
<!-- Wrapper for slides -->
<div class=\"carousel-inner\">";
$firstitem = true;
while($row = $result1->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
$image_url = $row["image"];
$image_alt = $row["imagealt"];
if($firstitem)
{ echo"<div class=\"item active\">
<img src=\" $image_url \"
alt=\".$image_alt.\" style=\"width:100%;\">
</div>";
$firstitem= false;
}
else
echo"<div class=\"item\">
<img src=\" $image_url \"
alt=\".$image_alt.\" style=\"width:100%;\">
</div>";
}
echo " </div>
<!-- Left and right controls -->
<a class=\"left carousel-control\" href=\"#myCarousel2\" data-slide=\"prev\">
<span class=\"glyphicon glyphicon-chevron-left\"></span>
<span class=\"sr-only\">Previous</span>
</a>
<a class=\"right carousel-control\" href=\"#myCarousel2\" data-slide=\"next\">
<span class=\"glyphicon glyphicon-chevron-right\"></span>
<span class=\"sr-only\">Next</span>
</a>
</div>
</div>
</td>
</tr>";
echo "<script type=\"text/javascript\">$(document).ready(function() {
alert(\"hello!\");
});</script>";
}
else {
echo "0 results";
}
$conn->close();
?>
check if you added the jquery script in this page or not.

passing a MySQL data as string through PHP to javascript

I am trying create a social media site.
In the fourth line, the onclick='func_cmnt_open(".$row['post_ID'].") should display a div unique to each entry. it seems like a syntax error but I'm unable to narrow it down if its in PHP or javascript.
In line 6, id attribute was set to comment".$row['post_ID']." to make each div unique from the while loop iteration.
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>".$row["username"]." said ".$row["cm_text"]." at ".$row["time"]." comment".$row['post_ID']."</h4>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(".$row['post_ID'].")'>☰</button>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>";
echo "<div class='w3-black' id='comment".$row['post_ID']."' style='display: none'>";
echo " <div class='w3-container''>";
echo " <h4>";
echo " ///code was here ";
echo " </h4>";
echo " <button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById('id02').style.display='block''>☰</button>";
echo " <div id='id02' class='w3-modal'>";
echo " <div class='w3-modal-content w3-card-4'>";
echo " <header class='w3-container w3-teal'> ";
echo " <span onclick='document.getElementById('id02').style.display='none'' class='w3-button w3-display-topright'>×</span>";
echo " ///code was here";
echo " </header>";
echo " <div class='w3-container'>";
echo " <form class='w3-container' action='home.php' method='GET'>";
echo " <p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>";
echo " <p><button class='w3-button w3-black'>Post</button></p>";
echo " </form>";
echo " </div>";
echo " <footer class='w3-container w3-teal'>";
echo " <p>nothing here</p>";
echo " </footer>";
echo " </div>";
echo " </div>";
echo " </div>";
echo "</div>";
}
}
JAVASCRIPT:
i had tried using string concat to join "comment" and "num" to get the unique id of div. that did not work either.
function func_cmnt_open(num) {
if (document.getElementById('comment'+num.tostring()).style.display == "none")
{
document.getElementById('comment'+num.tostring()).style.width = "100%";
document.getElementById('comment'+num.tostring()).style.display = "block";
}
else
{
document.getElementById('comment'+num.tostring()).style.width = "0%";
document.getElementById('comment'+num.tostring()).style.display = "none";
}
}
OUTPUT:the selected button should invoke the javascript with arguments from PHP code and display a black div where i can enter comments
I'm not sure of this is the most efficient way of doing it. please advice if there are any other way also to achieve the same output.
If post_ID is not strictly a string you will get a syntax error in Javascript.
... onclick='func_cmnt_open(".$row['post_ID'].")'...
Consider this value 1 Above will create this in the page source code.
... onclick='func_cmnt_open(1)'...
Now consider this value post1
... onclick='func_cmnt_open(post1)'...
In the second case it should be
... onclick='func_cmnt_open("$row['post_ID']")'...
To get that in PHP with the way you have echo you'll have to escape double quotes or the ' in the attribute will also cause issues. So like this:
... onclick='func_cmnt_open(\"".$row['post_ID']."\")'...
There were a few other quoting issues onclick='document.getElementById('id02').style.display='block'' ..>, I don't know if I got them all but should be better.
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>{$row["username"]} said {$row["cm_text"]} at {$row["time"]} comment{$row['post_ID']}</h4>
<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(\"{$row['post_ID']}\")'>☰</button>
<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>
<div class='w3-black' id='comment{$row['post_ID']}' style='display: none'>
<div class='w3-container''>
<h4>
///code was here
</h4>
<button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById(\"id02\").style.display=\"block\"'>☰</button>
<div id='id02' class='w3-modal'>
<div class='w3-modal-content w3-card-4'>
<header class='w3-container w3-teal'>
<span onclick='document.getElementById(\"id02\").style.display=\"none\"' class='w3-button w3-display-topright'>×</span>
///code was here
</header>
<div class='w3-container'>
<form class='w3-container' action='home.php' method='GET'>
<p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>
<p><button class='w3-button w3-black'>Post</button></p>
</form>
</div>
<footer class='w3-container w3-teal'>
<p>nothing here</p>
</footer>
</div>
</div>
</div>
</div>";
}
}

get event of ul li javascript

I have a problem of access to certain class, because I've a list that descent of data base and when I select an item, select all.¿Why?
I think that I need acces with the event.
This is the script:
$(document).ready(function(){
$("#contenedor_central .boton_opciones").click(function(e){
e.preventDefault();
if ($("#contenedor_central .menu_noticia").hasClass('desactivado')){
$("#contenedor_central .menu_noticia").css({"display":"table"});
$("#contenedor_central .menu_noticia").removeClass('desactivado');
$("#contenedor_central .menu_noticia").addClass('activado');
}else{
$("#contenedor_central .menu_noticia").removeClass('activado');
$("#contenedor_central .menu_noticia").css({"display":"none"});
$("#contenedor_central .menu_noticia").removeClass('activado');
$("#contenedor_central .menu_noticia").addClass('desactivado');
}
});
});
This script show all ul and I want only the clicked element to be shown
This is the php:
<?php
echo '<div id="contenedor_central" class="contenedor_central">';
$conexion=mysqli_connect("***", "***", "", "***");
if(!$conexion){
echo "La conexion ha fallado : " . mysqli_error();
exit();
}
$tamano_paginas=6;
if(isset($_GET["pagina"])){
if($_GET["pagina"]==1){
header("Location: index.php");
}else{
$pagina=$_GET["pagina"];
}
}else{
$pagina=1;
}
$empezar=($pagina-1)*($tamano_paginas);
$consulta="SELECT * FROM contenidoblog";
if($resultado=mysqli_query($conexion, $consulta)){
$num_filas=mysqli_num_rows($resultado);
$total_paginas=ceil($num_filas/$tamano_paginas);
}
$consulta_filtrada="SELECT * FROM contenidoblog LIMIT $empezar, $tamano_paginas";
$resultado1=mysqli_query($conexion, $consulta_filtrada);
if($resultado1){
while($registro= mysqli_fetch_assoc($resultado1)){
echo "<div class='opciones'>
<ul class='menu_noticia desactivado'>
<li>Favoritos</li>
<li>Compartir</li>
<li>Me gusta</li>
<li>Comentar</li>
</ul>
</div>";
echo "<div id='cajaContenedor'> ";
if($registro['imagen']!=""){
echo "<img src='imagenes/" . $registro['imagen'] . "' />";}
echo"<div id='contenedor_titular_cabecera'> <i id='btnn' class='boton_opciones fa fa-bars fa-lg' aria-hidden='true'></i>
<a href='*'".$registro['cabecera']."&id=".$registro['id']."'>";
echo "<p> ". $registro['fecha'] ."</p>";
echo "<H2>" . $registro['cabecera'] . "</H2>";
echo "<p>". $registro['titular'] . "</p>";
echo "</a>";
echo "</div>";
echo "</div>";
}
}
mysqli_close($conexion);
for($i=1;$i<=$total_paginas;$i++){
echo "<a href='?pagina=". $i . "'/>" . $i . "</a> ";
}
echo "<BR></BR>";
echo "</div>";
?>
This is the image
Because $("#contenedor_central .menu_noticia") will choose all the li.
You have to distinguish between your current click li.
You can add eventListener for all the $("#contenedor_central .menu_noticia") and use $(this) to choose your current click li.
$("#contenedor_central .menu_noticia).click(function(e){
e.preventDefault();
if ($(this).hasClass('desactivado')){
$(this).css({"display":"table"});
$(this).removeClass('desactivado');
$(this).addClass('activado');
}else{
$(this).removeClass('activado');
$(this).css({"display":"none"});
$(this).removeClass('activado');
$(this).addClass('desactivado');
}
})

why does this one jquery work and the other does not

I am creating a cms and am using php to dynamically create jquery on the fly. The first below works fine when the php renders the jquery inlce in my code but the second only works if I place on my js function script which is called into the header of the page:
# start index video row
echo '<script type="text/javascript">';
echo "$('#indexVid').fancybox({"; echo " \n";
echo "'width' : '75%',"; echo " \n";
echo "'height' : '75%',"; echo " \n";
echo "'autoScale' : true,"; echo " \n";
echo "'transitionIn' : 'elastic',"; echo " \n";
echo "'transitionOut' : 'elastic',"; echo " \n";
echo "'type' : 'iframe'"; echo " \n";
echo "});"; echo " \n";
echo '</script>';
# open help
echo '<script type="text/javascript">';
echo '$("#open_testing").click( function(){ $("#info_testing").css("display","block"); } );'; echo " \n";
echo '$("#close_testing").click( function(){ $("#info_testing").css("display","none"); } );'; echo " \n";
echo '</script>';
$name = 'testing';
$this_label = 'Learn about Site Settings';
$info = 'Site Settings are variable aspects of the site which are common to all or most all of the pages on the site such as copyright information and such, or they are miscellaneous variables necessary to a specific aspects of the site such as the send mail limitation. To find out more about each click on the info icon.';
<p id="open_'.$name.'" class="info-link"><i class="icon-info-sign"></i> '.$this_label.'</p>
<div id="info_'.$name.'" class="hide info-wrap">
<h3>'.$this_label.'</h3>
echo $info;
<p class="center"><a id="close_'.$name.'" class="button small" title="Close Info Panel">
<i class="icon-remove-sign"> </i> Close</a></p>
</div>
The html is above
$("#open_testing").click( function(){ $("#info_testing").css("display","block"); } );
$("#close_testing").click( function(){ $("#info_testing").css("display","none"); } );
which is working if the above jquery is in the header but not if inline in the code
can some explain why? thanks

Categories

Resources