0

hey I'm new in php so didn't know more about it my question is : I have create multiple forms in 1 php file and i am trying to pass a variable with same name $action from multiple form in name attribute of hidden input type tag to another php from where i can retrieve it by $_POST[] method like as $_POST['$action'] but it showing error :Undefined index: $action

i have 1st php file :studentInfo.php and code are as follow:

<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>

and 2nd php file as:

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();
}
Akash sharma
  • 295
  • 3
  • 12

0 Answers0