smile
smile

Reputation: 117

getting id from post request

i'm trying to make CMS with PHP

i need to get the if from post request

<form action="updatevideo.php" method="post" role="form">

    Title:      <input name="title" type="text" required> <br />
    description:<input name="desc" type="text"  required> <br />
    url:        <input name="ytl" type="text"  required> <br />
    <input type="hidden"  name="id"  />
    <input type="submit" name="addVideo" value="Add New Video" />
</form>

how can i make this input's value = id

    <input type="hidden"  name="id"  />

on control page

public function Update()
{
    /*
        1-get data into variables 

        2-validation

        3-Database
    */

    if(isset($_GET['id']) && (int)$_GET['id']>0)
    {

        $id = (int)$_GET['id'];
        $user = $this->videoModel->Get_By_Id($id);
        print_r($user);
        echo
        '
        <!DOCTYPE html>
            <html>
            <head>
             <title>Page Title</title>
             </head>
                <body>

                    <form action="updatevideo.php" method="post" role="form">

                       Title:      <input name="title" type="text" required> <br />
                       description:<input name="desc" type="text"  required> <br />
                       url:        <input name="ytl" type="text"  required> <br />
                                   <input type="hidden"  name="id"  />
                                   <input type="submit" name="addVideo" value="Add New Video" />
                    </form>

                </body>
            </html>
           ';        
    }


    else
    {


        if(isset($_POST['addVideo']))
        {
            $id    = $_POST['id'];
            echo "5ara" ;
            $title = $_POST['title'];
            $desc  = $_POST['desc'];
            $url   = $_POST['ytl'];

            //Validation

            $vid = $this->getVid($url); //video id -_-
            $data = array(

                'title'      => $title,
                'desc'       => $desc,
                'vid'        => $vid
           );



           if($this->videoModel->Update($id,$data))
           {
               System::Get('tpl')->assign('message','User Updated');
               System::Get('tpl')->draw('success');
           } 


           else
           {
               System::Get('tpl')->assign('message','Error Updating User');
               System::Get('tpl')->draw('error');                   
           }
        }


        else
        {
           System::Get('tpl')->assign('message','NO USER CHOSEN');
           System::Get('tpl')->draw('error');                
        }
    }

}

Upvotes: 0

Views: 9569

Answers (3)

RiggsFolly
RiggsFolly

Reputation: 94662

Quite simple really. As you checked it exists and moved it into a variable you can just echo the $id into the value attribute of that input tag.

<input type="hidden" name="id" value="' . $id . '" />

Using your code:

echo '
    <!DOCTYPE html>
    <html>
    <head>
         <title>Page Title</title>
     </head>
     <body>

          <form action="updatevideo.php" method="post" role="form">

               Title:      <input name="title" type="text" required> <br />
               description:<input name="desc" type="text"  required> <br />
               url:        <input name="ytl" type="text"  required> <br />
              <input type="hidden"  name="id" value="' . $id . '" />
                               <input type="submit" name="addVideo" value="Add New Video" />
           </form>
      </body>
      </html>';        

Upvotes: 1

DeDee
DeDee

Reputation: 2002

Edited due to question edit.

<input type="hidden" name="id" value="'.$id.'" />

You echo a big block of text, this way you can concatenate the $id by stopping block output, including $id and resuming block output.

Upvotes: 1

Harshal
Harshal

Reputation: 3622

Define the VALUE attribute :

<input type="hidden" name="id" value="ID_VALUE"/>

OR if you have id in varible than use below code :

<input type="hidden" name="id" value="<?php echo $id; ?>"/>

OR As you are using the HTML in PHP than:

<input type="hidden" name="id" value="'.$id.'"/>

ID_Value should be your id like 1,2,3..etc.

And Get the id on Action page :

$id = $_POST['id'];

Apart from this you have mention POST in form method and on your action page you are trying to get the values using GET method, which is wrong.

Upvotes: 1

Related Questions