在用户获取请求

时间:2017-05-10 12:55:13

标签: php jquery html

我的管理网页出现问题,当我需要进行用户更新时,wicth无法正常工作。 这是我的主页,页面应显示用户在选择表单上的详细信息:

<?php
if(isset($_POST['submit']))
{
    session_start();
    $data = $_SESSION['data'];
    unset($_GET['data']);
    header("location: modify.php");
}
?>

<html>
<head>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
        function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("txtHint").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET","getuser.php?q="+str,true);
                xmlhttp.send();
            }
        }
    </script>
</head>
<body>

    <form action="" method="post"> 
    <select name="users" onchange="showUser(this.value)">
      <option value="">Select a person:</option>
      <?php
        require_once('../conf.php');
        $query_list = "SELECT * FROM studentinfo ORDER BY(id) DESC";
        $result_list = mysql_query($query_list) or die('error in query_list');
    //  $n=1;
        while($row = mysql_fetch_array($result_list))
        {
        ?>
        <option value="<?php echo $row[0]; ?>"><?php echo $row[2]; ?></option>
        <?php
    //  $n=$n+1;
        }   
        ?> 
      </select>
    <input type="submit" name="submit"/>
     </form> 
    <br>
    <div id="txtHint">
        <b>listed here...</b>
    </div>
</body>
</html>

有我的getuser.php页面:

<html>
<head>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }

        table, td, th {
            border: 1px solid black;
            padding: 5px;
        }

        th {text-align: left;}
    </style>
</head>
<body>

    <?php
        require_once('../conf.php');
        session_start();
        $q = intval($_GET['q']);
        $getuser="SELECT * 
                FROM studentinfo 
                WHERE id = '".$q."'";
        $result = mysql_query($getuser) or die('error in getuser');
        echo "<table>";
        ?>
        <tr>
        <th>Id</th>
        <th>Mac</th>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
        <th>Department</th>
        <th>City</th>
        <th>State</th>
        </tr>
        <?php
        if($row = mysql_fetch_array($result)) {
            $_SESSION['data'] = $row;
            echo "<tr>";
            echo "<td>" . $row[0] . "</td>";
            echo "<td>" . $row[1] . "</td>";
            echo "<td>" . $row[2] . "</td>";
            echo "<td>" . $row[3] . "</td>";
            echo "<td>" . $row[4] . "</td>";
            echo "<td>" . $row[5] . "</td>";
            echo "<td>" . $row[6] . "</td>";
            echo "<td>" . $row[7] . "</td>";
            echo "</tr>";   
        }
        echo "</table>";
    ?>
</body>
</html>

最后我的modify.php页面:

<html>
<head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }

    table, td, th { 
        border: 1px solid black;
        padding: 5px;
    }

    th {text-align: left;}
    </style>
</head>
<body>
    <?php
    require_once('../conf.php');
    session_start();
    $data = $_SESSION['data'];
    //clear get data
    if(isset($_POST['submitbutton']))
    {
            echo "11111111";
            $edit="UPDATE TABLE studentinfo 
                    SET id=$data[0], 
                        mac='$_POST['Mac']', 
                        firstname='$_POST['FirstName']',
                        lastname='$_POST['LastName']', 
                        email='$_POST['Email']', 
                        department='$_POST['Department']', 
                        city='$_POST['City']', 
                        state='$_POST['State']'     
                    WHERE id=$data[0]";
            $result_edit=mysql_query($edit) or die ('edit user failed');
            if(!$result_edit)
                    echo "edit done";
            else
                    echo "edit failed";
    }


    //if(ISSET($_POST['submit']))

    ?>
    <form action="" type="post">
        <table>
            <tr>
                <th>Mac</th>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Email</th>
                <th>Department</th>
                <th>City</th>
                <th>State</th>
                </tr>
            <tr>


                <td>
                <input type="text" name="Mac" value="prova"></input>
                </td>
                <td>
                <input type="text" name="FirstName" value="<?php echo htmlspecialchars($data['firstname']); ?>" />
                </td>
                <td>
                <input type="text" name="LastName" value="<?php echo $data[3]; ?>" />
                </td>
                <td>
                <input type="text" name="Email" value="<?php echo $data[4]; ?>" />
                </td>
                <td>
                <input type="text" name="Department" value="<?php echo $data[5]; ?>" /></td>
                <td>
                <input type="text" name="City" value="<?php echo $data[6]; ?>" />
                </td>
                <td>
                <input type="text" name="State" value="<?php echo $data[7]; ?>" />
                </td>
                <td>
                <input type="submit" name="submitbutton" />
                </td>

            </tr>
        </table>
    </form>
</body>
</html>

现在,它看起来效果很好,直到修改页面。我无法理解为什么在modify.php页面中我无法在POST上保存变量以进行mysql上传。 有人可以帮助我理解为什么不工作。 感谢。

1 个答案:

答案 0 :(得分:0)

表单没有操作

<form action="" method="post"> 

也改变

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

需要将动作设置为某些php脚本以获取帖子数据