使用name属性将变量从php传递到另一个php?

时间:2015-06-09 20:42:24

标签: php forms input hidden

嘿,我是php的新手,所以不知道更多 我的问题是: 我在1个php文件中创建了多个表单 我试图将隐藏输入类型标签的名称属性中的多个表单的同名$ action变量传递给另一个PHP,我可以通过$ _POST []方法检索它 比如$ _POST ['$ action'] 但它显示错误:未定义的索引:$ action

我有第一个php文件:studentInfo.php,代码如下:

<form action="studentAction.php" method="post">
    <br/>ROLL NO: <input type="text" name="rollno"/><br/><br/>
    BRANCH : <input type="text" name="branch"/><br/><br/>
    NAME   : <input type="text" name="name"/><br/><br/>
    EMAIL  : <input type="text" name="email"/><br/><br/>
    PHONE  : <input type="text" name="phone"/><br/><br/>
    ADDRESS: <input type="text" name="address"/><br/><br/>
    <input type="hidden" name="<?php $action='add'?>" value="add"/>
    <input type="SUBMIT" value="  Add  "/>
</form>

<form action="studentAction.php" method="post">
    <input type="hidden" name="<?php $action='show'?>" value="shw"/>
    <input type="SUBMIT" value=" show "/>

    </form>

    <form action="studentAction.php" method="post">
    <input type="hidden" name="<?php $action='update'?>" value="upd"/>
    <input type="SUBMIT" value="Update"/>
    </form>

    <form action="studentAction.php" method="post">
    <input type="hidden" name="<?php $action='del'?>" value="del"/>
    <input type="SUBMIT" value="Delete"/>
    </form>

和第二个php文件:

if($_POST['$action']=="add"){
$name    = $_POST['name'];
$branch  = $_POST['branch'];
$rollno  = $_POST['rollno'];
$email   = $_POST['email'];
$phone   = $_POST['phone'];
$address = $_POST['address'];

$sql = "INSERT INTO studentinfo (rollno,branch,name,address,email,phone)
values('$rollno','$branch','$name','$address','$email','$phone')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
if($_POST['$action']=="shw"){
$sql1 = "SELECT * FROM studentInfo";
$result = $conn->query($sql1);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "rollno: " . $row["rollno"]. "<br/>";
        echo "branch: " . $row["branch"]. "<br/> ";
        echo "name: " . $row["name"]. "<br/>";
        echo "address:" . $row["address"]. "<br/>";
        echo "email: " . $row["email"]. "<br/>";
        echo "phone: " . $row["phone"]. "<br/>";
    }
} else {
    echo "0 results";
}
$conn->close();
}

0 个答案:

没有答案