Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Some Basic Questions: Some kind heart, please answer.

    When I try hard but I cannot understand any code, I feel like crying. Can anybody help me?




    if (isset($_POST['password']) && $_POST['name'] != "")



    1. Why is the sign (!) at the end? But usually I noticed that the (!) sign comes at the beginning of the $_post []. Something like if(!isset($_post......))

    2. What does the "" sing mean?

    3. Can I translate the code as following: if the password and name variables are not set.............

    4. Is it necessary to check whether the $_post['submit'] is set or not before checking whether other variables are set or not?



    <input name="check" type="checkbox" id="change_email" value="change_email" />



    1. what is this input?

    2. what is this name="Check"? Is it the name of the field in the table?

    3. what is value="change_email"? Is it the data to be stored in the field of a table?



    Another code from another PHP Coder:

    if(isset($_POST['send']))
    {
    switch($_POST['radio'])
    {
    case 'change_email':
    $ref = ($total - 1);
    break;
    case 'Optout':
    $ref = $total - 2;
    break;
    case 'Remove_Completely':
    $ref = ($total - 3);
    break;
    case 'No_changes':
    $ref = '';
    break;
    default:
    $ref = '';
    break;
    }

    1. The coder has already checked whether the submit button has been pushed or not. But why did not he check whether the $_post['radio'] is set or not?

    2. Can I assume that if the submit button is pushed, $_post['submit'] variable is set? In the same manner, are the variables like $_post['name'], $_post['age'] etc. set, once the user fills up the name and age fields? Or are they set, only the user pushes the submit button?
    Last edited by blankskull; 06-29-2010 at 08:43 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by blankskull View Post
    if (isset($_POST['password']) && $_POST['name'] != "")

    1. Why is the sign (!) at the end? But usually I noticed that the (!) sign comes at the beginning of the $_post []. Something like if(!isset($_post......))
    ! at the beginning of a logic statement means to invert (true becomes false, false becomes true). When you see != it means "not equal".

    Quote Originally Posted by blankskull View Post
    2. What does the "" sing mean?
    Sign*

    Its just comparing the content of the variable ($_POST['name']) to a empty string.

    Quote Originally Posted by blankskull View Post
    3. Can I translate the code as following: if the password and name variables are not set.............
    No. It is "If the post password is set and the post name is not an empty string..."

    Quote Originally Posted by blankskull View Post
    4. Is it necessary to check whether the $_post['submit'] is set or not before checking whether other variables are set or not?
    Its usually a good idea, for security reasons. Plus, sometimes you'll have multiple buttons on a form and each might interact differently with the data.


    Quote Originally Posted by blankskull View Post
    <input name="check" type="checkbox" id="change_email" value="change_email" />


    1. what is this input?
    A checkbox

    Quote Originally Posted by blankskull View Post
    2. what is this name="Check"? Is it the name of the field in the table?
    No, name is how a input is referenced. So if this was being sent by POST, when processed, to read it, you would check $_POST['check'].

    Quote Originally Posted by blankskull View Post
    3. what is value="change_email"? Is it the data to be stored in the field of a table?
    Well, not directly no. Its simply the data being passed along the form. Although for checkboxes, many browsers don't send value, just on/off, true/false, etc.


    Quote Originally Posted by blankskull View Post
    PHP Code:
    if(isset($_POST['send']))
    {
      switch(
    $_POST['radio'])
      {
        case 
    'change_email':
          
    $ref = ($total 1);
          break;
        case 
    'Optout':
          
    $ref $total 2;
          break;
        case 
    'Remove_Completely':
          
    $ref = ($total 3);
          break;
        case 
    'No_changes':
          
    $ref '';
          break;
        default:
          
    $ref '';
          break;
      } 
    First, when asking for help, helps to actually post in a way the helper can accurately read so we can help without our eyes bleeding. Bold is not a good option, but as its written in a few places, the php or code tag is... it actually allows whitespace indent.

    Quote Originally Posted by blankskull View Post
    1. The coder has already checked whether the submit button has been pushed or not. But why did not he check whether the $_post['radio'] is set or not?
    That's what the case statement is doing. If the value doesn't match any of cases given, it goes to the default case, setting $ref to an empty string.

    Quote Originally Posted by blankskull View Post
    2. Can I assume that if the submit button is pushed, $_post['submit'] variable is set? In the same manner, are the variables like $_post['name'], $_post['age'] etc. set, once the user fills up the name and age fields? Or are they set, only the user pushes the submit button?
    Yes, when a submit button is pressed, the $_POST variable with the NAME of the submit button is set (not a variable named submit). What its set to depends on the browser in question.

    As to the second part, its important to note web programming vs software programming... a webpage when loaded is COMPLETELY on the users end, processing is done on the server end (now, there are quibbles such as JS or AJAX, but in the end, its still user data being sent back to the server for processing). The question of if a variable is set when a user enters data into a field is actually moot: there is no variable. When a user hits a submit button, data is now sent to a server, where it is processed into variables. So as a literal answer, the variables are set when the button is hit, but that's only because they are not variables on the users end.
    Last edited by Keleth; 06-29-2010 at 08:56 PM.

  • Users who have thanked Keleth for this post:

    blankskull (06-30-2010)

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    29
    Thanked 1 Time in 1 Post
    1. the (!) sign means no. Is a logic operator.
    for example, in your case
    PHP Code:
    if (isset($_POST['password']) && $_POST['name'] != ""
    means that $_POST]['name'] must be diferent (!=) to nothing ("") to pass as a condition together with the isset($_POST['password']).
    2. the "" means empty, it can also be '' (simple quotes)
    3. Yes you can:
    PHP Code:
    if ( !isset($_POST['password']) && !isset($_POST['name'])) 
    4. Yes you should, check if the form was submitted first, and then validate the form inputs (if they are empty, if they have the expected data, etc)
    If the form was not submited, you may redirect the user to the page that holds the form.


    But why did not he check whether the $_post['radio'] is set or not? maybe it is marked as default (usually the first radio) in the form, but its not always like this, in that case, it is assumed that one radio will remain checked when pushing the send button.

    But in case of the name, password, etc fields, if the user push the send button, they will remain empty.

  • Users who have thanked hernantz for this post:

    blankskull (06-30-2010)

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by hernantz View Post
    3. Yes you can:
    PHP Code:
    if ( !isset($_POST['password']) && !isset($_POST['name'])) 
    That is not the same as the code given

    PHP Code:
    if ( !isset($_POST['password']) && $_POST['name'] != ""
    They do not read the same way.

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    You people are really kind and helpful. Thanks to all of you. Thank you, Keleth.
    Last edited by blankskull; 06-30-2010 at 04:49 AM.

  • #6
    Regular Coder
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    29
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Keleth View Post
    That is not the same as the code given

    PHP Code:
    if ( !isset($_POST['password']) && $_POST['name'] != ""
    They do not read the same way.
    I know, I thought he wanted to to get "if the password and name variables are not set"

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    1. $refrence_user==' '
    2. $Refrence_User=" "


    Dear Keleth and Hernantz, Again I have some more questions:

    1. Are options 1 and 2 the same? I guess that option 2 means that the value of $reference_user variable is set to blank.

    2. But what is the meaning of option 1? Also I guess that the meaning of option 1 is that the value of $reference_user is equal to blank or null. Am I right?

    Again:

    1. $reference_user==' '
    2. $reference_user==" "

    Do the above mentioned options mean the same?
    Last edited by blankskull; 06-30-2010 at 06:16 PM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Code:
    $refrence_user == ' '
    means to check if $refrence_user is set to a space.

    Code:
    $Refrence_User = " "
    means to set $Reference_User to a space.

    Note that they are two difference variables... variables are case sensitive. You changed it in the bottom, but its different (top from bot).

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Another way of saying this:

    Code:
    $variable == ""
    is an expression that evaluates to a boolean by testing if both operands are equal. So (1 == 1) evaluates to true, and (2 == 1) evaluates to false. That's why you see these in conditional statements:

    Code:
    if (1 == 1) /*do something*/ else /*do other thing*/
    So if the expression evaluates to true, the first code block is entered and executed. If the expression evaluates to false, the else block of code is entered and executed.

    Meanwhile:

    Code:
    $variable = "";
    is an assignment. It assigns the value on the right to the location on the left. Like putting your groceries into a bag. You can't do:

    Code:
    1 = $variable;
    Because 1 is not a location, it's a value. It's like trying to put the bag into your groceries.

  • Users who have thanked Beagle for this post:

    blankskull (07-02-2010)

  • #10
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle View Post
    It's like trying to put the bag into your groceries.
    Thanks, Beagle, for translating the code in a man's daily life English. It's easy to understand.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •