I am developing a simple forum for my php project. I have a viewpost.php file which displays all the posts and has a read more button.like this.
<?php
foreach($rows as $output){
echo '<div id="dd">';
$uid = $output['uid'];
$result2 = $conn->prepare("SELECT username FROM users WHERE uid = '$uid'");
$result2->execute();
$rows2 = $result2->fetchAll();
foreach($rows2 as $username_row){
echo 'POST BY '.$username_row['username'].':';}
echo '<h3>Title:'.$output['title'].'</h4>';
?>
<form action="comments.php" method="post">
<input type="submit" value="Read more" name="">
</form>
<?php
echo '</div>';
}
?>
now I want to load post and comments in comments.php. but I want post_id to retrieve post,title and comments from the database. I wonder how to pass the post_id from viewpost.php to comments.php when the user clicks on the readmore button.any help will be really appreciable am a newbee.
You can add a input to your form
<input type="hidden" value="<?php echo $output['id'] ?>" name="id" />
and in your comments.php file use $_POST['id']
or use URL like :
<a href="comments.php?id='<?php echo $output['id'] ; ?>'">
and in your comments.php file use $_GET['id']
Related
I created a form where as soon as I click on the input of the submit, it writes me a value on the column to the database table.
So far everything is fine, but when the current page reloads, I can't see the result that was written to the database.
I can only see the result if I submit the form for the second time on the same page.
My code:
<?php
$increId = $_order->getRealOrderId();
$pathAss = 'My file';
$connectionresource = Mage::getSingleton('core/resource');
$connectionWrite = $connectionresource->getConnection('core_write');
$table = 'sales_flat_order';
$query = "UPDATE ".$table." "
. "SET upload_file_1='" . $pathAss . "'"
. " WHERE increment_id='".$increId."'";
$connectionWrite->query($query);
?>
<form id="abbLogoOrder" action="" method="post" enctype="multipart/form-data">
<a href="<?php echo $_order->getupload_file_1(); ?>"
title="<?php echo basename($_order->getupload_file_1()); ?>">
<?php echo basename($_order->getupload_file_1()); ?>
</a> //THIS VALUE RETURNS EMPTY AFTER FIRST SUBMIT FORM
<b>
<?php
/* $testget = ['getupload_file_'.$count.'()'];
echo $_order->$testget; */
?>
</b>
<div class="upload-btn-wrapper">
<button class="btn" style="font-size:13px;">SELECT YOUR FILES</button>
<input type="file" name="abbFile<?php echo $count; ?>[]" id="abbFile<?php echo $count; ?>" multiple="multiple" />
<input type="submit" value="SEND" />
</div>
</form>
What am I doing wrong?
You are not re-fetching the order details after the save function. So the details are getting from the initial state, so the result is old, but the next reload it changes to updated value and shows the right output.
But still it's unclear where you are loading the order object.
guys. I was going ok with my app until I got to this type of form, written below. I have 2 items in this table and the ID's are "1" and "2". Even though, if I press "Submit" on the "1" ID item, it prints me "2" every time. Does anyone have a clue what might be the problem? Thank you.
<form method="POST">
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
<?php } ?>
</form>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>
You're outputting two items into the same form with the same name. When you click the Submit button, it gathers all of the fields in the form based on the name attribute and sends them on to wherever the form is being posted.
If you want to have a button that submits each ID with a separate button, you'd want to try having a new form for each ID. There are other ways of doing this too, but based on your current code, try something like this:
<?php
$query = "SELECT * FROM table";
while($row = $query->fetch_array())
{
$id = $row['id'];
?>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>" >
<input type="submit" name="submit">
</form>
<?php } ?>
<?php
if(isset($_POST['submit']))
{
echo $_POST['id'];
}
?>
A 'quiz' currently contains a quiz id, name, description and topic (from a topic table).
I am wanting to setup a simple 'Edit Quiz' page.
The problem is - if a quiz is called 'Quiz1' and I change the quiz name on the edit page to 'Quiz2', once the save button is clicked it will revert back to 'Quiz1' and not stored.
I have setup an echo as shown in the code to check that they are actually getting stored, after the save button is clicked it would show 'Quiz1' but this value is not stored in my database. The SQL Has been tested on PhpMyAdmin and seems to work too.
PHP Code:
<? if (!empty($_POST)) {
$id = $_POST['qzid'];
$qzname = $_POST['qzname'];
$qzdesc = $_POST['qzdesc'];
$ctname = $_POST['ctname'];
$checkQuiz = $db->prepare("SELECT qz_name FROM quizzes WHERE qz_name = :qz_name");
$checkQuiz->execute(array(':qz_name' => $qzname));
$qzChanged = "Quiz details updated successfully";
$sql = "UPDATE quizzes SET `qz_name` = :qzname, `qz_desc` = :qzdesc WHERE `quizzes`.`id` = :qzid";
$q = $db->prepare($sql);
$q->execute(array(':qzname' => $qzname, ':qzdesc' => $qzdesc, ':qzid' => $id));
echo $qzname, $qzdesc; //THIS RETURNS THE CHANGED VALUES
}?>
HTML Code:
<form action="edit_quiz.php?id=<?php echo $row['id'] ?>" method="POST">
<input type="hidden" name="qzid" id="qzid" value=""/>
<!-- selection box -->
<p>Topic Name:
<select class="form-control" name="ctname" id="ctname">
<?php
while ($tresult = $stmt->fetch()) {
echo "<option>" . $tresult["ct_name"] . "</option>";
}
?>
</select>
</p>
<p>Quiz Name: <input type="text" name="qzname" value="<?php echo $row['qz_name']; ?>"/></p>
<p>Quiz Description: <textarea name="qzdesc" value=""/><?php echo $row['qz_desc']; ?> </textarea></p>
<input type="submit" class="btn btn-success" value="Save"/> <a class="btn" href="quizzes.php">Back</a>
Please note this code has been simplified.
Any help I would appreciate very much thanks!
You are not posting the id it looks like.
Is your database named localhost, otherwise remove that part.
can you help me with displaying output from search form ?
My search form is in sidebar and i would like to display result on main content div.
Navigation is done by switch
this is form in sidebar
<form name="searchform" method="get" action="index.php?page=search">
<input type="text" name="searchword" size="15" />
<input type="submit" name="search" value="Click" class="formbutton" />
</form>
this is switch that should include output to the main content:
switch($_GET['page']){
case "search":
include("includes/search.php");
break;
}
and this is included search.php which should display result:
<?php
include('connect.php');
if(isset($_GET['search'])){
$search_value = $_GET['searchword'];
$query = " SELECT * FROM Articles WHERE keywords LIKE '$search_value'";
$run = mysql_query($query);
while($row=mysql_fetch_array($run)){
$post_id = $row['id'];
$post_title = $row['title'];
echo "<p><a href='index.php?page=post&&post=$post_id'>$post_title</a></p></ br>";
}
}
?>
What about if you specify a different page entirely in the form action instead of index.php? What you did is tell the form to use the index.php and send the GET data to it. If that doesn't help then be more specific on what you want
I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)