like system check if user like/unlike button - php

so here is my code for like system
<div class="btn like">
<div class="boxcoracao">
<?php foreach ($db->checklike($postid,$session_id) as $chk) {
if($postid== $chk['pl_puid'] && $session_id == $chk['pl_uid']){
?>
<input type="hidden" name="likepid" id="likepid" value="<?php echo $postid ?>">
<input type="hidden" name="likemid" id="likemid" value="<?php echo $mem_id ?>">
<span class="coracao ativo" name="like"><br>   Love</span>
<?php
}else{
?>
<input type="hidden" name="likepid" id="likepid" value="<?php echo $postid ?>">
<input type="hidden" name="likemid" id="likemid" value="<?php echo $mem_id ?>">
<span class="coracao" name="like"><br>   Love</span>
<?php
}
}
?>
</div>
</div>   
what happen here is if sessionID exists on the DB the if statement will run if not else will... but what happens is after writing this code my button disappear
sql query
function checklike($pid,$mid){
$query = "SELECT * FROM plike WHERE pl_puid = '$pid' AND plc_uid = '$mid'";
$stmt = $this->dbh->prepare($query);
$stmt->execute(array("0"));
$active_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $active_data;
}
also im confuse im in going to use forloop for this??.. any better idea on how to do it...
this button refers to the default icon if the user didn't hit like
this button refers to the icon where the user hit like
but what happens on the above code is it disappears the button since the button is inside the if statement i dont have any idea on how to do it.. any idea please..

You are using if else statements inside html wrongly.
See this:
link
Example:
<? if ($condition): ?>
<p>Content</p>
<? elseif ($other_condition): ?>
<p>Other Content</p>
<? else: ?>
<p>Default Content</p>
<? endif; ?>

Related

Populate input values with PHP variables with related ID

I am trying to create a form when the user clicks a button on a selected element. However, when i try it, i click a button and it either loads multiple, or loads 1 but shows only one value, if that makes sense. Here is my code:
<?php
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<div class="service_display">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
</div>
<?php
}
?>
I have a form that will appear with jQuery when the button is pressed, and im trying to fill the values with the respective id's. Here is my code for that:
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
I think the problem is me not knowing where to put that code, if i put it in the while loop, it works, but understandably, i have like 10 input fields pop up..
if i put it outside the while loop and run a separate query to fill it, its empty..
As per you explanation of the problem its clear that you are looping the forms along with the front end display divs, which is not that good idea.
Always its better to use one modal window and then depending upon the click you have to load the data onto it and then pop it up.
Anyway for your scenario there may be more than one solution but this one will also serve the purpose:
Put the "show-onclick" whole div inside the "service_display" div so that now also it gets looped with no issues.
Now when the button gets clicked find the closest "service_display" div, it will give you the dom element & then use that and popup the "show-onclick" div enclosed within that.
Follow the code below:
<?php
$database = new mysqli("localhost", "root", "", "so");
$sql = "SELECT * FROM services";
$result = $database->query($sql);
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
?>
<div class="service_display" id="sd_<?php echo $row['id']; ?>">
<div class="service_header"><?php echo $row['service_name']; ?></div>
<div class="service_desc"><?php echo $row['service_desc']; ?></div>
<div class="service_options">
<button type="button" class="additional_files" id="additional_files">Edit</button>
</div>
<div class="show-onclick">
<h3>Edditing service: <?php echo $row['service_name']; ?></h3>
<hr>
<form action="inc/save_edit.php?id=<?php echo $row['id']; ?>" class="service_editform">
<input type="text" name="service_name" value="<?php echo $row['service_name']; ?>">
<textarea><?php echo $row['service_desc']; ?></textarea>
</form>
</div>
</div>
<?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".show-onclick").css("display", "none");
$(document).on("click", ".additional_files", function() {
$(".show-onclick").css("display", "none");
var sd_id=$(this).closest('div.service_display').attr("id");
$("#"+sd_id+" .show-onclick").css("display", "block");
});
});
</script>
The only change i made to your code is i have added a dynamic id field to service_display div.
And i have used jquery to show and hide the forms of respective clicked buttons.
Again here i have used oop based mysqli which is not mandatory yours is also perfectly fine.

PHP delete current 'note' not working

