Changing css property of elements having similar kind of id's - php

I am having a problem while changing the css property of elements having similar kind if id's using javascript.
I am running a php loop through a series of hidden elements. So I am having there id's like this imgstat_1, imgstat_2 and so on.
The code i am using is
foreach($img as $img1){?>
<li>
<a href="#" onclick="changeimgstat('<?php echo $counter;?>')">
<img src="<?php echo $img1->vchEventImg?>" width="75" height="75" alt="" id="abc_<?php echo $img1->vchImgStatus?>" />
</a>
</li>
<input type = "hidden" value="<?php echo $img1->vchImgStatus?>" id="imgstat_<?php echo $counter;?>">
<?php $counter++;
}?>
<input type="hidden" value="" name="newimg" id="newimg">
and javascript like this
function changeimgstat(obj)
{
var xyz = document.getElementById('newimg').value;
alert(document.getElementById('abc_'+xyz));
document.getElementById('abc_'+obj).style.border="3px solid red";
var status = document.getElementById('imgstat_'+obj).value;
document.getElementById('newimg').value = status;
}
So could someone suggest what i am doing wrong

As I found there is no error in your code, but have you defined $counter before foreach loop. And your img id should be abc_<?php echo $counter?> rather than abc_<?php echo $img1->vchImgStatus?> which you are using in javascript.
Try:
CSS
.img3{border:3px solid red;}
.img{border:none;}
HTML and PHP
<?php
$counter=1;
foreach($img as $img1){?>
<li>
<a href="#" onclick="changeimgstat('<?php echo $counter;?>')">
<img class="img" src="<?php echo $img1->vchEventImg?>" width="75" height="75" alt="" id="abc_<?php echo $counter?>" />
</a>
</li>
<input type="hidden" value="<?php echo $img1->vchImgStatus?>" id="imgstat_<?php echo $counter;?>">
<?php $counter++;
}?>
<input type="hidden" value="" name="newimg" id="newimg">
SCRIPT
<script>
function removeAllClass()
{
var allimg=document.getElementsByClassName("img");
for(var index=0;index<allimg.length;index++)
{
allimg[index].className = "img";
}
}
function changeimgstat(obj)
{
removeAllClass();
document.getElementById('abc_'+obj).className="img img3";
var status = document.getElementById('imgstat_'+obj).value;
document.getElementById('newimg').value = status;
}
</script>

The best solution is to add a css class attribute to elements. And write styles for the class.
In the onclick function you can add the class to the elements

It seems you have:
<img ... id="abc_<?php echo $img1->vchImgStatus?>">
and
<input ... value="<?php echo $img1->vchImgStatus?>" id="imgstat_<?php echo $counter;?>">
Are you sure these two elements will have the id you expect?

try the following:
foreach($img as $img1){?>
<li>
<a href="#" onclick="changeimgstat('<?php echo $counter;?>')">
<img src="<?php echo $img1->vchEventImg?>" width="75" height="75" alt="" id="abc_<?php echo $counter;?>" />
</a>
</li>
<input type = "hidden" value="<?php echo $img1->vchImgStatus?>" id="imgstat_<?php echo $counter;?>">
<?php $counter++;
}?>
<input type="hidden" value="" name="newimg" id="newimg">
Javascript:
function changeimgstat(obj)
{
var xyz = document.getElementById('newimg').value;
alert(document.getElementById('abc_'+xyz));
document.getElementById('abc_'+obj).style.border="3px solid red";
var status = document.getElementById('imgstat_'+obj).value;
document.getElementById('newimg').value = status;
}
I have changes the id="abc_"
because in the javascript function the parameter passed by you is the counter so the same shoul be the id value.

Related

How do i generate links that will display these products depending on the ID from the database to another page (product page) in php

