Jump to content

true statement


jimfog

Recommended Posts

I have a form and above it(in the script) I have the following function called: if (filled_out($_POST)){ echo 'thanks for your detail.';} Here is the function:

function filled_out($post){  	 if (isset($_POST)) {		return true;  }}

I do not think it is necessary to show the form code-it is just a simple form with some fields.The problem is that when I go to the page where the form is and WITHOUT submitting it or filling the fields the "thanks for your details"is triggered. I cannot understand how the $_POST variable gets set since the submit button is not pressed. It seems that it gets set be just going to the page of the form-something strange, as far as I know. I must say that the form and the form action are in the same page(although this does not play role here). The funny thing is I erased the form and still the if statement output true.I cannot figure out from where this post array gets set-probably I will have to post the whole script. In addition to the above, I used var_dump to check what is contained in the $_POST array and I get(as expected) the following:

array (size=0)  empty

Edited by jimfog
Link to comment
Share on other sites

It may be because $_POST is a super global array and it's considered 'set' all the time regardless if anything is in it or not. Probably be better to check if something is set inside the $_POST array like: isset($_POST['submit']);You can use the empty function to check if $_POST has anything inside once the form is submitted.

function filled_out() // $_POST is global, no need to pass it as a parameter {           if (!iempty($_POST))          {            return true;          }}

Edited by Don E
Link to comment
Share on other sites

It seems you are correct because in the php manual I found also this: and an empty GET or POST field returns an empty string instead of NULL,

Link to comment
Share on other sites

actually $_GET $_POST have assigned an empty array, not empty string. so isset() will always return true. if you want to check existance of an particular element of $_GET or $_POST isset() will work as intented.

Link to comment
Share on other sites

Ok I got that...here is another problem I encountered relevant to the form again-checking if the fields are empty. Here is the function to check if the fields are empty:

function filled_out($post){  // test that each variable has a value  foreach ($post as $key => $value) {	 if ($value == '') {	    return false;	 }elseif(!empty($value))	 { return true;}  }}

It does not work as expected though.If 2 fields are filled and not others are filled the code proceeds as though the user filled in all of the fields. Here is the if statement that calls the above function.

if(!filled_out($_POST))		 {echo 'you did not fill in all of the fields';}		 elseif(filled_out($_POST))		 {echo 'thanks';}	 }

Link to comment
Share on other sites

Ok I got that...here is another problem I encountered relevant to the form again-checking if the fields are empty. Here is the function to check if the fields are empty:
function filled_out($post){  // test that each variable has a value  foreach ($post as $key => $value) {	 if ($value == '') {		return false;	 }elseif(!empty($value))	 { return true;}  }}

It does not work as expected though.If 2 fields are filled and not others are filled the code proceeds as though the user filled in all of the fields. Here is the if statement that calls the above function.

if(!filled_out($_POST))		 {echo 'you did not fill in all of the fields';}		 elseif(filled_out($_POST))		 {echo 'thanks';}	 }

