I'm having a problem submitting only one of the forms at my site.
The forms are being created dynamic through a foreach loop.
Every time i'm submitting one form, it submits all of them, so if there is 10 posts then they have a reply form each, and all it inserted to the database.
<div id="newsFeed">
<div class='newsBox'>
<div class="newsProfileInfo">
<div class="newsProfileImg">
<img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $name; ?>">
</div><!--newsProfileInfo ends here-->
<div class="newsName">
<p>
<?php echo $res->name." ".$res->surname;?>
</p>
</div><!--newsName-->
</div><!--newsProfileInfo ends here-->
<div class="newPost">
<form action='' method='post'>
<textarea name="newPost" placeholder="Opret nyt indlæg"></textarea>
<input type="submit" name="postSubmit" value="Opret">
</form>
</div>
<?php
$data = newsFeed($db);
foreach($data as $news){
$newsId = $news['newsId'];
$headline = $news['headline'];
$post = $news['post'];
$date = $news['postDate'];
$dates = date_create($date);
$name = $news['name'];
$surname = $news['surname'];
$profileImg = $news['profileImage'];
$path = "images/profileImages/";
?>
<div class="newsProfileInfo">
<div class="newsProfileImg">
<img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name; ?>">
</div><!--newsProfileInfo ends here-->
<div class="newsName">
<p><?php echo substr($name ." ". $surname, 0,11);?></p>
</div><!--newsName-->
<p class="postDate">
<?php echo date_format($dates, 'd/m/Y') ." - ".date_format($dates,'H:i');?>
</p>
</div><!--newsProfileInfo ends here-->
<div class="newsPost">
<p>
<?php echo $post;?>
</p>
</div>
<?php
$reply = replyToPost($db,$newsId);
foreach($reply as $msg){
$replyId = $msg['replyId'];
$message = $msg['message'];
$news_fk = $msg['newsId_fk'];
$userId_fk = $msg['userId_fk'];
$date = $msg['date'];
$name = $msg['name'];
$surname = $msg['surname'];
$profileImg = $msg['profileImage'];
$path = "images/profileImages/";
?>
<div class="replysLoop">
<div class="replyImg">
<img src="<?php echo $path, $profileImg;?>" alt="<?php echo $name;?>">
</div>
<div class="replysBoxes">
<p><?php echo $message;?></p>
</div><!--replysBoxes ends here-->
</div><!--replysLoop ends here-->
<?php
}
?>
<div class="replyToMsg">
<div class="replyImg">
<img src="<?php echo $path, $res->profileImage;?>" alt="<?php echo $res->name; ?>">
</div>
<?php
if(isset($_POST['replySubmit'])){
insertReply($db,$newsId);
header('Location: index.php');
}
?>
<div class="inputHolder">
<form action="" method="post">
<input type="text" name="replys" placeholder="Besvar indlæg">
<input type="submit" name="replySubmit" value="Besvar">
</form>
</div>
</div>
</div><!--newsbox ends here-->
<?php
}//foreach loop newsfeed ends here
?>
As far as I am aware the submit button will only submit the form that it is in. In fact it is very hard to submit more than one form at a time.
I suspect the problem is that the form submits the reply and that it is not bound to a current comment and ends up getting added to all of them.
I suggest that you start with something like this:
<?php
//... something that results in this post setting `$postid`
?>
<div class="reply">
<form action="" method="post">
<input type="hidden" name="replyToPostID" value="<?php echo $postid; ?>">
<input type="text" name="replyToPost" placeholder="Besvar indlæg">
<input type="submit">
</form>
</div>
Then your PHP can make SQL that pins the comment down to one parent comment. Of course you are forcing me to guess because there is no way what you have posted is the entire code.
See also: Submit Multiple Forms With One Button
Related
i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art
thats the code for making an article window
<?php $articles_qr = mysqli_query($connection, "SELECT * FROM `articles` ");
$articles = array();
while ( $art = mysqli_fetch_assoc($articles_qr))
{
$articles[] = $art;
}
?>
<?php foreach ($articles as $art)
{
?>
<section>
<div class="containerstuff">
<div class="stuffpic">
<img src="<?php echo "../static/imagespages/",$art['image'] ?>" class="pico">
</div>
<div class="article">
<h1><?php
echo $art['title']
?>
</h1>
<?php
echo $art['text'];
echo $art['id']
?>
</div>
</div>
<div class="scrollmenu">
<?php include "../includes/comments.php";?>
</section>
<?php
} ?>
thats the code comments window
<?php
date_default_timezone_set(timezone_identifier);
include_once '../comments.ink.php'
?>
<div class="containercom">
<img src="#" class="commpic">
<p class="comment"></p>
</div>
<div class="blockcom">
<form class='form' method='POST' action="<?php echo setComments($connection)?>">
<div class='form__group'>
<input type='hidden' name='page_id' value="<?php echo $art['id']?>" >
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='pubdate' value="<?php echo date('Y-m-d H:i:s')?>" >
<textarea name='text' class='form__control' placeholder ='Введите комментарий...' required=''></textarea>
</div>
<div class='form__group'>
<input type='submit' class='form__control2' name='commentSubmit'></input>
</div>
</div>
</form>
and thats the code for INSERT function
<?php
static $firstTime = true;
function setComments($connection) {
if(isset($_POST['commentSubmit'])){
$idcom = $_POST['page_id'];
$uid = $_POST['uid'];
$pubdate = $_POST['pubdate'];
$text = $_POST['text'];
$sql =
"INSERT INTO `comments` (page_id, uid, pubdate, text)
VALUES ('$idcom', '$uid', '$pubdate', '$text')";
$result = $connection->query($sql);
$firstTime = false;
}
}
so how can i make insert only for 1 article (so when i add it now, there are appears as many comments as many articles i have in database)
I think you should use ajax to append a new comment which is the widely used solution, the way u r doing will become difficult to handle for you.
I haven't found the solution, so I just selected another way.
For each article I placed a button to post a comment which will send user to the page with this article and for this button for "href" I wrote php code (href = "comments.php?id=<?php echo $art['id']"?>) and for this page I use $_GET to select articles only for this id. Then I just placed there comments-function that I wrote so it works alright now because function works only for 1 argument
I'm currently coding a blog to get experience with php.
In the edit.php I want to give the category of a post if a post has one.
This is the edit.php:
<div class="categories-heading">
<?php if (!empty($entry->c_Id)): ?>
<div class="category-heading">
<h3>Category: <?php echo e($categoryFromId); ?></h3>
</div>
<div class="category-heading">
<h3>change category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
</div>
</form>
<?php else: ?>
<div class="category-heading">
<h3>choose category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
This is the part of the function edit() in the PostsAdminController.php which is relevant for this:
if(!empty($_POST['title']) AND !empty($_POST['subheading']) AND
!empty($_POST['content'])
AND !empty($_POST['category'])) {
$entry->title = $_POST['title'];
$entry->subheading = $_POST['subheading'];
$entry->content = $_POST['content'];
$entry->c_Id = $_POST['category'];
$this->postsRepository->update($entry);
$savedSuccess = true;
}
$categoryFId = $this->categoryRepository->oneCategory($entry->c_Id);
$categoryFromId = $categoryFId['category'];
var_dump($categoryFromId);
$this->render("post/admin/edit", [
'entry' => $entry,
'savedSuccess' => $savedSuccess,
'categories' => $categories,
'categoryFromId' => $categoryFromId
]);
}
And this is the function oneCategory in the CategoryRepository.php that interacts with the database.
public function oneCategory($id)
{
$table = $this->getTableName();
$model = $this->getModelName();
$stmt = $this->pdo->prepare("SELECT `category` FROM `$table`
WHERE id = :id");
$stmt->setFetchMode(PDO::FETCH_CLASS, $model);
$oneCategory = $stmt->fetch(PDO::FETCH_CLASS);
return $oneCategory;
}
oneCategory() gives out an array no matter if I put SELECT * or SELECT category in the query, that's why I put $categoryFId['category'] into a variable after.
It works, but I think there must be a more easy or quicker way (like special functions or so), I just didn't manage to find what I search for
Hello monsters of programming. I just want to ask a question about using $_SESSION and $_GET. When to use $_GET and $_SESSION? what is the best for passing variable? Im just new to php and html and i don't know what is the best practice. Can someone help me to understand both of them?
Here is the example of my code. I used $_SESSION for passing the variable $newsid;
here is the edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type='hidden' name="news_id" value="<?php echo $newsid;?>">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
header('Location: edit2.php');
}
?>
here is the edit2.php
<?php
session_start();
$id = $_SESSION['news_id'];
include_once('connection.php');
$sql = "SELECT * FROM news WHERE news_id = '$id'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $title;?>"/><br>
Date<input type ="text" name="date" value="<?php echo $date;?>" /><br>
Content<textarea name="content"><?php echo $content;?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $newsimage;?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
$_GET is for parameters that are needed during that specific request (or can be easily carried over to other pages), e.g.:
item IDs
current page (pagination)
user's profile name
...
$_SESSION is for data that needs to be persisted across multiple requests, e.g.:
current user's ID
shopping carts
list filters
...
You should use the one that better suits your use case.
That being said, I'd consider storing news_id in the session a bad thing. What if I want to edit multiple items and open multiple browser tabs? I'll end up overwriting my data. Just because you can use sessions doesn't mean you should.
this is what i want to do, i have two pages, first is the "edit.php" and second is the "edit2.php". the page "edit.php" this is the page where all the news is shown and you will select what news you will edit by clicking the "edit button" and the "edit2.php" where will i exactly edit the news. i want to pass the value of the selected news in another page. But here is the problem, when i clicked the "Sample news 1" edit button the data showing in another page is the "Sample news 2". Even when i clicked the "Sample news 2" edit button, the data is also the "Sample news 2". Can someone give me ideas on how to fix this?
here is the picture of edit.php. I click the edit button of "Sample news 1".
here is the picture of edit2.php. and this is the output data. The data should be "Sample news 1" not "Samplel news 2".
here is my php code in edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $newsid;
$_SESSION['n_title'] = $title;
$_SESSION['n_date'] = $date;
$_SESSION['n_content'] = $content;
$_SESSION['n_image'] = $newsimage;
header('Location: edit2.php');
}
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
here is my php code for "edit2.php"
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $_SESSION['n_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $_SESSION['n_date']; ?>" /><br>
Content<textarea name="content"><?php echo $_SESSION['n_content']; ?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $_SESSION['n_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Remove if(isset($_POST['esubmit'])){ code out of while loop and add values in form as I shown in below example
Pass $newsid in edit form as hidden and retrieve the content based on new
<form action="" method="post">
<input type='hidden' value="<?php echo $newsid;?>" name="news_id">
<input type="submit" name="esubmit" value="edit" />
</form>
and in your php add this line.
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
The problem is, in each iteration of while loop you're overwriting $newsid, $title, $date,... variables. So when you submit the form, the last row's data will get stored in the corresponding $_SESSION variables.
So here's the solution to your problem.
You don't need $_SESSION to pass the form values to edit2.php page, instead change the <form> element in the following way,
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
On edit2.php page, first catch the news_id value using $_GET superglobal , like this:
$newsid = $_GET['news_id'];
And then get the appropriate news details using this $newsid, and finally populate the form.
Here's the complete code,
edit.php
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
edit2.php
<?php
if(isset($_POST['esubmit'])){
$newsid = $_GET['news_id'];
include_once('connection.php');
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}
}
if(isset($_POST['submit'])){
// Write code to commit the edit details
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['esubmit'])){
?>
<form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="content"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="submit" value="Update" />
</form>
<?php
}
?>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Here is the issue, You are looping your data to the session, usually single session keeps only a value. Since that final item of your loop will store on the session [in this case it is 'Sample news 2']. I believe, you can place it on a hidden field & post it to the next page or you can use URL Parameter [GET], to pass the Id to the next page.
Ex : <a href ='edit2.php?newsId ='<?php echo $newsid?>> Edit </a>
what I think is happening is when you are clicking edit button this edit.php page is again reloading and that sql query is running again that's why it is taking value of first image in both cases.what I suggest you to try is use $_get[] instead of $_SESSION and $_POST and pass value of image id directly to page edit2.php and query others details from database in that page and then display it.try it
Remove <form> and <input> use just <a> instead like this.
<a href='edit2.php?id=$news_id'>Edit</a>
and then fetch this id in edit2.php like following
$news_id=$_GET['id'];
and atlast fetch other details of news from the database using this id like'
$query='select * from news where news_id=$news_id';
I decided to leave in all the code to make it less confusing to those who see this.
On line #57, the only form on this page, I'm trying to $_POST the id that equals the post_iD.
Everything uploads correctly to MySQL besides the post_iD.
I always get a Notice: Undefined index: post_iD on $post_iD = $_POST['post_iD']; inside the if(isset($POST['comment'])).
I'm sure the issue is with how I'm trying to retrieve the post_iD inside the form, and not any issues with the PDO but html as PDO the data is being inserted correctly besides the post_iD which I have mentioned.
I'm using post_iD to loop posts from database, it works besides inside the form, any enlightenment with this issue?
Code Below.
if(isset($_POST['comment'])){
$comment = $_POST['comment'];
$post_iD = $_POST['post_iD'];
$data = $Wall->Insert_Comment( $uiD, $post_iD, $comment, $_SERVER['REMOTE_ADDR'] );
}
if ( $updatesarray ){
foreach ($updatesarray as $data){
$post_iD = $data['post_iD'];
$orimessage = $data['message'];
$message = tolink(htmlcode($data['message']));
$time = $data['created'];
$mtime = date("g:i", $time);
$username = $data['username'];
$uploads = $data['uploads'];
$uiD = $data['uid_fk'];
?>
<div class="wrap">
<div class="item" id="stbody<?php echo $post_iD;?>">
<div class="loop-post">
<div class="loop-post-content">
<div class="loop-post-image">
<a href="" class="post-link">
<?php
if ($uploads){
$s = explode(",", $uploads);
foreach ($s as $a){
$newdata = $Wall->Get_Upload_Image_Id($a);
if ($newdata) echo "<a href='uploads/" . $newdata['image_path'] . "' rel='facebox'>
<img src='uploads/" . $newdata['image_path'] . "' width='520' height='245' class='imgpreview attachment-top_story_post wp-post-image' /></a>";
}
echo "</div>";
}
?>
</a>
</div>
<div class="loop-post-byline">By <a rel="author" title="Posts by Emil Protalinski" href=""><?php echo $username;?></a>
<span class="date"> — <?php echo $mtime;?></span>
</div>
<a class="post-link">
<?php echo clear($message);?>
</a>
<div class="post_comment">
<?php $x=1; include_once 'load_comments.php'; ?>
<div class="commentupdate" id="commentbox<?php echo $post_iD;?>">
<div class="stcommentimg">
<img src="<?php echo $photo;?>" class="small_face">
</div>
<div class="stcommenttext">
<form method="POST" action="">
<textarea name="comment" class="comment" id="<?php echo $post_iD;?>" value="<?php echo $post_iD;?>"></textarea> #57
<input type="submit" value="comment">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php } } else echo '<h3 id="noupdates">No Updates!</h3>';?>
You are never actually setting the post_iD variable anywhere. If you want to use it in the $_POST array you need to set it in the form first.
<form method="POST" action="">
<input type="hidden" name="post_iD" value="<?php echo $post_iD; ?>" />
<textarea name="comment" class="comment" id="<?php echo $post_iD;?>" value="<?php echo $post_iD;?>"></textarea> #57
<input type="submit" value="comment">
</form>
You need to do this...
<textarea name="comment" class="comment" id="comment-<?php echo $post_iD;?>"></textarea>
<input type="hidden" id="post_iD" name="post_iD" value="<?php echo $post_iD;?>" />
Cuz youre never passing the post_iD anywhere....this will pass it in hidden form...
And the value for your textarea wont be $post_iD, it will likely be a comment/post of some type....I assume you just had that for debugging