0

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.

1
  • fyi and totally unrelated: ids should be unique Commented Jul 13, 2018 at 11:24

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@K.Nehe No problem. If the answer has helped you solve your problem, please remember to mark it as "accepted" (click the tick symbol next to the question so it turns green). Thanks :-)
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.