user8454964
user8454964

Reputation:

How to pass name of an input through GET request in PHP

I am building a website CMS in PHP OOP and in this CMS, basically there's an option for admins of a site to update their navigation menu items. So it basically look like this:

print screen

And the code for this goes like this:

<?php 
    if (isset($_GET['id'])){
        $id = $_GET['id'];
        $menuSet = new Menus();
        $menuSet->SelectMenuById($id);
        echo "
        <div class='content-wrapper'>
            <section class='content-header'>
                <h1>
                    Menu Settings
                    <small>".$menuSet->GetMenuName()."</small>
                </h1>
                <ol class='breadcrumb'>
                    <li class='active'>menusettings.php</li>
                </ol>
            </section>
            <section class='content'>
                <div class='row'>
                    <div class='col-xs-12'>
                      <div class='box'>
                        <div class='box-header'>
                          <h3 class='box-title'></h3>

                          <div class='box-tools'>
                            <div class='input-group input-group-sm' style='width: 150px;'>
                              <input type='text' name='table_search' class='form-control pull-right' placeholder='Search'>

                              <div class='input-group-btn'>
                                <button type='submit' class='btn btn-default'><i class='fa fa-search'></i></button>
                              </div>
                            </div>
                          </div>
                        </div>
                        <!-- /.box-header -->
                        <div class='box-body table-responsive no-padding'>
                          <table class='table table-hover'>
                            <tr>
                              <th>Number</th>
                              <th>Link</th>
                              <th></th>
                            </tr>
                            ";
                            for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
                                $func = "GetMenuLink".$i;
                                /* $menuItemValue = $menuSet->{$func}();
                                if (!$menuItemValue) {
                                    continue;
                                } */
                                echo "
                                    <tr>
                                    <td>$i</td>
                                    <td><input type='text' name='' value='".$menuSet->$func()."'/></td>
                                    <td>
                                        <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
                                        <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
                                    </td>
                                    </tr>
                                ";
                            }
                            echo "
                          </table>
                        </div>
                        <!-- /.box-body -->
                      </div>
                      <!-- /.box -->
                    </div>
                  </div>
            </section>
        </div>
    ";
    }else{
        echo "
        <div class='content-wrapper'>
            <section class='content-header'>
                <h1>
                    Menu Settings
                    <small></small>
                </h1>
                <ol class='breadcrumb'>
                    <li class='active'>menusettings.php</li>
                </ol>
            </section>
            <section class='content' style='text-align:center;'>
                <h5><strong> 
                    You need to adjust the <span style='color:red'>menu_name</span> in the url first. So go back to <a href='dashboard.php'>dashboard</a> page.</br>
                </strong></h5>   
            </section>
        </div>
        ";
    }
    ?>

So as you can see I have used t a for loop to retrieve data from MySQL table: (Starts at line 43)

for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
                                $func = "GetMenuLink".$i;
                                /* $menuItemValue = $menuSet->{$func}();
                                if (!$menuItemValue) {
                                    continue;
                                } */
                                echo "
                                    <tr>
                                    <td>$i</td>
                                    <td><input type='text' name='' value='".$menuSet->$func()."'/></td>
                                    <td>
                                        <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
                                        <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
                                    </td>
                                    </tr>
                                ";
                            }

And here is the table which contains the items name:

print screen 2

