drwghu6386 2018-07-13 11: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 11: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条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致