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">
    <table>
      <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>
    </table>
    </form>

and this is my addUserDao class

    <?php
    include "commonFunctions.php";
  ?>
  
  <?php
    $username=$_POST['username'];
    $userid=$_POST['userid'];
    $userpassword=$_POST['userpassword'];
    $useremail=$_POST['useremail'];
    $usermobile=$_POST['usermobile'];
    

    if(empty($_POST['username'])){
     $_REQUEST['usernamemsg']="Username Cannot be Empty";
     echo '<script type="text/javascript">
      var carnr;
      carnr = "'.$username.'"
     console.log(carnr);
     </script>' ;
    }

    if(empty($_POST['userid'])){
     $_REQUEST['useridmsg']="UserId cannot be Empty";
    }

    if(empty($_POST['userpassword'])){
     $_REQUEST['userpswdmsg']="UserPassword cannot be Empty";
    }

    if(empty($_POST['useremail'])){
     $_REQUEST['useremailmsg']="UserEmail cannot be Empty";
    }

    if(empty($_POST['usermobile'])){
     $_REQUEST['usermobilemsg']="UserMobile cannot be Empty";
    }

    redirect("http://localhost:819/php/admin/addUser.php");
   ?>

and this is commonFunctions.php file

    <?php
        function redirect($url) {
        ob_start();
        header('Location: '.$url);
        ob_end_flush();
        die();
        }
     ?>

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.

2 Likes

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

<!doctype html>
 <html>
 <head>
 <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;}
  </style>
 </head>

 <body>



  <?php

  if(isset($_POST['Submit'])){
  $emp_name=trim($_POST["emp_name"]);
  $emp_number=trim($_POST["emp_number"]);
  $emp_email=trim($_POST["emp_email"]);

   if($emp_name==""){
   $errorMsg="error:You did not enter a name";
   $code="1";
    }

   elseif($emp_number==""){
   $errorMsg="error:Please enter number.";
   $code="2";
    }

   //check if the number field is numeric
   elseif(is_numeric(trim($emp_number))==false){
   $errorMsg="error:Please enter numeric value.";
  $code="2";
   }

    elseif(strlen($emp_number)<10){
   $errorMsg="error:Number should be ten digits";
   $code="2";
   }

 //check if email field is empty
 elseif($emp_email==""){
 $errorMsg="error:You did not enter a email.";
 $code="3";
 }

 else{
 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">
  <tr>
  <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>
  </tr>
  <tr>
  <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>
  </tr>
  <tr>
  <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>
  </tr>
  <tr>
  <td></td>
  <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
  </table>
  </form>
  </body>
   </html>

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.

yeah

that was just beginner trouble

set variable:

$_REQUEST[‘someVar’]=‘someValue’;

and on requested page

$_REQUEST[‘someVar’]

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.