And here is the class (Menu.class.php) that I have used for this:

    <?php 
    class Menus
    {
        public $id,$mname,$menui,$menul1,$menul2,$menul3,$menul4,$menul5,$menul6,$menul7,$menul8,$menul9,$menul10,$menul11,$menul12,$menul13;
        public function __construct()
        {
            $this->db = new Connection();
            $this->db = $this->db->dbConnect();
        }
        public function GetMenus()
        {
            if(empty($name))
            {
                $menu = $this->db->prepare("select * from menu_nav");
                $menu->execute();
                $menu_array = array();
                while($row = $menu->fetch())
                {
                    $menu_array[] = $row;
                }
                return $menu_array;
            }
            else
            {
                header("Location: php/includes/errors/008.php");
                exit();
            }
        }
        public function SelectMenuById($id)
        {
            if(!empty($id))
            {
                $mnu = $this->db->prepare("select * from menu_nav where id = ?");
                $mnu->bindParam(1,$id);
                $mnu->execute();
                while($row = $mnu->fetch())
                {
                    $this->id           = $row['id'];
                    $this->mname        = $row['menu_name'];
                    $this->menui        = $row['menu_items'];
                    $this->menul1       = $row['menu_link_1'];
                    $this->menul2       = $row['menu_link_2'];
                    $this->menul3       = $row['menu_link_3'];
                    $this->menul4       = $row['menu_link_4'];
                    $this->menul5       = $row['menu_link_5'];
                    $this->menul6       = $row['menu_link_6'];
                    $this->menul7       = $row['menu_link_7'];
                    $this->menul8       = $row['menu_link_8'];
                    $this->menul9       = $row['menu_link_9'];
                    $this->menul10      = $row['menu_link_10'];
                    $this->menul11      = $row['menu_link_11'];
                    $this->menul12      = $row['menu_link_12'];
                    $this->menul13      = $row['menu_link_13'];
                }
            }
            else
            {
                header("Location: php/includes/errors/009.php");
                exit();
            }
        }
        public function DeleteMenu($id)
        {
            if(!empty($id))
            {
                $adm = $this->db->prepare("delete from menu_nav where id = ?");
                $adm->bindParam(1,$id);
                $adm->execute();
            }
            else
            {
                header("Location: php/includes/errors/010.php");
                exit();
            }
        }
        public function DeleteMenuItem($i)
        {
            if(!empty($i))
            {
                $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = NULL");
                $adm->bindParam(1,$i);
                $adm->execute();
            }
            else
            {
                header("Location: php/includes/errors/011.php");
                exit();
            }
        }
        public function EditMenuItem($i,$e)
        {
            if(!empty($i))
            {
                $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = $e");
                $adm->bindParam(1,$i);
                $adm->execute();
            }
            else
            {
                header("Location: php/includes/errors/012.php");
                exit();
            }
        }
        public function GetId()
        {
            return $this->id;
        }
        public function GetMenuName()
        {
            return $this->mname;
        }
        public function GetMenuItems()
        {
            return $this->menui;
        }
        public function GetMenuLink1()
        {
            return $this->menul1;
        }
        public function GetMenuLink2()
        {
            return $this->menul2;
        }
        public function GetMenuLink3()
        {
            return $this->menul3;
        }
        public function GetMenuLink4()
        {
            return $this->menul4;
        }
        public function GetMenuLink5()
        {
            return $this->menul5;
        }
        public function GetMenuLink6()
        {
            return $this->menul6;
        }
        public function GetMenuLink7()
        {
            return $this->menul7;
        }
        public function GetMenuLink8()
        {
            return $this->menul8;
        }
        public function GetMenuLink9()
        {
            return $this->menul9;
        }
        public function GetMenuLink10()
        {
            return $this->menul10;
        }
        public function GetMenuLink11()
        {
            return $this->menul11;
        }
        public function GetMenuLink12()
        {
            return $this->menul12;
        }
        public function GetMenuLink13()
        {
            return $this->menul13;
        }
    }
?>

So everything works fine except one thing...

The problem is I can not update and change the menu items, because I have provided an input field and I don't know how to pass the data of this field through itemedit.php page within it's menu nav id:

<tr>
                                <td>$i</td>
                                <td><input type='text' name='' value='".$menuSet->$func()."'/></td>
                                <td>
                                    <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
                                    <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a>
                                </td>
                                </tr>

So please any idea, suggestion or solution on this?

And itemedit.php goes like this also:

    if (isset($_GET['i'])){
        $i = $_GET['i'];
        $e = // value of the updated input
        $editItem = new Menus();

        if(isset($_POST['yes'])){
            $editItem->EditMenuItem($i,$e);
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">';   
            exit; 
        }
        if(isset($_POST['no'])){
            echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">';    
            exit; 
        }
echo ... 

Upvotes: 2

Views: 65

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

There's no <form> encapsulating the input elements. Plus you have use an <input type="submit" ... > element instead of anchor tag to send user's inputted data to itemedit.php page. So change your for loop in the following way,

for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
    $func = "GetMenuLink".$i;
    ?>
        <tr>
            <form action="itemedit.php?i=<?php echo $i; ?>" method="post">
        <td>$i</td>
        <td><input type='text' name='e' value='<?php echo $menuSet->$func(); ?>'/></td>
        <td>
            <input type='submit' value='Edit' class='glyphicon glyphicon-pencil' />&nbsp; 
            </form>
            <a title='Remove' href='itemdelete.php?i=<?php echo $i; ?>'><span class='glyphicon glyphicon-remove'></span></a>
        </td>
        </tr>
    <?php
}

And in itemedit.php page, process your form's submitted data in the following way,

if (isset($_GET['i'])){
    $i = $_GET['i'];
    $e = $_POST['e'];
    $editItem = new Menus();
    // update the menu items
    $editItem->EditMenuItem($i,$e);
    ...

Update(1):

Your prepared statement and bindParam() method call is also wrong. Those should be like this:

$adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = ?");
$adm->bindParam(1, $e, PDO::PARAM_STR);

Upvotes: 2

Related Questions