<?php
foreach($products as $i => $product): ?>
<article class="products__card">
<a href="./product.php?id">
<img src="<?php echo $product['image'] ?>" alt=""
class="product__image">
<h3 class="products__title"><?php echo
$product['Device_name'] ?></h3>
<span class="products__price"><?php echo
number_format($product['price']) ?> UGX</span>
</a>
<button class="products__button">
<i class="uil uil-shopping-bag"></i>
</button>
<input type="hidden" name="id" value="<?php echo $product['id']
?
>">
</article>
<?php endforeach; ?>
The sql is okay the column for id exists but I am not sure how to generate pages depending on the id of the clicked item.
The jumping back and forth between PHP mode and HTML mode is inappropriate.
You mention SQL but there is no SQL shown.
Instead of breaking out of PHP into HTML and back just make the strings in PHP and use concatenation to make the string and echo it.
echo `<a href="./product.php"><img class="product__image" src="' . $product['image'] . '" alt="" />';
foreach works with arrays as key-value pairs. Where i is your key and $products. If you do not need the key (i) then you do not need to use a key variable.
You should not use associative arrays here. It is very difficult to embed them in PHP strings. I can show you how to do it correctly but for now I will simplify the variables. And we can eliminate concatenation.
$image = $product['image'];
$price = number_format($product['price']);
$device = $product['Device_name'];
$id = $product['id'];
This is not valid, it will show nothing in the Browser
<i class="uil uil-shopping-bag"></i>
The "i" tag was used to italicize text:
<i>The will render in italic print</i>
The <i> tag was replaced by <span>
The img tag ends with />. needs the slash as does <input ... />
foreach($products as $product){
echo <<<EOT
<article class="products__card">
<a href="./product.php?id">
<img class="product__image" src="$image" alt="" />
<h3 class="products__title">$device</h3>
<span class="products__price">$price UGX</span>
</a>
<button class="products__button"></button>
<input type="hidden" name="id" value="$id" />
</article>
EOT;
}
Now the link to the product page:
I saw you used and anchor a href="./product.php">
And you used <input type="hidden" name="id" value="$id" />
You should use a <form> and with a POST rather than an anchor GET
<form action="./product.php? method="post"><button><img src="icon.jpg" width="20" height="20" /></button><input ="hidden" name="id" value="$id"</form>
NOTE: Always specify a width and height in a <img>. If you do not, it gives the Browser a lot of unnecessary work to do.
Now the associative array. I assume you got that from mySQL.
This is how I get the column contents.
I use the fetch_array and specify only the numeric array.
I get the numeric array because the list() function will assign the value of a numeric array to simple variable.
$sql = "SELECT `id`,`Device_name`,`price`,`image` FROM `Products` WHERE 1";
$results = mysqli_query($conn,$sql);
while(list($id,$device,$price,$image) = mysqli_fetch_array($results, MYSQLI_NUM)){
echo <<<EOT
<article class="products__card">
<img class="product__image" src="$image" alt="" />
<h3 class="products__title">$device</h3>
<span class="products__price">$price UGX</span>
<form action="./product.php? method="post"><button><img src="icon.jpg" width="20" height="20" /></button><input ="hidden" name="id" value="$id"</form>
</article>
EOT;
}
If I understand you, you need to have a link to the selected product, if that is what you asked, you can create it like this:
product.php?id=<?php echo $product['id']; ?>
With this you will get an example if your product has 412 ID in the database, the link will be https://example.com/product.php?id=412.
And in product.php page you need to do something like this:
$link = null;
if(!empty($_GET['id'])) {
$link = $_GET['id'];
}
if (null === $link) {
header('Location: /index.html');
} else {
// code
...
The first line of code prevents users to manually change id number of your product, in that case, they will be automatically redirected to the index page or else the display page. Hope it helps.

Show image inside JQuery Mobile PopUp

I am using JQuery Mobile PopUp control inside a dynamic table to let user view image in PopUp. Here is my code:
<head>
<link rel="stylesheet" href="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
<script src="https://d10ajoocuyu32n.cloudfront.net/jquery-1.9.1.min.js"></script>
<script src="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>
while($FormDataRow = mysql_fetch_array($formDataSQLObj))
{
$Imagepath = "Uploaded_Fotos/";
echo "<tr>";
echo "<td>";
?>
<a href="#popupBasic" data-rel="popup">
<img src="<?php echo $Imagepath.$FormDataRow[16]; ?>" width="100px"/>
</a>
<div data-role="popup" id="popupBasic">
<img src="<?php echo $Imagepath.$FormDataRow[16]; ?>" />
</div>
<?php
echo "</td>";
echo "</tr>";
}
The image appear fine inside table but popup shows empty when we click on image.
What's missing?
If you are using same id for multiple images this may be a issue because id is supposed to be a unique so in your code i notice the id is same for each image will not gonna work try this
$index=1;
while($FormDataRow = mysql_fetch_array($formDataSQLObj))
{
$Imagepath = "Uploaded_Fotos/";
echo "<tr>";
echo "<td>";
?>
<a href="#popupBasic_<?php echo index;?>" data-rel="popup">
<img src="<?php echo $Imagepath.$FormDataRow[16]; ?>" width="100px"/>
</a>
<div data-role="popup" id="popupBasic_<?php echo index;?>">
<img src="<?php echo $Imagepath.$FormDataRow[16]; ?>" />
</div>
<?php
echo "</td>";
echo "</tr>";
$index++;
}

Database link not working

I have a problem with this page
the green go logo next to the image is meant to visit the promotions site saved on my database. But it loads the same page in a new tab.
My code for this is:
<img alt="" title="" src="GO.png" height="50" width="50" align="right" />
Any ideas why it does not work?
If you need any code for any of my pages then please let me know and I will edit this and add it.
Thanks.
HOW DO I HREF MY GO IMAGE TO A LINK SAVED UNDER promo_link IN MY DATABASE TABLE?
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
**if(isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);**
$articles = $article->fetch_all();
?>
<html>
<head>
<title>xclo mobi</title>
<link rel="stylesheet" href="other.css" />
</head>
<body>
<?php include_once('header.php'); ?>
<div class="container">
Category = ???
<?php foreach ($articles as $article) {
if ($article['promo_cat'] === $_GET['id']) { ?>
<div class="border">
<a href="single.php?id=<?php echo $article['promo_title']; ?>" style="text-decoration: none">
<img src="<?php echo $article['promo_image']; ?>" border="0" class="img" align="left"><br />
**<img alt="" title="" src="GO.png" height="50" width="50" align="right" />**
<br /><br /><br /><br />
<font class="title"><em><center><?php echo $article['promo_title']; ?></center></em></font>
<br /><br />
<font class="content"><em><center><?php echo $article['promo_content']; ?></center></em></font>
</div><br/><br />
</a>
<?php } } } ?>
</div>
<?php include_once('footer.php'); ?>
</body>
</html>
Your href attribute is empty and because you're using target="_blank" clicking on the link will open the same page in a different tab.
The $data['promo_link'] is empty. I can't tell why because there is no code.
Edit
It seems you forgot the brackets
replace $article = new article; with $article = new article();
also $article->fetch_data($id) is probably returning an empty value.
As I can see, your id value is "FREE". make sure that article->fetch_data("FREE") returns what you expecting it to return.
I also suggest changing you code style.
This is much more easy to read:
if ($param){
echo '<div>' . $data["bla"] . '</div>';
}
mixing your PHP code and yout HTML code makes it hard to read and understand.
Note: <font class="title"><em><center><?php echo $article['promo_title']; ?></center></em></font> is deprecated. Use CSS:
HTML/PHP code:
echo '<div class="title">' . $article['promo_title'] . '</div>';
And the CSS:
.title{
text-align: center;
font-size: 1.5em;
}

dynamic creation of html elements using php and doing pagination using javascript

I am creating a element dynamically in html and want to use it in a javascript function by referring to its ID. But as its ID is dynamically generated using PHP , so how do i pass it to the javascript function into getElementById?
Actually , i am trying to do pagination using javascript. I have displayed Page No.s, and put HREF on them, but onclick function am stuck up? Please help. My code is as below:
<?php
echo 'Page';
while($d>0)
{
?>
<input type="hidden" value="<?php echo $z; ?>" name="pagination" id="<?php echo 'pagination' .$z ; ">
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked(); submit();" >
<?php echo $z; ?>
</a>
</label>
<?php
$z++;
}
?>
in your javascript functio you can use the same PHP expression to get the id
var id="<?php echo '\"' .$z '\"'; >";
or you can have it like this,
<script type="text/javascript" language="javascript">
<!--
<?php
echo("id= $z;");
?>
// -->
</script>
to get the id
<label id="<?php echo 'labelofpagination'.$z; ?>" >
<a href="#" onclick="PaginationLabelClicked('<?php echo "\"" ."labelofpagination".$z ."\""; ?>'); submit();" >
<?php echo $z; ?>
</a>
</label>
and javascript
function PaginationLabelClicked(id){
// you can get the id here...
}

correct place for my form tag to pass correct value in action PROBLEM

i have this code:
<body>
<div class="header">
<?php if (isset($_SESSION['user_id']) && !isset($menu)) { ?>
<div class="menu_holder">
<ul>
<li>
<a href="<?php echo ADDRESS; ?>menu.php" class="green_link">
<img src="<?php echo IMAGES; ?>template/menu.gif" width="51" height="20" border="0" />
</a>
</li>
</ul>
</div>
<?php } ?>
<?php
if (!isset($plainHeader))
{
$plainHeader = " ";
?>
<img src="<?php echo IMAGES; ?>template/logo.gif" width="160" height="94" />
<?php
}
?>
</div>
<br/>
<?php $id_to = $user['profile_id_contact']; ?>
<div class="main_content">
<center>
<div class="innerContainer">
<span class="headings2">FREE CHAT CONTACTS</span>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<?php
if (count($users) > 0)
{
foreach ($users as $user)
{
//some php here
?>
<a href="#" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
<?php
}
}
?>
</form>
</div>
</center>
</div>
</body>
as my code is now the form tag is in the wrong place because my tag links does not show.
where must i put my form tag so that when i click on any of the uniquecode links i pass the correct $id_to in the action??? when i move the form tag after the my links show but regardless of which link i click on it passes the first link's $id_to with the action. i have also tried to pass $id_to as a hidden field which i had after the sumbit but still it passes the first link's id
please help? i have been struggeling with this for some time now...i cannot redirect the page via JS becuase this site is for a MOBILE aka mobi site
please help? im desperate
thank you
if i move the form tag and have it like this:
messageSent.php?id=">
and i view the page source $id_to contains correct id but as sson as i go to sentMessage.php the id in the url is incorrect
To pass a id to the next page in a link you need to add "?id='.$id.'" at the end of the url.
e.g.
<a href="<?php echo ADDRESS; ?>menu.php?id=5" class="charcoal_link" style="line-height: 20px;">
To make sure that each link is different you can right click and copy the url to double check.

Categories