简体   繁体   English

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

[英]how to send html data to another php page

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. 根据数据库中记录的数量显示许多div。当用户单击仅该div的提交按钮并将其发送到showposts.php时,如何获取位置和描述数据。 The same should occur for all the other divs. 其他所有div也会发生相同的情况。

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. 我也许建议您最好只为帖子提交一个唯一的ID,而不要为帖子中的特定字段提交。 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. 然后,服务器可以确定真正引用了哪个帖子,并且当然可以根据需要再次从数据库中检索其余数据。 eg assuming you have some variable called $PostID which is the ID of the post from the database, then you can do: 例如,假设您有一个名为$PostID变量,它是数据库中帖子的ID,则可以执行以下操作:

<form action='showPosts.php' method ='POST'>
  <input type='hidden' name='PostID' value='$PostID'/>
  <button class='btn btn-primary' type='submit'>Apply</button>
</form>

PS 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. PS在<div包含id='hey'将导致无效的HTML-ID旨在唯一标识一个元素,但是由于多次回显此元素,因此您将得到许多具有相同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. 例如,如果您尝试使用JavaScript选择div,它将给您带来问题。

use this solution to send your data id to other php page 使用此解决方案将您的数据ID发送到其他php页面

 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM