检索php中的下拉框选择

时间:2015-10-01 23:17:10

标签: php html mysql ajax

我正在尝试从以下位置检索用户选择:

UserInterface.php

 <form name="formname" method="post" action="index.php">
    <div id='userList'>

        <?php
        //get constants for database
        require("database.php");

        // Opens a connection to a MySQL server
        $connection = mysqli_connect ($server, $username, $password);
        if (!$connection){  
            die('Not connected : ' . mysqli_error());
            }

        // Set the active MySQL database
        $db_selected = mysqli_select_db($connection, $database);
        if (!$db_selected) {
            die ('Can\'t use db : ' . mysqli_error($connection));
        }

        $query = "SELECT distinct user FROM route";
        $result = mysqli_query($connection, $query);


            echo '<select name="box" style="width:400px;">';

                $count = 1;

               while($r = mysqli_fetch_assoc($result)){
                echo "<option =".$count.">".$r['user']."</option>"; 
                $count++;
            }   
            echo '</select>';
            ?>
        <input type="submit" id="button" value="Get Started!"/> 

我希望在Map.php中使用它,在那里我将执行sql查询,而sql查询又用于生成XML。

Map.php

//get constants for database
require("database.php");

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);

//$user value dependant on user selection from dropdown menu
$query = "SELECT * FROM route WHERE user ='$selected'";
$result = mysqli_query($connection, $query);
if (!$result){
  die('Invalid query: ' . mysqli_error($connection));
}

//set header to xml
\header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = mysqli_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("alt", $row['alt']);
  $newnode->setAttribute("id", $row['id']);
  $newnode->setAttribute("user", $row['user']);
}

echo $dom->saveXML();

Map.php随后用于index.php(我的网页主页,其中包含所有内容),因为我使用ajax使用Map.php方法从downloadUrl检索xml。

的index.php

downloadUrl("Map.php", processXML);

我一直在尝试将下拉值恢复到Map.php,但我无法理解。我查看了以下选项:

$user = $_POST['box'];

session_start();
$_SESSION["user"] = $_POST['box'];

我还尝试将数据直接发布到Map.php并重定向到index.php,但这不起作用。我想这可能是因为我在Map.php中使用index.php从而创建了一系列的循环?!

有没有办法将数据发布到Map.php,然后只重定向到index.php一次?!

如果这种情况不连贯而且有点乱,我很抱歉,这已经很晚了,而且我对这个问题非常紧张。

任何帮助或指示都会非常感激 - 谢谢!

0 个答案:

没有答案