I'm generating a delete button for each 'note' the user creates. However, no matter which delete you click, its deleting the most recently saved note, not the one corresponding to the note. I assume that something is wrong with my hidden field 'deleteID'.
<!-- connections.php connects to the database -->
<?php require 'connections.php'; ?>
<!-- check to make sure the user is logged in,
if not then redirect them to login.php -->
<?php session_start();
if(isset($_SESSION["UserID"])){
} else {
header('Location: Login.php');
die();
}?>
<!-- $result is a query containing all the notes
of the current user -->
<?php $UserID = $_SESSION["UserID"];
$result = mysqli_query($con, "SELECT * FROM notes WHERE UserID = '$UserID'");
?>
<!-- when you click 'save' upload the new note
to the database and refresh the page.
when you click 'delete' remote the note
that goes with that button from the database -->
<?php if(isset($_POST['save'])) {
session_start();
$note = $_POST['note'];
$UserID = ($_SESSION["UserID"]);
$sql = $con->query("INSERT INTO notes (UserID, note)Values('{$UserID}','{$note}')");
header('Location: Account.php');
} else if (isset($_POST['delete'])){
$deleteID = $_POST['deleteID'];
$sql = $con->query("DELETE FROM notes WHERE noteID = '$deleteID'");
header('Location: Account.php');
} else if (isset($_POST['edit'])){
}?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>My Account</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<h1 class="titleAct">Welcome</h1>
<form action="" method="post" name="notesubmit" id="notesubmit">
<div>
<textarea name="note" cols="50" rows="4" form="notesubmit" id="noteinput">New Note
</textarea>
</div>
<input name="save" type="submit" class="button" id="save" value="Save">
<!-- Whenever a note is saved, print out the
note with timestamp followed by the edit
and delete buttons for each note -->
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php $note = $row['note'];?>
<?php $date = $row['time'];?>
<?php $noteID = $row['noteID'];?>
<div id="note">
<p class="note"><?php echo $date; ?></br> ---------------- </br><?php echo $note; ?> </p>
</div>
<input name="deleteID" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
<input name="delete" type="submit" class="button" value="Delete">
<?php endwhile; ?>
</form>
<div>
<a class="link" href="Logout.php">Logout</a>
<div>
<a class="link" href="Deactivate.php">Deactivate My Account</a>
</div>
</div>
</body>
</html>
I am thinking its not the delete at all - but the div with the note in it - each has the same id since they are in the while loop and therefore every note div has the id of "note"
<div id="note">
its should be
<div id="note<?php echo $noteID;?>";
The error seems to be in the name of the input. Should be the same as the value you set in the ID part.
Like this :
<input name="hidden<?php echo $noteID;?>" type="hidden" id="hidden<?php echo $noteID;?>" value="<?php echo $noteID; ?>">
Otherwise, all your inputs have the same name, and it takes the last one to do the query.
EDIT
Put the name of the value you want to delete directly in the submit button like this :
<input name="delete-<?php echo $noteID;?>" type="submit" class="button" value="Delete">
Then you can get the value in $_POST array and explode() the value on - separator to get the ID value.
you also have multiple submit inputs for the one form - one to trigger the save and one each for each echo-ed delete note, so triggering the delete submit function is going to first trigger the save submit - therefore $_POST['save'] will be acted on before the $_POST['delete']. This could be the issue behind the action of altering the last input note rather than the one that was the trigger action.
I would suggest splitting off the add note function to a different form than the delete note functions. You can have the same if else logic and this should work as expected in the new version since you are actually posting the information you are expecting to.

PHP separate id for every div

to be honest this is more of a how to then help with code i already have. So i hope this is okay, else of course i will delete my question again. Anyway here goes i have a site with boxes, with a picture headline and a submit button. All the info in these boxes is being delivered, from my database. And of course in my database i also have a id cell, and if i try to echo out the id cell with the rest of the info in the box it shows up fine. But when i try to assign the id output variable to a header location, i do for some weird reason always get the id 3. Eventhough the id´s shows up perfectly fine, in the boxes. I have included my php code and i am still a beginner to php so sorry for this noob question. :)
session_start();
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_array($sqlSelect))
{
$id = $feed['id'];
if(isset($_POST['readArticle']))
{
$id = $_SESSION['id'];
header("Location:"."redirect.php?".SID.$idArticle);
}
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<?php $idArticle= $feed['id'];?>
<h2><?php echo $feed['headline'];?></h2>
</div>
You are setting $idArticle at the bottom of the loop but trying to use it at the top so it will be pulling it from the previous result. Try:
while ($feed=mysqli_fetch_assoc($sqlSelect)){
$idArticle= $feed['id'];
$sid = $_SESSION['id'];
if(isset($_POST['readArticle']))
{
header("Location:"."redirect.php?".$sid.$idArticle);
}
//rest of code
}
You will have to put div inside the loop.
I also replaced the header redirect with form action attribute (you may want to replace method POST with GET instead).
ID is passed with a hidden field
<?php
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_assoc($sqlSelect))
{
$id = (int)$feed['id'];
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="redirect.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<h2><?php echo $feed['headline']; ?></h2>
debug: <pre><?php print_r($feed); ?></pre>
</div>
<?php } // end of while loop ?>

