PHP separate id for every div - php

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 ?>

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.

like system check if user like/unlike button

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; ?>

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.

Form submitting in php

--EDIT---
i have created a text box area where users can input some data,
Basically when i press the submit button, it should save the inputted data. here is the full code, i can't fix it. :/
The user entry does not get updated.
<?php
if ( $act == "htmlprofile" ) {
?>
<div class="contentcontainer">
<div class="contentmboardheaderarea"><img src="images/header.png" />
<div style=”word-wrap: break-word”><div class="contentheadertext"><?php echo "$hlang2"; ?></div></div></div>
<div class="contentheading">
<form id="htmlform" name="htmlform" method="post" action="options.php?act=htmlsubmit">
<div class="contentheading4">
<?php echo "$olang15"; ?></div>
</div>
<div class="contentmboardlistarea2"><textarea id="htmlprofile" name="htmlprofile" cols="33" rows="10"><?php echo $qry2[htmlprofile];?>
</textarea></div></form>
<div class="contentbuttonarea">
<div class="contentbutton1" onClick="document.location.href = 'profile.php?act=index'";><?php echo "$glang3"; ?></div>
<div class="contentbutton2" onClick="document.forms['htmlform'].submit();"><?php echo "$glang21"; ?></div>
<div class="contentbutton3"></div>
<div class="contentbutton4"></div>
<div class="contentbutton5" onClick="document.location.href = 'help.php#htmlprofile'";><?php echo "$glang5"; ?></div>
</div>
</div>
<?php
}
?>
<?php
if ( $act == "htmlsubmit" ) {
$save ='Profile updated successfully';
$query4 = "UPDATE members SET htmlprofile = '$htmlprofile' WHERE username = '$myuser'";
mysql_query($query4);
?>
There is no value attribute in textarea you have to enter the content between the <textarea>text</textarea>
See for reference tag_textarea
It should be like this
<textarea id="htmlprofile" name="htmlprofile" cols="33" rows="16" >
<?php echo $qry2[htmlprofile]; ?>
</textarea>
Refer to this answer also which-value-will-be-sent-by-textarea-and-why
Your div contentheadertext is not contained within the <form></form> so if you have inputs there, they won't be included in the form submission.
To resolve, move the opening <form> tag higher, so that it encloses all inputs.
the submit works fine. But you have no variables with the post data definied. And textareas haven't "value". And when you don't need fixed strings in your echo, don't use the "
try the following:
<textarea id="htmlprofile" name="htmlprofile" cols="33" rows="16"><?php echo htmlspecialchars($_POST['htmlprofile']); ?></textarea>
if you are trying to use a php variable inside HTML do that simply by echo $myvariable ; Quotes not required there. But if u are trying to echo a string use quotes just like echo 'mystring' .
Moreover when you are trying to pass some values as part of form submission, the required values should be between the tags
if you want to show any value inside text area, remember it doesn't have a 'value' attribute. So write the code like this: value to be displayed

PHP Passing value

Im having trouble passing the value id in my code to edit.php.
In displaynews i print out articles from the database. It also creates a link that redirects me to edit.php and sending the $id value with it.
Link to displaynews function
http://snipt.org/zhla8
Here's where im having trouble
<h3>EDIT NEWS ARTICLE</h3>
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<textarea name="editnewstext"><?php $news=Textarea(1);echo $news ?></textarea> <!--HERE i need to replace the 1 with id passing in displaynews -->
<input type="submit" name="Edit_News" id="Edit_News">
<?php
include 'db.php';
include'editnewsarticle.php';
if(isset($_POST['Edit_News']))
{
$content= $_POST['editnewstext'];
geteditnews(1,$content); //<!--HERE i need to replace the 1 with idpassing in displaynews -->
Header("location:Home.php");
}
Link to Edit.php page
http://snipt.org/zhkj8
Link to GetnewsTextarea function
http://snipt.org/zhlb9
Link To editnewsarticle function
http://snipt.org/zhki2
Please dont comment on how the mysql extension is depreciated and that my code is open for sql injections.
Thanks in advance
EDIT: Here's the solution
if(isset($_GET['id']))
{
$id = $_GET['id'];
$data = mysql_query("SELECT * FROM news WHERE id = '$id' ");
}
?>
Does these changes in edit.php help?
if (isset($_POST['id']))
$id = $_POST['id'];
<?php $news=Textarea($id);echo $news ?>
geteditnews($id, $content);
Add a hidden field for the id right after the form tag. As such:
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">

Categories