How to set some value in PHP $_REQUEST variable?

Hi there

I am trying to apply server side validation on a form submission but its not working here is my code of form

   <form method="post" action="addUserDao.php">
      <tr><td>Username</td><td><input type="text" name="username" class="username"/><?php if(isset($_REQUEST['namemsg'])){echo $_REQUEST['usernamemsg'];}?></td></tr>
      <tr><td>UserId</td><td><input type="text" name="userid" class="userid"/><?php if(isset($_REQUEST['useridmsg'])){echo $_REQUEST['useridmsg'];}?></td></tr>
      <tr><td>Password</td><td><input type="password" name="userpassword" class="userpassword"/><?php if(isset($_REQUEST['userpswdmsg'])){echo $_REQUEST['userpswdmsg'];}?></td></tr>
      <tr><td>Email</td><td><input type="text" name="useremail" class="useremail"/><?php if(isset($_REQUEST['useremailmsg'])){echo $_REQUEST['useremailmsg'];}?></td></tr>
      <tr><td>Mobile No.</td><td><input type="text" name="usermobile" class="usermobile"/><?php if(isset($_REQUEST['usermobilemsg'])){echo $_REQUEST['usermobilemsg'];}?></td></tr>
      <tr><td></td><td><input type="submit" value="Register"/></td></tr>

and this is my addUserDao class

    include "commonFunctions.php";

     $_REQUEST['usernamemsg']="Username Cannot be Empty";
     echo '<script type="text/javascript">
      var carnr;
      carnr = "'.$username.'"
     </script>' ;

     $_REQUEST['useridmsg']="UserId cannot be Empty";

     $_REQUEST['userpswdmsg']="UserPassword cannot be Empty";

     $_REQUEST['useremailmsg']="UserEmail cannot be Empty";

     $_REQUEST['usermobilemsg']="UserMobile cannot be Empty";


and this is commonFunctions.php file

        function redirect($url) {
        header('Location: '.$url);

when I enter empty name in fields I don’t get any message display on it(Those set on addUserDao.php file).

Where is mistake?

The $_REQUEST array is where user submitted data can be accessed from that has been submitted the a url query string or a get method on a form ($_GET), data submitted via the post method of a form ($_POST) or a cookie ($_COOKIE). In the context of what you’re trying to do you should be using sessions

Ok So I am a complete beginner in PHP

Just guide me what is proper way to use a server side validation in PHP.
In Java I use this approach

Get Value From JSP to Servlet->
Perfom Some Calculation->
Forward Page to JSP with attribute set on request.setAttribute(“”)->
Access that attribute is JSP via Tag library

what about php?

get value from user input =>
perform validation =>
output result based on validation

I Know this is general logic.And it will be same

but what in terms of PHP I want to know:

where I should perform same page or different page?
How to set value on request or session?
How to access them in target page?

these things I want to know.

Here is some documentation on session handling in PHP. Apart from that, you’d usually have a hook in one of your controllers somewhere where you do your session munging. It really depends on what you are trying to do. Reading the link I posted should help you understand what you need to know about the session handling itself; architecturally, it really depends on your goals.

@vngx - You seem to be asking how to design a php application. It’s not an easy question to answer.

You might want to take a look at these two articles:

If you can follow the above articles then you should have a basic idea of building a php web application. At that point you can decide if a framework suits your needs or to roll your own.


Ok So here I have made some change in my code to test validation

<!doctype html>
 <meta charset="utf-8">
 <title>Employee Information Sample HTML Form</title>
 <style type="text/css">
 .errorMsg{border:1px solid red;}
 .message{color:red; font-weight:bold;}




   $errorMsg="error:You did not enter a name";

   $errorMsg="error:Please enter number.";

   //check if the number field is numeric
   $errorMsg="error:Please enter numeric value.";

   $errorMsg="error:Number should be ten digits";

 //check if email field is empty
 $errorMsg="error:You did not enter a email.";

 echo "Success";

  <?php if(isset($errorMsg)){ echo "<p class='message'>".$errorMsg."</p>";} ?>
 <form name="registration" id="registration" method="post" action="">
  <table width="400" border="0" align="center" cellpadding="4" cellspacing="1">
  <td>Employee Name:</td>
  <td><input name="emp_name" type="text" id="emp_name" value="
   <?php  if(isset($name)){echo         $name;} ?>"
  <?php if(isset($code)&&($code==1)){echo "class=errorMsg" ;} ?> ></td>
  <td>Contact No.:</td>
  <td><input name="emp_number" type="text" id="emp_number" value="
  <?php       if(isset($number)){echo $number;} ?>"
  <?php if(isset($code)&&($code==2)){echo "class=errorMsg";}?> > </td>
  <td>Personal Email:</td>
   <td><input name="emp_email" type="text" id="emp_email" value="
  <?php if(isset($email)){echo  $email;} ?>"
   <?php if(isset($code)&&$code==3){echo "class=errorMsg" ;} ?> ></td>
  <td><input type="submit" name="Submit" value="Submit"></td>

so problem with this when I submit this form there is POST url on the top.I want that URL to be change to GET on top.Each time I try to refresh the page POST request is called.

How can I do this?

What do you mean there’s a POST url on the top? On the top of what?

And what is the line of code

if(isset($name)){echo $name;}

as there doesn’t seem to be anything that sets $name

You could change the method to “get”.
Still if you refresh your form after submitting it, the data will be resent.


that was just beginner trouble

set variable:


and on requested page


Is that supposed to be setting a $_GET, a $_POST or a cookie -.

$_REQUEST is just a way to reference values from all three without knowing which of the three the value came from - and hence is recommended to NOT be used.

1 Like

yeah use $_GET or $_POST to get request parameter

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.