如何通过PHP中的GET请求传递输入的名称 [英] How to pass name of an input through GET request in PHP

查看:122
本文介绍了如何通过PHP中的GET请求传递输入的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP OOP中构建一个网站CMS,并且在这个CMS中,基本上有一个选项让管理员更新他们的导航菜单项。所以它基本上是这样的:



打印屏幕



此代码如下所示:

 <?php 
if(isset($ _GET ['id'])){
$ id = $ _GET ['id'];
$ menuSet = new Menus();
$ menuSet-> SelectMenuById($ id);
echo
< div class ='content-wrapper'>
< section class ='content-header'>
< h1>
菜单设置
< 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> ($ i = 1; $ i< = $ menuSet-> GetMenuItems(); $ i ++){
$ func =GetMenuLink。$ i;
$ b;
$ b / * $ 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>& amp ; NBSP;
< a title ='删除'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>
菜单设置
< small>< / small>
< / h1>

可以看到我使用ta for循环从MySQL表中检索数据:
(从第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>& amp ; NBSP;
< a title ='删除'href ='itemdelete.php?i = $ i'>< span class ='glyphicon glyphicon-remove'>< / span>< / a>
< / td>
< / tr>
;
}

这里是包含项目名称:



打印屏幕2



以下是我为此所用的类(Menu.class.php)

 <?php 
class菜单
{
public $ id,$ mname,$ menui,$ menul1,$ menul2 ,$ menul3,$ menul4,$ menul5,$ menul6,$ menul7,$ menul8,$ menul9,$ menul10,$ menul11,$ menul12,$ menul13;
公共职能__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;
}
返回$ 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->准备(从menu_nav中删除,其中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();

$ b $公共函数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;
}
}
?>

所以一切正常,除了一件事...



问题是我无法更新和更改菜单项,因为我提供了一个输入字段,并且我不知道如何通过 itemedit.php 传递此字段的数据。 code>页面内的菜单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>& amp ; NBSP;
< a title ='删除'href ='itemdelete.php?i = $ i'>< span class ='glyphicon glyphicon-remove'>< / span>< / a>
< / td>
< / tr>

那么请给出任何想法,建议或解决方案?

itemedit.php 也是这样:

  if(isset($ _ GET ['i'])){
$ i = $ _GET ['i'];
$ e = //更新输入的值
$ editItem = new Menus();

if(isset($ _ POST ['yes'])){
$ editItem-> EditMenuItem($ i,$ e);
echo'< META HTTP-EQUIV =RefreshContent =0; URL = menus.php>';
出口;
}
if(isset($ _ POST ['no'])){
echo'< META HTTP-EQUIV =RefreshContent =0; URL = menus.php >';
出口;
}
echo ...


解决方案

封装 input 元素没有< form> >。另外,您还可以使用< input type =submit...> 元素来代替anchor标签,以将用户输入的数据发送到 itemedit.php 页面。因此,请按照以下方式更改您的 for 循环,

  for($ i = 1; $ i< = $ menuSet-> GetMenuItems(); $ i ++){
$ func =GetMenuLink。
?>
< tr>
< form action =itemedit.php?i =<?php echo $ i;?>方法= 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 ='删除'href ='itemdelete.php?i =<?php echo $ i; >'>< span class ='glyphicon glyphicon-remove'>< / span>< / a>
< / td>
< / tr>
<?php
}

itemedit.php 页面,按以下方式处理表单提交的数据:

  if(isset($ _ GET ['i' ])){
$ i = $ _GET ['i'];
$ e = $ _POST ['e'];
$ editItem = new Menus();
//更新菜单项
$ editItem-> EditMenuItem($ i,$ e);
...

更新(1): p>

您准备的语句和 bindParam()方法调用也是错误的。这些应该是这样的:

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


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

解决方案

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

这篇关于如何通过PHP中的GET请求传递输入的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