Delete DB table item with HTML button in Joomla

Can some one point me in the right direction, I'm trying do delete item from user "favorite articles" table. Module works like that: in table: id, user_Id, article_Id and article_link.
Article "save" function works no problem. Then I did "delete" button next to each entry, but i don't know how to process it. My guess is through that:
public static function delete_art(){
$db =& JFactory::getDBO();
$query = 'DELETE FROM #__cdart WHERE id = *SOMF HERE!*';
$db->setquery($query);
$db->query();
}
but I don't know how to tell which article "delete" button did user press...
Update:
Buttons made like that:\
<div class="fapa_box">
<?php foreach ($getFapList as $getFap): ?>
<div class="fapa<?php if(modCdFapaHelper::pageExists() === $getFap->fap_id) echo ' fapa_active'; ?>" id="fapa_<?php echo $getFap->fap_id; ?>">
<span class="fapa_icon ui-icon ui-icon-star"> </span><a class="fapa_tooltip" href="<?php echo str_replace('&', '&', $getFap->fap_link); ?>" title="<?php echo htmlspecialchars($getFap->fap_note); ?>"><?php echo htmlspecialchars($getFap->fap_title); ?></a>
<form name="fapa_delete" action="" method="post">
<?php echo JHTML::_('form.token'); ?>
<input type="hidden" name="fapa" value="remove" />
<button type="submit" name="submit" value="submit">delete</button>
</form>
</div>
<?php endforeach; ?>
</div>

javascript empty field validation

I have used the following code to display product details in my shopping cart website.
<div id="inner_right">
<form name="product_form" id="product_form" method="post" onsubmit="form_quantity(<?php echo $productid; ?>);">
<input type="hidden" name="hidden_<?php echo $productid; ?>" id="hidden_<?php echo $productid; ?>" />
<h1>Product Details of <?php echo $fetchproductname; ?></h1>
<div> </div>
<div id="product_left"><img src="<?php echo $path.$fetchimage; ?>" alt="" width="400" height="300" /></div>
<div id="product_right">
<div><strong>Category Name:</strong> <?php echo $categoryname; ?></div>
<p><strong>Product Number:</strong> <?php echo $fetchproductno; ?></p>
<p><strong>Price:</strong> <span class="price">$<?php echo $fetchproductprice; ?></span></p>
<p><strong>Stock:</strong> <?php echo $fetchproductstock; ?> nos</p>
<?php
$select_quantity = "SELECT * FROM `tbl_cart` WHERE `intProductid` = '".$productid."' AND `intSessionid` = '".$globalsessionid."'";
$select_quantity_res = mysql_query($select_quantity);
$sel_qty_num = mysql_num_rows($select_quantity_res);
$fetch_quantity = mysql_fetch_array($select_quantity_res);
$fetch_proid = $fetch_quantity['intProductid'];
$fetch_exqty = $fetch_quantity['intQuantity'];
?>
<p><strong>Quantity:</strong> <input name="quantity_<?php echo $productid; ?>" id="quantity_<?php echo $productid; ?>" value="<?php echo $fetch_exqty; ?>" class="quantity" type="text" /></p>
<div class="submit">
<button id="registerButton" type="submit">Add To Cart</button>
</div>
<input type="hidden" name="cart" id="cart" value="<?php echo $productid; ?>" />
</div>
<div class="clear"> </div>
</form>
</div>
There is an quantity field and add to cart button in my page. If the buyer click add to cart button without entered the quantity field an error should popup. For that i used the following javascript code.
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty =='')
{
alert(Please enter quantity);
}
}
But it doesn't work. I couldn't trace the error. How can i correct my code?
You've got a string literal with no quotes:
alert(Please enter quantity);
You need to say:
alert("Please enter quantity");
// OR
alert('Please enter quantity');
(When you say you couldn't trace your error, what did you actually try? If using Chrome it has built-in debugging tools, or for Firefox you can download Firebug, and these tools can easily tell you about errors like this.)
And to preempt your next question, once you fix the above error you'll find that although the alert displays the form still submits. You need to update your onclick to return the result of your form_quantity() function, and return false when you don't want the submit to go ahead (i.e., when there's a validation error):
<form name="product_form" id="product_form" method="post"
onsubmit="return form_quantity(<?php echo $productid; ?>);"></form>
<script>
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
if(enteredqty === '') {
alert('Please enter quantity');
return false;
}
}
</script>
function form_quantity(val){
var enteredqty = document.getElementById('quantity_'+val).value;
alert(enteredqty);// check the givel value is right.
if(enteredqty ==''){
alert("Please enter quantity");// double qute added.
}
}
Check this, you may find some path.

Categories