2013-04-09 120 views
0

我如何在另一页上回显以下结果。我需要它回应用户选择的内容。PHP回声结果

<strong>Return</strong>    
    <select id="Date" name="Date">      
    <option value="0">--Select date/time--</option> 
    <?php foreach ($return as $return) { ?> 
     <option value="<?php echo $return; ?>"><?php echo $return; ?></option> 
    <?php } ?> 

感谢

当用户进行选择的全部代码。他们在这个页面上选择的信息是我想在其他页面上回复的内容。

<!DOCTYPE html> 
<?php 
session_start(); 
?> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="viewport" content="width=device-width, initial-scale=1" /> 
     <meta name="apple-mobile-web-app-capable" content="yes" /> 
     <meta name="apple-mobile-web-app-status-bar-style" content="black" /> 
     <title> 
     </title> 
     <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
     <link rel="stylesheet" href="my.css" /> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> 
     </script> 
     <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js"> 
     </script> 
     <script src="my.js"> 
     </script> 
     <!-- User-generated css --> 
     <style> 
     </style> 
     <!-- User-generated js --> 
     <script> 
      try { 

    $(function() { 

    }); 

    } catch (error) { 
    console.error("Your javascript has an error: " + error); 
    } 
     </script> 
    </head> 
    <body> 
     <!-- Home --> 
     <div data-role="page" id="page1"> 
      <div data-theme="a" data-role="header"> 
      <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left"> 
        Back 
       </a> 
       <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right"> 
       Home 
       </a> 
       <h3> 
        Book Car 
       </h3> 
      </div> 

      <div data-role="content"> 
       <h3> 
        Select date/time: 
       </h3> 
       <br /> 
<?php 
{ 
    mysql_connect("localhost" , "" , "") or die (mysql_error()); 
    mysql_select_db("") or die(mysql_error()); 

    $query = "SELECT `book_time`, `book_date`, `return_time`, `return_date` FROM `Rental`"; 

    //executes query on the database 
    $result = mysql_query ($query) or die ("didn't query"); 

    //this selects the results as rows 
    $num = mysql_num_rows ($result);  

    $return = array(); 
    $rent = array(); 
    while($row=mysql_fetch_assoc($result)) 
    { 

     $rent[] = $row['book_date'].' '.$row['book_time']; 
     $return[] = $row['return_date'].' '.$row['return_time']; 
    } 
} 
?>  

    <form method="post" action="car.php"> 
    <strong>Rent out</strong>    
    <select id="Date" name="Date">      
    <option value="0">--Select date/time--</option> 
    <?php foreach ($rent as $rent) { ?> 
     <option value="<?php echo $rent; ?>"><?php echo $rent; ?></option> 
    <?php } ?> 

    </select> 
    <br /> 

    <strong>Return</strong>    
    <select id="Date" name="Date">      
    <option value="0">--Select date/time--</option> 
    <?php foreach ($return as $return) { ?> 
     <option value="<?php echo $return; ?>"><?php echo $return; ?></option> 
    <?php } ?> 


    </select> 

       <br /> 
       <br /> 

       <input type="submit" name="Submit" value="Book" /> 
      </form> 

      </div> 
     </div> 
    </body> 
</html> 
+0

如何'$ return'设置? – 2013-04-09 20:30:46

+2

'foreach($ return作为$ return)'实际上工作吗? – Anther 2013-04-09 20:31:45

+0

@DarylGill,日期 – user2236990 2013-04-09 20:32:51

回答

3

型基础101:

~~~index.php~~~ 
<form action='the_new_page.php' method='POST'> 
    <input type='text' name='first_thing'> 
    <input type='hidden' name='some_hidden_guy' value='1'> 
    <input type='submit' name='submit_button' value='submit'> 
</form> 

当用户提交上述表格,浏览器会引导他们到the_new_page.php,并the_new_page.php会知道所有形式的信息。

表单通过namevalue,没有别的什么你需要知道的。 id纯粹是出于HTML目的,并且作为HTML中的规则,您不允许具有多个具有相同ID的元素。人们通常会混淆地将表单字段的id命名为表单字段的“名称”,这使得学习每种方法都很困难。

因此,提交表单,然后它会去你的第二页,然后之后你可以这样做:

~~~the_new_page.php~~~ 
//You can then do 
echo $_POST['some_hidden_guy'] //will be 1 
echo $_POST['first_thing'] //Will be whatever you inserted into the text box 

如果你想要的形式返回到当前页面你,你只需将action留为空白,或者作为<form action=''><form method='POST'>

为多个页面保留相同的信息!

现在,这是一种非常不程序的方式来做到这一点,因为您应该找到一种方法,无需在以前的页面上重复相同的代码。如果你不这样做,现在我警告你。这个应用程序的维护将成为一场噩梦,因为每当你想添加一个新的字段时,你都必须编辑每一个页面。在这个例子上。

现在,假设您将信息传递给car.php,并且您现在想在carplan.php上使用它。

~~~car.php~~~ 
    <form action='carplan.php' method='GET or POST, whichever it is you be using using'> 
     <input type='hidden' name='Date' value='<?php echo $_GET['date'] ?>'> 
     <input type='hidden' name='some_other_thing' value='<?php echo $_GET['some_other_thing'] ?>'> 
     <option name='plan_id'> 
      <?php foreach($plans as $plan): ?> 
       <select value='<?php echo $plan['id'] ?>'><?php echo $plan['name'] ?>'> 
      <?php endforeach; ?> 
     </option> 
     <input type='submit' name='submit' value='Get That Plan'> 
    </form> 

终于在carplan.php

~~~carplan.php~~~ 
    <form action='the_next_step.php' method='GET or POST, whichever you be using'> 
     <input type='hidden' name='Date' value='<?php echo $_GET['date'] ?>'> 
     <input type='hidden' name='some_other_thing' value='<?php echo $_GET['some_other_thing'] ?>'> 
     <input type='hidden' name='plan_id' value='<?php echo $_GET['plan_id']?>'> 
     <input type='submit' name='submit' value='The next step!'> 
    </form> 
+0

我遇到的问题是表单没有在页面上传递值。用户的结果需要显示在页面的最后。 – user2236990 2013-04-09 21:08:19

+0

更改你的表单'method ='GET''。它应该使变量非常明显,以及为什么变量不被传递。并在'car.php'中回显$ _GET ['Date']。在car.php上工作的 – Anther 2013-04-09 21:10:33

+0

。那么,如何让另一个页面上的工作不是car.php。 – user2236990 2013-04-09 21:21:52

0

你可以重复此通过..

echo $_GET['Date']; 

OR

echo $_POST['Date']; 

OR

echo $_REQUEST['Date']; 
+0

尝试所有3.他们都没有工作 – user2236990 2013-04-09 20:39:11

+1

最后一个和前两个之一应该在表单的'action'属性中命名的页面上工作。 – Barmar 2013-04-09 20:40:38

+1

最后和前两个...这意味着以上所有。 – 2013-04-09 20:42:41