Jump to content

checking if form is filled


jimfog

Recommended Posts

I am trying to construct a function that will check if a form is filled, and if the form is indeed filled, then proceed and output some code. Despite I have made a similar question in the past, this one, is based on a different context. First of all, and before writing the function code, here is the underlying logic of the script, not unusual for a script that contains forms. if(filled_out){//proceed with output}else{output_form()} Here is filled_out:

function filled_out($post){    $wasFilledOutCompletely = false;foreach ($post as $key => $value) {    if ((isset($key)) &&(empty($value)))  {	 $wasFilledOutCompletely =  true;	 echo'You did not fill the form entirely ';	 exit;  }	 else	    {$wasFilledOutCompletely =  true;	    echo 'Thanks';	 }    return $wasFilledOutCompletely;}}

The code works OK if the both the fields of the form are empty-in which case "You did not fill the form entirely" is echoed.But if one field is filled and the other not, a "thanks" is echoed. It does not make sense, since the other field is still empty I should again get message that the form is not filled entirely. What complicates things, is that the code works as expected if I remove the else block-I cannot understand how this causes problems. Any ideas?

Link to comment
Share on other sites

You're only testing for one condition at a time. You need code that keeps track of the accumulated tests.

Edited by niche
Link to comment
Share on other sites

What's the difference from this thread that already covers this topic?http://w3schools.inv...78 Anyway,

  • It seems like you don't quite understand the use of the flag. Why would you set $wasCompletelyFilledOut to true, in a conditional block that is specifically testing for empty values? (i.e. false/failure cases). If you still had questions about the flag, or return statements, the other thread was the place to ask
  • In our other thread, I suggested using the flag or return. I don't know where you got exit from, since it intent is to terminate a script. http://php.net/manual/en/function.exit.php
  • Its redundant to check isset on $key, since it wouldn't be there unless it already existed. Even a form field that wasn't filled out, would have a value of an empty string and would pass isset, so no need to use it.
  • Also, you really shouldn't have the messages in your function. Echoing from a function won't make it any more portable or useful in more than one situation, more so when you are trying to create a (seemingly) reusable function.

It still stands to me that my code sample from the other thread still works. I'm not sure what extra functionality you are trying to get out of it here that it didn't already satisfy.

function filled_out($post){  $wasFilledOutCompletely = true;    // test that each key has a non empty string value  foreach ($post as $key => $value) {	 if (empty($value)) {		$wasFilledOutCompletely =  false;	 }  }   return $wasFilledOutCompletely;} if(filled_out($_POST){   echo 'Thanks';}else{   echo 'You forgot to fill something out';};

Edited by thescientist
Link to comment
Share on other sites

What's the difference from this thread that already covers this topic?http://w3schools.inv...78 Anyway,

  • It seems like you don't quite understand the use of the flag. Why would you set $wasCompletelyFilledOut to true, in a conditional block that is specifically testing for empty values? (i.e. false/failure cases). If you still had questions about the flag, or return statements, the other thread was the place to ask
  • In our other thread, I suggested using the flag or return. I don't know where you got exit from, since it intent is to terminate a script. http://php.net/manua...nction.exit.php
  • Its redundant to check isset on $key, since it wouldn't be there unless it already existed. Even a form field that wasn't filled out, would have a value of an empty string and would pass isset, so no need to use it.
  • Also, you really shouldn't have the messages in your function. Echoing from a function won't make it any more portable or useful in more than one situation, more so when you are trying to create a (seemingly) reusable function.

The point I was trying to make was that you let filled out run completely over the array and then return its "findings". It still stands to me that my code sample from the other thread still works. I'm not sure what extra functionality you are trying to get out of it here that it didn't already satisfy.

function filled_out($post){  $wasFilledOutCompletely = true;    // test that each key has a non empty string value  foreach ($post as $key => $value) {	 if (empty($value)) {		$wasFilledOutCompletely =  false;	 }  }   return $wasFilledOutCompletely;} if(filled_out($_POST){   echo 'Thanks';}else{   echo 'You forgot to fill something out';};

Edited by thescientist
Link to comment
Share on other sites

Here is the explanation as to why your code from the other topic does not do what I want in this specific scenario:Your code(the function, when it starts running) it outputs true.This has as a result that the following code will run:

if(filled_out){//proceed with output}output_form

The output contained in the block above, I do not wanted it to run when the user comes in the page in the first place.I wanted to run ONLY if has filled the form. With your code it gets run ANYWAY. Cause your function, outputs true.When the user comes in the page for the first time, I want him to go straight to the form. This can be done if the function outputs false(which your version does not in the first place). This is the logic-that is why a tweaked your function in the first place. Furthermore I am just trying to understand the overall logic here(and yours)-I have not reached a solid understanding yet.And the worst part is that when the conditions of the application change, code must be reworked again-not easy to do, Comments are welcome.

Edited by jimfog
Link to comment
Share on other sites

With your code it gets run ANYWAY. Cause your function, outputs true.When the user comes in the page for the first time, I want him to go straight to the form. This can be done if the function outputs false(which your version does not in the first place). This is the logic-that is why a tweaked your function in the first place. Furthermore I am just trying to understand the overall logic here(and yours)-I have not reached a solid understanding yet.And the worst part is that when the conditions of the application change, code must be reworked again-not easy to do, Comments are welcome.
This sounds more like a problem with your implementation of my code, rather than the code itself. The point of a reusable function like this is to give it one task, and one task only; to check if a form was filled out or not. It shouldn't mix presenation/user output (i.e. messages) or handling page control flow. A simple true/false return should be sufficient to tell you if a form was filled out completely, according to the requirements you are interested in. Other requirements of your application should live in their own functions, control flows, etc. Not all mashed into on small piece of code. To me the obvious thing would be to test if the form was submitted.
if(isset($_POST['submit'])){  if(filled_out($_POST)){	  }else{   }}

Edited by thescientist
Link to comment
Share on other sites

Certainly you got a point in what you say. let me explain why I ended up with such code.Suppose the user comes for the 1st time in index.php-then the login form should be displayed.But in the same page I need code where it will display output in case the user has filled the form. All of these in one page.The above logic is depicted by this:

if(filled_out){//proceed with output}else{output_form()}

The form action of the form is index.php itself.After the user fills the form/logs is=n he will be transferred to the code block which satisfies the if statement above. With your code I do not think I can do that.Of course you could argue that maybe the above logic is not the best-the fact is I am still trying to implement the best code here. And as the coding progresses I see sometimes that I need adjust my code.

Link to comment
Share on other sites

I guess I'm not really sure what the actual problem is. Are you just confused about to handle a login form that redirects to itself, in general?

Edited by thescientist
Link to comment
Share on other sites

Well, the problem is this-sorry If I am not clear,I am hours in front of the PC:Here is the code, the complete, which includes your function. Id the user fills in the form and logins he will be shown member related content:

if (isset($_POST['submit']))		 		  {if(filled_out($_POST))				 {			 $username = $_POST['username'];			 $passwd = $_POST['password'];			 if (login($username, $passwd)) {				 $_SESSION['valid_user'] = $username;				 output_header('output_header_list', $username);	             ....member related functions			 }		 }		 else		 echo   'you did not filled....';		 exit;	 }      output_login_form();

The above works as I wanted. Here is the tricky part for which I started reconsidering the fill_out function.As you see above, a session is created. The problem is that when the user comes to the above page(AFTER THE SESSION IS CREATED)by simply opening a second tab,instead of seeing member content he sees the form. I am trying to build an IF statement that will detects the session and the user will get to see his member section. In the course I realized that maybe I must change filled_out for this to work, but it turned out to be a mess as you saw. So the problem is this, integrating an if (session==true) statement in the above code effectively. Sorry again, but I am tired.

Link to comment
Share on other sites

perhaps from the beginning it would be better if you outlined the key components of your application beforehand. It's hard to help when every new answer brings about a new inclusion of application functionality, which would fundamentally change the just given answer. I know you're tired, and I'm not attacking you, but it saves a lot of time, rather than finding out the bigger picture 9 posts into the thread. So, what you need to do is set a control flow, of precedence, in your application. It seems like the first order of business is to check for $_SESSION, then check if a form was submitted, then default to showing the form. Assuming there are no other requirements to your application, here is an approach.

if(isset($_SESSION['valid_user']) && !empty($_SESSION['valid_user']){  //we have an active SESSION, use it    output_header('output_header_list', $_SESSION['valid_user']);  // ....member related functions}else if(isset($_POST['submit']){  //user has no SESSION, but submitted a form, process it   if(filled_out($_POST)){	$username = $_POST['username'];	$passwd = $_POST['password']; 	if (login($username, $passwd)) {	  $_SESSION['valid_user'] = $username;	  output_header('output_header_list', $username);	  //....member related functions	};  }else{	echo 'You didnt fill out the form completely'; 	output_login_form();  };}else{   //no SESSION or form submit, show page    output_login_form();};

Edited by thescientist
Link to comment
Share on other sites

perhaps from the beginning it would be better if you outlined the key components of your application beforehand. It's hard to help when every new answer brings about a new inclusion of application functionality, which would fundamentally change the just given answer. I know you're tired, and I'm not attacking you, but it saves a lot of time, rather than finding out the bigger picture 9 posts into the thread.
You are not wrong, it was not in my intention to that the topic goes like this.I though that maybe it would be better to narrow the issue and focus on a part of the problem, solve that, and then from there continuemyself.I thought that if I asked from the beginning about the session issue, that would require an extensive answer and in addition posting the wholescript...anyway.
So, what you need to do is set a control flow, of precedence, in your application. It seems like the first order of business is to check for $_SESSION, then check if a form was submitted, then default to showing the form. Assuming there are no other requirements to your application, here is an approach.
I tried another approach-it works too, but if you think it has a flaw(which I cannot detect) say it: if(isset($_SESSION['valid_user'])){//member related functions} For the flag, I have some questions, but I am going to make in the other post, as you proposed. Regarding the key,(isset(key)),if I understood correctly, from the moment the user clicks,even if the fields of the form are empty, the keys of the array will be set.So, I must not use it when when checking if a user has filled a form. Not only for this example here in the topic, but in general.
Link to comment
Share on other sites

Regarding the key,(isset(key)),if I understood correctly, from the moment the user clicks,even if the fields of the form are empty, the keys of the array will be set.So, I must not use it when when checking if a user has filled a form. Not only for this example here in the topic, but in general.
Sorry, I realize I didn't fully explain myself there. It is only redundant in this specific context because you are already iterating through an array. You cannot iterate through members that aren't there, so by virtue of it being POST and a submitted form, but default they will already be "isset". So really checking for empty is only necessary, since an empty string will return true for empty(), as you would want in a validation conditional.
Link to comment
Share on other sites

It is only redundant in this specific context because you are already iterating through an array. You cannot iterate through members that aren't there, so by virtue of it being POST and a submitted form, but default they will already be "isset". So really checking for empty is only necessary, since an empty string will return true for empty(), as you would want in a validation conditional.
You are mentioning that the "members aren't there" yet. Yes, but if someone hits the submit button then they start to exist. What I Understand, is that the reason that I must not check for the keys being set, is that this IS NOT an indication that the fields are filled, in contrast what holds with the values. That said, keys are set, even if the field is empty-so no checking must be made for these. Am I saying it correctly?
Link to comment
Share on other sites

You are mentioning that the "members aren't there" yet. Yes, but if someone hits the submit button then they start to exist. What I Understand, is that the reason that I must not check for the keys being set, is that this IS NOT an indication that the fields are filled, in contrast what holds with the values. That said, keys are set, even if the field is empty-so no checking must be made for these. Am I saying it correctly?
I am not disputing that they aren't there, in fact, that's exactly what I am saying. All I saying is checking for isset on something that you know will already be "isset" (because you are already looping through an array's members) is redundant. The point of using isset, for example, is to make sure you don't access a member of an array that doesn't exist. i.e.
if(isset($_POST['username'])){  echo "Hello " . $_POST['username'];};

If you weren't looping through the array, then you would want to use isset. But since you are already looping through POST, and all the keys will be "isset" by default via form submit (i.e. a value of empty string or something else i.e some user value), then you don't need to call isset on $key.

Edited by thescientist
Link to comment
Share on other sites

SO. in other words, no need to check for isset, since by definition, these array members will be set anyway upon hitting the submit button.Is that what you mean?Thanks for the help-I forgot to tell that.Developing needs patience, and it requires even more if you are trying to explain to someone else.

Edited by jimfog
Link to comment
Share on other sites

It doesn't have anything to do with a submit button, or a form, or $_POST specifically. It has to do with looping through an array using foreach. When you do this: foreach ($post as $key => $value) You are telling PHP to loop through the array and for each item to set the $key and $value variables to certain values in the array. You're telling it to set those variables. So obviously those variables are going to be set, there's no reason to check for that. There is no situation where you would tell PHP to set the variables and they would end up not getting set.

Link to comment
Share on other sites

It doesn't have anything to do with a submit button, or a form, or $_POST specifically. It has to do with looping through an array using foreach. When you do this: foreach ($post as $key => $value) You are telling PHP to loop through the array and for each item to set the $key and $value variables to certain values in the array. You're telling it to set those variables. So obviously those variables are going to be set, there's no reason to check for that. There is no situation where you would tell PHP to set the variables and they would end up not getting set.
You mean that with the foreach statement you cite(and which I also cite in the first post), the key and value variables take their value from the array create?This value assignment does not take-if I got it correct-does not take place upon hitting the submit button but when we use the foreach loop. Am I saying it now correctly? The variables named key and value could have other names of course.
Link to comment
Share on other sites

right. POST in this case will be whatever you see in. in the foreach loop, the $key just represents the member/index, and the value, well, the value of that key/member.

var_dump($_POST)

$_POST is just an associative array. There's nothing super magical about it other than it as a super global (which only affects it scope, not its behavior). Nothing different than just doing this

<?php$user = array(  "username" => "thescientist",  "password" => "abc123"); foreach($user as $key => $value){  echo $key . " is: " . $key ;};

Edited by thescientist
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...