drwghu6386 2018-07-13 03:22
浏览 44
已采纳

如何将html数据发送到另一个php页面

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.

展开全部

  • 写回答

2条回答 默认 最新

  • doubao6464 2018-07-13 03:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部