从另一个页面抓取表单数据并以html格式显示

时间:2016-06-06 03:39:17

标签: php html

我有一个用餐类型页面(breakfast.php),用户可以选择早餐套餐。确认包裹选择后,我想在homepage.php中显示该选择。如果我使用input ='text'并获取表单数据,它可以很好地工作,但由于某种原因它不会选择我的h4元素的内容。即使有4或5个包裹,有没有办法获取选择?

这是我的代码:

breakfast.php

<form action="homePage.php" method="post">

<div class= "breakfast">




<u><h2>The Share Collection</h2></u>
</br>
<img src= "\images\Breakfast\breakfast1.jpg" class="funky">
</br>
</br>
<dl>
  <dd>Mini jar of housemade granola: vanilla yoghurt, fresh fruit (v) (6)</dd>
  <dd>Goat cheese, cherry tomato &amp; basil, mushroom tart (v) (8)</dd>
  <dd>Mini slider jamon serrano, fig jam, rocket, brie &amp; black pepper (8)</dd>
  <dd>Chef's mini sweet muffin (v) (8)</dd>
  <dd>Bowl of strawberries &amp; grapes (v) (1)</dd>
</dl>

</br>
<input type='button' value='Select This Package' id='button3' onclick="changemyButton()">

<hr>

<section> 
<h1 id="logo">Total Ordered</h1></section>
<hr>
</br>

<h4 id ="selection" name="foodchoice">   </h4>
<br>
<br>
<br>
<br>
<input type="submit" id="button4" value="Submit Your Selection" name="submit">

</center>
</form>

homepage.php

<p> You have selected <?php echo $_POST["foodchoice"];?><br> as your order!

我是php的新手,希望以最佳方式执行此操作。 感谢。

3 个答案:

答案 0 :(得分:0)

您应该使用不同的html标记而不是h4标记。 h4是一个标头标签,除非你使用ajax,否则它不会发送表单数据。对于你正在做的事情,你可以使用select和option标签。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

答案 1 :(得分:0)

这可能就是您所需要的,您可以根据需要更改警报()

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">        </script>
 <div class= "breakfast">
 <u><h2>The Share Collection</h2></u>
 </br>
 <img src= "\images\Breakfast\breakfast1.jpg" class="funky">
 </br>
 </br>
 <dl>
 <dd>Mini jar of housemade granola: vanilla yoghurt, fresh fruit (v) (6)</dd>
 <dd>Goat cheese, cherry tomato &amp; basil, mushroom tart (v) (8)</dd>
 <dd>Mini slider jamon serrano, fig jam, rocket, brie &amp; black pepper (8) </dd>
 <dd>Chef's mini sweet muffin (v) (8)</dd>
 <dd>Bowl of strawberries &amp; grapes (v) (1)</dd>
 </dl>
 </br>
 <input type='button' value='Select This Package' id='button3' onclick="changemyButton()">
<hr>
   <section> 
    <h1 id="logo">Total Ordered</h1></section>
<hr>
</br>
<h4 id ="foodchoice" name="foodchoice">  ABCD  </h4>
<br>
<br>
<br>
<br>
<input type="button" id="submit" value="Submit Your Selection" name="submit">
</div>
<div id="myRes">DIV TO DISPLAY RESULT</div>
<script>
$(document).ready(function() {
   $("#submit").click(function() {
    var var1    = $("#foodchoice").html();
    $.ajax({
        url     : 'homePage.php',
        type    : 'post',
        data    : {foodchoice : var1},
        success : function(data) {
            $("#myRes").html(data); 
        },
        error   : function(data) {
            alert("Some thing went wrong"); 
        }
    });
  });

});
</script>

在这里你可以看到breakfast.php将html值发送到homePage.php

  

编辑:这是另一种可以使用它而不是Ajax的方法

 <form action="homePage.php" method="post"> 
 <h4 id="meals"  name="meals" > Pizza</h4>
 <input type="hidden" id="hiddeninput" name="hiddeninput"></textarea>
 <input type="submit" id="subs" name="subs" value="Submit"/>
 </form>
 <script>
  $(function(){
    $('#subs').click(function () {
     var mysave = $('#meals').html();
     $('#hiddeninput').val(mysave);
    });
  });
 </script>

homePage.php

echo $_POST['hiddeninput'];

答案 2 :(得分:0)

您应该实际循环选择的项目示例:
if(!empty($_POST['foodchoice'])) { foreach($_POST['foodchoice'] as $foodchoise) { //your code })

我创建了一个简单的示例,示例中只考虑了一个复选框而不是按钮。

<form action="homepage.php" method="post">
<input type="checkbox" name="foodchoice[]" value="Fresh Fruit">
<input type="submit" value="Submit!">

然后是php代码:
You have selected <?php if(!empty($_POST['foodchoice'])) { foreach($_POST['foodchoice'] as $foodchoise) { ?><br><?php echo $foodchoise;} }?> <br>as your order!