11 Replies - 803 Views - Last Post: 29 March 2012 - 07:40 AM Rate Topic: -----

#1 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

problem checking if variable is empty and set

Posted 29 March 2012 - 06:39 AM

hey there i am working on a project(for learning purposes) and i got stuck when checking if a variable is set and empty, i have a form that is getting submited and just when i enter the page it already displays the error before submiting the form.

here's the code that checks if the form is sent
else if(isset($char) && empty($char)) {
echo("please fill in all fields.");
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem checking if variable is empty and set

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:42 AM

thatís correct. when you load the form for the first time, there will be no form field set. and since you use the same site for the form and its processing Ö

either you make two different scripts (one for display and one for processing) or you check if there is a form submit at all.
Was This Post Helpful? 0
  • +
  • -

#3 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:44 AM

yea but i am checking if its set but it still prints the "please fill in all fields."

here's the code that checks if its set
$port = (isset($_POST['port'])) ? $_POST['port'] : null;

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:46 AM

if you load the page for the first time, $_POST is empty (it’s a GET Request, after all) and that’s where your isset() correctly complains about missing data.

PS. isset(null); // false

This post has been edited by Dormilich: 29 March 2012 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:49 AM

yea i see now :) but is there a way to have them submit the form then check if they wrote anything at all ? because i only want the error to be echoed if they left it empty and then submited the form :P
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:51 AM

View Posttheshowtekfan, on 29 March 2012 - 03:49 PM, said:

yea i see now :) but is there a way to have them submit the form then check if they wrote anything at all ?

thatís what your code currently does.


View Posttheshowtekfan, on 29 March 2012 - 03:49 PM, said:

because i only want the error to be echoed if they left it empty and then submited the form :P

View PostDormilich, on 29 March 2012 - 03:42 PM, said:

either you make two different scripts (one for display and one for processing) or you check if there is a form submit at all.

Was This Post Helpful? 0
  • +
  • -

#7 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 06:59 AM

i really don't get what ur saying :S ur saying that my script works ? that it checks if its set by a "user" and if he skipped that input then it echo's ? because thats not happening, what currently happens is that it echo's it at the second i go to the page with the form on and this line

$port = (isset($_POST['port'])) ? $_POST['port'] : null;


is supposed to check if its set before it gets used to avoid such stuff to happen

so how do i check if the "USER" submits the form and if he left it blank then he gets the error not if he just enters the page randomly...
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 07:06 AM

View Posttheshowtekfan, on 29 March 2012 - 03:59 PM, said:

so how do i check if the "USER" submits the form and if he left it blank then he gets the error not if he just enters the page randomly...

that finally brings us to the question "How does PHP know that there was a form submit via POST?". any ideas?

hint: you need to find something that is only there if the form was submitted.
Was This Post Helpful? 0
  • +
  • -

#9 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 07:18 AM

umm would the correct way be to make a variable for the submit button ? or is there a better/more appropriate way ? :)
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 07:25 AM

this is one viable option.

another option is to check, if the field names are set (even if they are empty)

and yet another option is to check, if a POST request was made.
Was This Post Helpful? 0
  • +
  • -

#11 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 07:31 AM

umm could someone explain in more depth how i could check if the field names are set and if the post request were made
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,052
  • Joined: 08-June 10

Re: problem checking if variable is empty and set

Posted 29 March 2012 - 07:40 AM

if a field name is set you check by either isset(), filter_input() or array_key_exists().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1