I think if you debugged your code you would probably find out what is going wrong, in that you are returning before you finish the loop. What would be better is to set a flag and just test for the "empty" condition and return when finished evaluating the entire array.
function filled_out($post){  $wasFilledOutCompletely = true;    // test that each key has a non empty string value  foreach ($post as $key => $value) {	 if ($value == '') {		$wasFilledOutCompletely =  false;	 }  }   return $wasFilledOutCompletely;}

edit: optionally, you could add a break statement after setting $wasFilledOutCompletely to false, since in your case all values must be required.

Edited by thescientist
Link to comment
Share on other sites

It worked...thanks. Although I have not yet understand how the logic works in your code. My debugger(xdebug), currently is not operational, so, I cannot debug the code and see what is hidden under the hood I have not tried yet though what are you mentioning about the break statement...I intend doing it.

Link to comment
Share on other sites

It worked...thanks. Although I have not yet understand how the logic works in your code.
it's pretty straightforward. It set a flag $wasFilledOutCompletely to an initial value of true. It then loops through $post and checks each member to see if each members value is a non empty string value. If is, then the user didn't fill out the form completely, so we would want to set $wasFilledOutCompletely to false; Then we return $wasFilledOutCompletely.
My debugger(xdebug), currently is not operational, so, I cannot debug the code and see what is hidden under the hood
You can't just use echo or print to output directly to the page?
I have not tried yet though what are you mentioning about the break statement...I intend doing it.
It's just one way. you could return directly from that condition too, but I like to keep my return statements at the end of my functions, and opt for flags for the most part.
Link to comment
Share on other sites

it's pretty straightforward. It set a flag $wasFilledOutCompletely to an initial value of true.
This is the point I do not understand...why use the flag and set it to true?
You can't just use echo or print to output directly to the page?
I thought you meant a proper debugger software...ok I got you now
Link to comment
Share on other sites

This is the point I do not understand...why use the flag and set it to true?
because you can set it to false. then return the value of the flag at the end of the function.
Link to comment
Share on other sites

  • 2 months later...

Sometimes in life you cannot get answers in the questions you ask...nonetheless these come completely afterwards and completely unexpectedly.. This is the case with the meaning of a flag which seems I am close at understanding it.I have one big function and inside of it 2 other:

function profile_check($required,$services,$pricelist){	 global $correctdata;	 $correctdata=true;	  if(!(val_phone($required['phone'])))		  {$correctdata=false;		  echo 'wrong phone number<br>';}     	 if(!(empty($services['service1']))||!(empty($pricelist['price1'])))			 { val_evr_else($services['service1'],$pricelist['price1']); }			//	 $addresstr=trim($required['address']);//	 $citytr=trim($required['address']);//	 $municipalitytr=trim($required['municipality']);							 return $correctdata;	   			}

II was trying to find a way that the inner function return true when the validation is correct and in addition this "true" of the inner function was "communicated" in the outer function...(at the same time if used the statement "return true" when the validation of the inner function was OK, this would create the problem that execution of the script would stop, something I do now want since there is a second function afterwards). Anyway...setting a flag helped me achieve that. Just look at the code and you will understand what I mean.A flag helped me achieve that

Link to comment
Share on other sites

why do you make all variables in your function global? Especially when you return them from said function?

Edited by thescientist
Link to comment
Share on other sites

I made $correctdata global for another reason-irrelevant to this topic.I am experimenting with something for which I am trying to find a solution-I will post if must to. As such...and for the moment...ignore the fact that I have a global variable. Just comment if the meaning of a flag is what I describe above.Just to confirm my/your rationale.

Link to comment
Share on other sites

It seems I made an error and I spoiled the flag functionality-I cannot find the error though, take a look at the code:

 function profile_check($required,$services,$pricelist) {        $correctdata=true;      if(!(val_phone($required['phone'])))           {$correctdata=false;          echo '<h2>Έχεις τα παρακάτω λάθη.</h2>              <br>Δεν έβαλες σωστό νούμερο τηλεφώνου<br>';}             //εδώ λήγει το phone validation      if(!(empty($services['service1']))||!(empty($pricelist['price1'])))             { val_evr_else($services['service1'],$pricelist['price1']); }                         var_dump($correctdata);        return $correctdata;        }  function val_evr_else($services,$pricelist){        if ((isset($services)) && ($pricelist == '')) {         $correctdata=false;         echo 'δεν έβαλες τιμή';     } else if ((isset($pricelist)) && ($services == '')) {         $correctdata=false;         echo 'δεν έβαλες υπηρεσία';     } else if (isset($pricelist, $services)) {         $validate = array(             array('formfield' => $pricelist, 'validate' => val_prices($pricelist), 'error' => 'Θα πρέπει να βάλεις αριθμητική τιμή'),             array('formfield' => $services, 'validate' => val_services($services), 'error' => 'Έχεις βάλει κατα λάθος νούμερο')         );       foreach ($validate as $value) {             if ($value['validate'] == false) {                 $correctdata=false;                 echo $value['error']. '<br>';             }         }              }    var_dump($correctdata);    return $correctdata;}

This statement here:

    if ((isset($services)) && ($pricelist == '')) {		 $correctdata=false;		 echo 'You did not entered a price';

checks if the user filled the price field in the form, otherwise $correctdata is set to false.The problem is that this is not communicated to the outer function profile_check.Profile_check returns $correctdata which OUGHT to be false when the price field is not filled, instead $coorrectdata is true, as set in the beginning of profile_check.

Link to comment
Share on other sites

$services and $pricelist are always set because you've passed them as parameters to the function. I'm sure it's been mentioned earlier already, you should be using isset() directly on $_POST[] array elements.

Link to comment
Share on other sites

$services and $pricelist are always set because you've passed them as parameters to the function. I'm sure it's been mentioned earlier already, you should be using isset() directly on $_POST[] array elements.
I corrected the isset code part of the code-I keep forgetting about the isset.I do not use isset directly on POST because for convenience I have the split what comes from the form($_POST) in other arrays($required, $services etc...). Anyway,,,neither of the above seems to play a role in the "return" issue/problem I mention.Here is revised code(shortened for demo purposes):
function val_evr_else($services,$pricelist){ 	 if (($services!=='') && ($pricelist == '')) {	  $correctdata['phoneempties']=false;		 echo 'you did not enter price';	   return $correctdata['phoneempties'];	 }     }

$correctdata['phoneempties']; ought to be communicated in the outer function, but it is not,$correctdata['phoneempties']; in the outer function is still true

Link to comment
Share on other sites

I just dumped the inner function to do my job.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...