diegoaguilar
diegoaguilar

Reputation: 8376

How $_POST variable/operation is managed in PHP?

When I do something like:

foreach($_POST as $post_key => $post_value){     
    /* Any code here*/  
}

So, something like:

$varSomething = $_POST['anything'];
$varSomethingElse = $_POST['somethingElse'];

Is it possible? When I catch a $_POST[' '], isn't that variable already consumed?

The main reason why I would do this is because after a form submission, I want to check wether some items of some type got certain value or not.

Is there aything else more appropiate?

Upvotes: 0

Views: 78

Answers (2)

hmrojas.p
hmrojas.p

Reputation: 582

Firstly the html code don't use variable types, for example, if you have

<input id="check" type="checkbox" />

without a established value, after that you have echo $_POST['chek'], you could think that the result would be a boolean value (false or true), but the correct result will be "on" or "off", you can coding this case. Also, if you want to know the type of your data, you can use regular expression on server side, for example:

<input type="text" id="number" value="1350" />

.....

PHP code

$data = $_POST['number'];
$regularExpression = "/^\d{1,10}$/";

if (preg_match($regularExpression, $data)) {
echo "Is numeric";
}

Good lucky.

Upvotes: 1

abhirathore2006
abhirathore2006

Reputation: 3745

if you don't know what is the name of element which is sending the data. the first method is ohk . but if know the name like password or username you can use second one in html

<input type="password" name ="password" />

in php

$pass_recvd=$_POST['password'];

there is no way to check the type i.e. text/password/checkbox/select etc. you have to do it on client side BEST WAY IS USING Jquery

if you wanna check whether a variable is set or not simple check by using isset method

if( isset($_POST['someVariableName'])) {}else{}

Upvotes: 0

Related Questions