Reputation: 454
Am getting data from database and its being echoed as shown below.it works fine
<?php
include 'retrieveAllPosts.php';
foreach ($studentsResults as $posts)
{
$title = htmlentities($posts['Title']);
$Location = htmlentities($posts['Location']);
$Description= htmlentities($posts['Description']);
$companyname = htmlentities($posts['companyname']);
echo "
<div class='col-md-6 z-depth-1-half' style='margin-left:250px; margin-top:10px;margin-bottom: 10px' id='hey'>
<h3>$title</h3>
<p name='location'>$Location</p>
<h6><b>Description</b></h6>
<p>$Description</p>
<p>$companyname</p>
<form action='showPosts.php' method ='POST'>
<button class='btn btn-primary' type='submit'>Apply</button>
</form>
</div>
";
}
?>
many divs according to the number of records in the database are displayed.How can i get the location and description data when the user clicks the submit button of a only that div and send it to showposts.php. The same should occur for all the other divs.
Upvotes: 0
Views: 67
Reputation: 191
use this solution to send your data id to other php page
<form action='showPosts.php' method ='POST'>
<input type="hidden" name="postid" value="<?php echo $postid; ?>">
<button class='btn btn-primary' type='submit'>Apply</button>
</form>
or you can simply use
<a href="showPosts.php?id=<?php echo $postid; ?>" class='btn btn-primary'>Apply</a>
Upvotes: 1
Reputation: 61784
If you add those fields as hidden fields into your form, then it's really simple - the values will be submitted back to the server when the submit button is clicked:
<form action='showPosts.php' method ='POST'>
<input type='hidden' name='Description' value='$Description'/>
<input type='hidden' name='Location' value='$Location'/>
<button class='btn btn-primary' type='submit'>Apply</button>
</form>
I would perhaps suggest though that you'd be better to submit simply a unique ID for the post, rather than particular fields from it. Then the server can be certain which post is really being referred to, and it can of course retrieve the rest of the data from the database again if required. e.g. assuming you have some variable called $PostID
which is the ID of the post from the database, then you can do:
<form action='showPosts.php' method ='POST'>
<input type='hidden' name='PostID' value='$PostID'/>
<button class='btn btn-primary' type='submit'>Apply</button>
</form>
P.S. Including id='hey'
in your <div
will result in invalid HTML - IDs are meant to uniquely identify an element, but since you are echo-ing this element multiple times you'll end up with many elements with the same ID. Obviously this makes no sense and is not allowed. It'll give you problems if you try to use JavaScript to select the div, for instance.
Upvotes: 4